@@ -319,7 +319,8 @@ function create_sysimg_object_file(object_file::String,
319
319
script:: Union{Nothing, String} ,
320
320
sysimage_build_args:: Cmd ,
321
321
extra_precompiles:: String ,
322
- incremental:: Bool )
322
+ incremental:: Bool ,
323
+ import_into_main:: Bool )
323
324
julia_code_buffer = IOBuffer ()
324
325
# include all packages into the sysimg
325
326
print (julia_code_buffer, """
@@ -419,10 +420,21 @@ function create_sysimg_object_file(object_file::String,
419
420
"""
420
421
421
422
# Make packages available in Main. It is unclear if this is the right thing to do.
422
- for pkg in packages
423
- print (julia_code_buffer, """
424
- import $pkg
423
+ if import_into_main
424
+ for pkg in packages
425
+ print (julia_code_buffer, """
426
+ if isdefined(Main, Symbol("$pkg "))
427
+ @warn(
428
+ "Skipping the import of $pkg into Main. A package with this name has " *
429
+ "already been imported. You can disable importing packages into Main " *
430
+ "by setting the `import_into_main` flag to `false`. " *
431
+ "Ref: https://github.com/JuliaLang/PackageCompiler.jl/issues/768"
432
+ )
433
+ else
434
+ import $pkg
435
+ end
425
436
""" )
437
+ end
426
438
end
427
439
428
440
print (julia_code_buffer, precompile_code)
@@ -504,6 +516,9 @@ compiler (can also include extra arguments to the compiler, like `-g`).
504
516
transitive dependencies into the sysimage. This only makes a difference if some
505
517
packages do not load all their dependencies when themselves are loaded. Defaults to `true`.
506
518
519
+ - `import_into_main::Bool`: If `true`, import all packages from `packages` into `Main`.
520
+ This allows calling `using .Package` without the Project.toml the sysimage was built with.
521
+
507
522
### Advanced keyword arguments
508
523
509
524
- `base_sysimage::Union{Nothing, String}`: If a `String`, names an existing sysimage upon which to build
@@ -537,6 +552,7 @@ function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector
537
552
soname= nothing ,
538
553
compat_level:: String = " major" ,
539
554
extra_precompiles:: String = " " ,
555
+ import_into_main:: Bool = true ,
540
556
)
541
557
# We call this at the very beginning to make sure that the user has a compiler available. Therefore, if no compiler
542
558
# is found, we throw an error immediately, instead of making the user wait a while before the error is thrown.
@@ -634,7 +650,8 @@ function create_sysimage(packages::Union{Nothing, Symbol, Vector{String}, Vector
634
650
script,
635
651
sysimage_build_args,
636
652
extra_precompiles,
637
- incremental)
653
+ incremental,
654
+ import_into_main)
638
655
object_files = [object_file]
639
656
if julia_init_c_file != = nothing
640
657
if julia_init_c_file isa String
0 commit comments