diff --git a/base/loading.jl b/base/loading.jl index 43ff380fd62ca..433c7c5521c69 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -460,6 +460,18 @@ function pkgdir(m::Module, paths::String...) return joinpath(dirname(dirname(path)), paths...) end +function get_pkgversion_from_path(path) + project_file = locate_project_file(path) + if project_file isa String + d = parsed_toml(project_file) + v = get(d, "version", nothing) + if v !== nothing + return VersionNumber(v::String) + end + end + return nothing +end + """ pkgversion(m::Module) @@ -477,12 +489,17 @@ the form `pkgversion(@__MODULE__)` can be used. This function was introduced in Julia 1.9. """ function pkgversion(m::Module) - rootmodule = moduleroot(m) - pkg = PkgId(rootmodule) - pkgorigin = @lock require_lock begin - get(pkgorigins, pkg, nothing) + path = pkgdir(m) + path === nothing && return nothing + @lock require_lock begin + v = get_pkgversion_from_path(path) + pkgorigin = get(pkgorigins, PkgId(moduleroot(m)), nothing) + # Cache the version + if pkgorigin !== nothing && pkgorigin.version === nothing + pkgorigin.version = v + end + return v end - return pkgorigin === nothing ? nothing : pkgorigin.version end ## generic project & manifest API ## @@ -1356,13 +1373,9 @@ function set_pkgorigin_version_path(pkg::PkgId, path::Union{String,Nothing}) assert_havelock(require_lock) pkgorigin = get!(PkgOrigin, pkgorigins, pkg) if path !== nothing - project_file = locate_project_file(joinpath(dirname(path), "..")) - if project_file isa String - d = parsed_toml(project_file) - v = get(d, "version", nothing) - if v !== nothing - pkgorigin.version = VersionNumber(v::AbstractString) - end + # Pkg needs access to the version of packages in the sysimage. + if Core.Compiler.generating_sysimg() + pkgorigin.version = get_pkgversion_from_path(joinpath(dirname(path), "..")) end end pkgorigin.path = path diff --git a/test/loading.jl b/test/loading.jl index 7ca540e6602d9..d057f0b3c3702 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -662,7 +662,7 @@ finally Base.set_active_project(old_act_proj) popfirst!(LOAD_PATH) end -@test Base.pkgorigins[Base.PkgId(UUID("69145d58-7df6-11e8-0660-cf7622583916"), "TestPkg")].version == v"1.2.3" +@test pkgversion(TestPkg) == v"1.2.3" @testset "--project and JULIA_PROJECT paths should be absolutified" begin mktempdir() do dir; cd(dir) do