Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import_to_main flag for Main module package name conflicts resolution #807

Merged
merged 9 commits into from
Oct 9, 2024
27 changes: 22 additions & 5 deletions src/PackageCompiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ function create_sysimg_object_file(object_file::String,
script::Union{Nothing, String},
sysimage_build_args::Cmd,
extra_precompiles::String,
incremental::Bool)
incremental::Bool,
import_into_main::Bool)
julia_code_buffer = IOBuffer()
# include all packages into the sysimg
print(julia_code_buffer, """
Expand Down Expand Up @@ -409,10 +410,21 @@ function create_sysimg_object_file(object_file::String,
"""

# Make packages available in Main. It is unclear if this is the right thing to do.
for pkg in packages
print(julia_code_buffer, """
import $pkg
if import_into_main
for pkg in packages
print(julia_code_buffer, """
if isdefined(Main, Symbol("$pkg"))
@warn(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you check that users will see this warning?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw it on a build once but let me confirm on a simple example

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

visible
image

"Skipping the import of $pkg into Main. A package with this name has " *
"already been imported. You can disable importing packages into Main " *
"by setting the `import_into_main` flag to `false`. " *
"Ref: https://github.com/JuliaLang/PackageCompiler.jl/issues/768"
)
else
import $pkg
end
""")
end
end

print(julia_code_buffer, precompile_code)
Expand Down Expand Up @@ -484,6 +496,9 @@ compiler (can also include extra arguments to the compiler, like `-g`).
transitive dependencies into the sysimage. This only makes a difference if some
packages do not load all their dependencies when themselves are loaded. Defaults to `true`.

- `import_into_main::Bool`: If `true`, import all packages from the sysimage into `Main`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this true? I thought it was just the packages in the [deps] section of the project file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I believe only deps from the project you're creating a sysimage of go in

Suggested change
- `import_into_main::Bool`: If `true`, import all packages from the sysimage into `Main`.
- `import_into_main::Bool`: If `true`, import all packages from `packages` into `Main`.

This is more accurate I guess

This allows calling `using .Package` without the Project.toml the sysimage was built with.

### Advanced keyword arguments

- `base_sysimage::Union{Nothing, String}`: If a `String`, names an existing sysimage upon which to build
Expand Down Expand Up @@ -516,6 +531,7 @@ function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector
soname=nothing,
compat_level::String="major",
extra_precompiles::String = "",
import_into_main::Bool=true,
)
# We call this at the very beginning to make sure that the user has a compiler available. Therefore, if no compiler
# is found, we throw an error immediately, instead of making the user wait a while before the error is thrown.
Expand Down Expand Up @@ -609,7 +625,8 @@ function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector
script,
sysimage_build_args,
extra_precompiles,
incremental)
incremental,
import_into_main)
object_files = [object_file]
if julia_init_c_file !== nothing
push!(object_files, compile_c_init_julia(julia_init_c_file, basename(sysimage_path)))
Expand Down