-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Description
Description
The experimental --trim
flag in Julia 1.12.0-rc1 has a bug where selective imports (e.g., using Module: func1, func2
) don't work properly. Variables/functions that are not explicitly imported become undefined, even though they should be accessible within the module's scope.
This issue was discovered while testing the --trim
feature with code patterns from NonlinearSolve.jl PR #665.
Minimal Reproducer
module TestTrimBug
using LinearAlgebra: norm # Selective import - NOT importing I
function test_identity()
return 2I # Will fail with --trim
end
end
TestTrimBug.test_identity()
Save as test_trim.jl
and run with:
julia --experimental --trim test_trim.jl
Expected Behavior
When using using LinearAlgebra: norm
, the module should still have access to all exported symbols from LinearAlgebra (like I
), as per normal Julia semantics.
Actual Behavior
With --trim
, only explicitly imported symbols are available. Attempting to use other symbols results in:
ERROR: LoadError: UndefVarError(:I, 0x000000000000966a, Main.TestTrimBug)
The hexadecimal reference (e.g., 0x000000000000966a
) indicates a compilation issue.
Environment
- Julia Version: 1.12.0-rc1
- Platform: Linux x86_64
- Command:
julia --experimental --trim
Full Test Script
Click to expand full test script
#\!/usr/bin/env julia
module TestTrimImportBug
using LinearAlgebra: norm, dot
# NOT importing: I (identity matrix)
function test_norm()
x = [1.0, 2.0, 3.0]
return norm(x) # This works
end
function test_dot()
x = [1.0, 2.0]
y = [3.0, 4.0]
return dot(x, y) # This works
end
function test_identity()
return 2I # UndefVarError with --trim
end
end
println("Julia \$(VERSION) --trim import bug reproducer")
println("=" ^ 50)
println("\nTesting selective imports with --trim...")
try
result = TestTrimImportBug.test_norm()
println("✓ norm() works: \$result")
catch e
println("✗ norm() failed: \$e")
end
try
result = TestTrimImportBug.test_dot()
println("✓ dot() works: \$result")
catch e
println("✗ dot() failed: \$e")
end
try
result = TestTrimImportBug.test_identity()
println("✓ I (identity) works: \$result")
catch e
println("✗ I (identity) failed: \$e")
if isa(e, UndefVarError)
println(" This is the --trim bug\!")
end
end
Output:
Julia 1.12.0-rc1 --trim import bug reproducer
==================================================
Testing selective imports with --trim...
✓ norm() works: 3.7416573867739413
✓ dot() works: 11.0
✗ I (identity) failed: UndefVarError(:I, 0x000000000000966a, Main.TestTrimImportBug)
This is the --trim bug\!
Impact
This bug affects packages that use selective imports and const global caches, which is a common pattern in optimization and scientific computing packages. The NonlinearSolve.jl trimming tests would fail due to this issue.
Workarounds
- Use
using Module
instead of selective imports - Explicitly import all needed symbols
- Use fully qualified names (e.g.,
LinearAlgebra.I
)
Related
- This may be related to how
--trim
handles module initialization and symbol tables - See gist with full reproducers: https://gist.github.com/ChrisRackauckas-Claude/julia-trim-bug-reproducers
cc: @ChrisRackauckas (for NonlinearSolve.jl context)