@@ -460,6 +460,18 @@ function pkgdir(m::Module, paths::String...)
460
460
return joinpath (dirname (dirname (path)), paths... )
461
461
end
462
462
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
+
463
475
"""
464
476
pkgversion(m::Module)
465
477
@@ -477,12 +489,17 @@ the form `pkgversion(@__MODULE__)` can be used.
477
489
This function was introduced in Julia 1.9.
478
490
"""
479
491
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
484
502
end
485
- return pkgorigin === nothing ? nothing : pkgorigin. version
486
503
end
487
504
488
505
# # generic project & manifest API ##
@@ -1356,13 +1373,9 @@ function set_pkgorigin_version_path(pkg::PkgId, path::Union{String,Nothing})
1356
1373
assert_havelock (require_lock)
1357
1374
pkgorigin = get! (PkgOrigin, pkgorigins, pkg)
1358
1375
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), " .." ))
1366
1379
end
1367
1380
end
1368
1381
pkgorigin. path = path
0 commit comments