Skip to content

Conversation

@vishal2005025
Copy link

Title: Add compile-time _trim_enabled preference during --trim precompilation

Summary

When building with --trim, some packages need to know at precompile time that trimming is enabled so they can register / specialize code accordingly (see SciML/LinearSolve.jl#778). This PR ensures a compile-time preference _trim_enabled is visible to packages during the Pkg.instantiate(); Pkg.precompile() step invoked by JuliaC when trimming is requested.

What I changed

  • Implemented temporary depot-based LocalPreferences injection for precompilation in:
    • src/compiling.jl

Behavior

  • If an ImageRecipe has trimming enabled (i.e. recipe.trim_mode !== nothing && recipe.trim_mode != "no"), the precompile subprocess will:
    1. Create a temporary depot directory.
    2. Write config/LocalPreferences.toml containing:
      [Preferences]
      _trim_enabled = true
      
    3. Invoke Pkg.instantiate(); Pkg.precompile() with JULIA_DEPOT_PATH pointed to the temporary depot so Preferences.load_preference(Preferences, "_trim_enabled") will return true for packages during precompilation.
    4. Clean up the temporary depot after the precompile subprocess finishes.

Why this approach

  • The repo discussion suggested using a LocalPreferences.toml hack so packages can call:
    Preferences.load_preference(Preferences, "_trim_enabled")
    and see a compile-time flag during precompilation.
  • Using a temporary depot avoids touching the user's global depot or permanently modifying configuration.
  • This design is minimal, low-risk, and limited only to the precompile subprocess invoked by JuliaC.

Files edited

  • src/compiling.jl — add logic to create temporary depot, write LocalPreferences.toml, set JULIA_DEPOT_PATH for the precompile subprocess, and cleanup afterward.

Testing & verification

  • Ran the package test suite: julia --project -e "using Pkg; Pkg.test()" — all tests passed locally on Windows.
  • The existing test suite includes trimming-related tests (CLI and programmatic flows) and completed successfully.
  • Suggested additional test (optional): add a small integration test that asserts a package reading the preference during precompile returns true, e.g. a tiny test project that calls Preferences.load_preference(Preferences, "_trim_enabled") in a top-level file and checks the behavior during the Pkg.precompile() invoked by the compile pipeline.

Caveats & notes

  • This relies on packages using Preferences.jl and reading LocalPreferences.toml via Preferences.load_preference. Packages that obtain preferences from other sources will not be affected.
  • The temporary depot is only used during Pkg.instantiate(); Pkg.precompile() subprocess and is removed afterwards; user's depot is not modified.
  • The change affects only the precompile subprocess for trimming-enabled runs. Runtime behavior is unchanged.
  • If you want to expose the preference differently (e.g., via an exported Base function or environment variable), we can iterate — this approach follows the repo discussion and avoids invalidating the global pkgimage cache.

How to try locally

Run the existing test-suite (this was used to validate the change):

julia --project -e "using Pkg; Pkg.test()"

@vishal2005025
Copy link
Author

please review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant