Skip to content

Commit b1f8d16

Browse files
authored
only store version of packages during load time when generating sysimage (#46738)
1 parent 100d7e8 commit b1f8d16

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

base/loading.jl

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,18 @@ function pkgdir(m::Module, paths::String...)
460460
return joinpath(dirname(dirname(path)), paths...)
461461
end
462462

463+
function get_pkgversion_from_path(path)
464+
project_file = locate_project_file(path)
465+
if project_file isa String
466+
d = parsed_toml(project_file)
467+
v = get(d, "version", nothing)
468+
if v !== nothing
469+
return VersionNumber(v::String)
470+
end
471+
end
472+
return nothing
473+
end
474+
463475
"""
464476
pkgversion(m::Module)
465477
@@ -477,12 +489,17 @@ the form `pkgversion(@__MODULE__)` can be used.
477489
This function was introduced in Julia 1.9.
478490
"""
479491
function pkgversion(m::Module)
480-
rootmodule = moduleroot(m)
481-
pkg = PkgId(rootmodule)
482-
pkgorigin = @lock require_lock begin
483-
get(pkgorigins, pkg, nothing)
492+
path = pkgdir(m)
493+
path === nothing && return nothing
494+
@lock require_lock begin
495+
v = get_pkgversion_from_path(path)
496+
pkgorigin = get(pkgorigins, PkgId(moduleroot(m)), nothing)
497+
# Cache the version
498+
if pkgorigin !== nothing && pkgorigin.version === nothing
499+
pkgorigin.version = v
500+
end
501+
return v
484502
end
485-
return pkgorigin === nothing ? nothing : pkgorigin.version
486503
end
487504

488505
## generic project & manifest API ##
@@ -1356,13 +1373,9 @@ function set_pkgorigin_version_path(pkg::PkgId, path::Union{String,Nothing})
13561373
assert_havelock(require_lock)
13571374
pkgorigin = get!(PkgOrigin, pkgorigins, pkg)
13581375
if path !== nothing
1359-
project_file = locate_project_file(joinpath(dirname(path), ".."))
1360-
if project_file isa String
1361-
d = parsed_toml(project_file)
1362-
v = get(d, "version", nothing)
1363-
if v !== nothing
1364-
pkgorigin.version = VersionNumber(v::AbstractString)
1365-
end
1376+
# Pkg needs access to the version of packages in the sysimage.
1377+
if Core.Compiler.generating_sysimg()
1378+
pkgorigin.version = get_pkgversion_from_path(joinpath(dirname(path), ".."))
13661379
end
13671380
end
13681381
pkgorigin.path = path

test/loading.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ finally
662662
Base.set_active_project(old_act_proj)
663663
popfirst!(LOAD_PATH)
664664
end
665-
@test Base.pkgorigins[Base.PkgId(UUID("69145d58-7df6-11e8-0660-cf7622583916"), "TestPkg")].version == v"1.2.3"
665+
@test pkgversion(TestPkg) == v"1.2.3"
666666

667667
@testset "--project and JULIA_PROJECT paths should be absolutified" begin
668668
mktempdir() do dir; cd(dir) do

0 commit comments

Comments
 (0)