Skip to content

Commit 3d38bb9

Browse files
add Pkg.precompile(f) do for delayed precompilation
1 parent 36959ee commit 3d38bb9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/API.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,13 @@ function precompile(ctx::Context, pkgs::Vector{PackageSpec}; internal_call::Bool
11761176
end
11771177
end
11781178

1179+
function precompile(f, args...; kwargs...)
1180+
Base.ScopedValues.@with _autoprecompilation_enabled_scoped => false begin
1181+
f()
1182+
Pkg.precompile(args...; kwargs...)
1183+
end
1184+
end
1185+
11791186
function tree_hash(repo::LibGit2.GitRepo, tree_hash::String)
11801187
try
11811188
return LibGit2.GitObject(repo, tree_hash)

src/Pkg.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ usable_io(io) = (io isa Base.TTY) || (io isa IOContext{IO} && io.io isa Base.TTY
6565
can_fancyprint(io::IO) = (usable_io(io)) && (get(ENV, "CI", nothing) != "true")
6666

6767
_autoprecompilation_enabled::Bool = true
68-
_autoprecompilation_enabled_scoped = ScopedValue{Bool}(true)
68+
_autoprecompilation_enabled_scoped = Base.ScopedValues.ScopedValue{Bool}(true)
6969
autoprecompilation_enabled(state::Bool) = (autoprecompilation_enabled = state)
7070
function should_autoprecompile()
7171
if Base.JLOptions().use_compiled_modules == 1 &&
@@ -204,11 +204,21 @@ const add = API.add
204204
Pkg.precompile(; strict::Bool=false, timing::Bool=false)
205205
Pkg.precompile(pkg; strict::Bool=false, timing::Bool=false)
206206
Pkg.precompile(pkgs; strict::Bool=false, timing::Bool=false)
207+
Pkg.precompile(f, args...; kwargs...)
207208
208209
Precompile all or specific dependencies of the project in parallel.
209210
210211
Set `timing=true` to show the duration of the precompilation of each dependency.
211212
213+
To delay autoprecompilation of multiple Pkg actions until the end use.
214+
This may be most efficient while manipulating the environment in various ways.
215+
216+
```julia
217+
Pkg.precompile() do
218+
# Pkg actions here
219+
end
220+
```
221+
212222
!!! note
213223
Errors will only throw when precompiling the top-level dependencies, given that
214224
not all manifest dependencies may be loaded by the top-level dependencies on the given system.

0 commit comments

Comments
 (0)