diff --git a/Project.toml b/Project.toml index 3ff423a..f060283 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ PackageCompiler = "9b87118b-4619-50d2-8e1e-99f35a4d4d9d" Patchelf_jll = "f2cf89d6-2bfd-5c44-bd2c-068eea195c0c" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00" +Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" StructIO = "53d494c1-5632-5724-8f4c-31dff12d585f" [compat] @@ -23,6 +24,7 @@ PackageCompiler = "2" Patchelf_jll = "0.18" Pkg = "1" RelocatableFolders = "1" +Revise = "3.11.0" StructIO = "0.3" julia = "1.10" diff --git a/src/compiling.jl b/src/compiling.jl index ebb0db4..8211752 100644 --- a/src/compiling.jl +++ b/src/compiling.jl @@ -78,12 +78,43 @@ function compile_products(recipe::ImageRecipe) end project_arg = recipe.project == "" ? Base.active_project() : recipe.project + # Prepare environment overrides for the precompile step. We clear JULIA_LOAD_PATH + # by default so precompilation happens in a clean environment. If trimming is + # enabled, create a temporary depot with a LocalPreferences.toml so packages can + # read a compile-time preference (e.g. Preferences.load_preference(Preferences, "_trim_enabled")). env_overrides = Dict{String,Any}("JULIA_LOAD_PATH"=>nothing) + tmp_depot = nothing + if is_trim_enabled(recipe) + # Create a temporary depot and write LocalPreferences.toml with _trim_enabled = true + tmp_depot = mktempdir() + prefs_dir = joinpath(tmp_depot, "config") + mkpath(prefs_dir) + prefs_file = joinpath(prefs_dir, "LocalPreferences.toml") + open(prefs_file, "w") do io + println(io, "[Preferences]") + println(io, "_trim_enabled = true") + end + # Use this temporary depot for the precompile subprocess so Preferences.jl + # will pick up the compile-time preference. + env_overrides["JULIA_DEPOT_PATH"] = tmp_depot + end + inst_cmd = addenv(`$(Base.julia_cmd(cpu_target=precompile_cpu_target)) --project=$project_arg -e "using Pkg; Pkg.instantiate(); Pkg.precompile()"`, env_overrides...) recipe.verbose && println("Running: $inst_cmd") precompile_time = time_ns() - if !success(pipeline(inst_cmd; stdout, stderr)) - error("Error encountered during instantiate/precompile of app project.") + try + if !success(pipeline(inst_cmd; stdout, stderr)) + error("Error encountered during instantiate/precompile of app project.") + end + finally + # Cleanup temporary depot if created + if tmp_depot !== nothing + try + rm(tmp_depot; recursive=true, force=true) + catch + @warn "Failed to remove temporary depot: $tmp_depot" + end + end end recipe.verbose && println("Precompilation took $((time_ns() - precompile_time)/1e9) s") # Compile the Julia code