Skip to content

Commit 99b8868

Browse files
Fix pkgdir for extensions (#55720)
Fixes #55719 --------- Co-authored-by: Max Horn <241512+fingolfin@users.noreply.github.com>
1 parent 88c90ca commit 99b8868

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

base/loading.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ package root.
508508
To get the root directory of the package that implements the current module
509509
the form `pkgdir(@__MODULE__)` can be used.
510510
511+
If an extension module is given, the root of the parent package is returned.
512+
511513
```julia-repl
512514
julia> pkgdir(Foo)
513515
"/path/to/Foo.jl"
@@ -525,7 +527,19 @@ function pkgdir(m::Module, paths::String...)
525527
rootmodule = moduleroot(m)
526528
path = pathof(rootmodule)
527529
path === nothing && return nothing
528-
return joinpath(dirname(dirname(path)), paths...)
530+
original = path
531+
path, base = splitdir(dirname(path))
532+
if base == "src"
533+
# package source in `../src/Foo.jl`
534+
elseif base == "ext"
535+
# extension source in `../ext/FooExt.jl`
536+
elseif basename(path) == "ext"
537+
# extension source in `../ext/FooExt/FooExt.jl`
538+
path = dirname(path)
539+
else
540+
error("Unexpected path structure for module source: $original")
541+
end
542+
return joinpath(path, paths...)
529543
end
530544

531545
function get_pkgversion_from_path(path)

test/loading.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,16 @@ end
10341034
end
10351035

10361036
@testset "Extensions" begin
1037+
test_ext = """
1038+
function test_ext(parent::Module, ext::Symbol)
1039+
_ext = Base.get_extension(parent, ext)
1040+
_ext isa Module || error("expected extension \$ext to be loaded")
1041+
_pkgdir = pkgdir(_ext)
1042+
_pkgdir == pkgdir(parent) != nothing || error("unexpected extension \$ext pkgdir path: \$_pkgdir")
1043+
_pkgversion = pkgversion(_ext)
1044+
_pkgversion == pkgversion(parent) || error("unexpected extension \$ext version: \$_pkgversion")
1045+
end
1046+
"""
10371047
depot_path = mktempdir()
10381048
try
10391049
proj = joinpath(@__DIR__, "project", "Extensions", "HasDepWithExtensions.jl")
@@ -1044,13 +1054,15 @@ end
10441054
cmd = """
10451055
$load_distr
10461056
begin
1057+
$ew $test_ext
10471058
$ew push!(empty!(DEPOT_PATH), $(repr(depot_path)))
10481059
using HasExtensions
10491060
$ew using HasExtensions
10501061
$ew Base.get_extension(HasExtensions, :Extension) === nothing || error("unexpectedly got an extension")
10511062
$ew HasExtensions.ext_loaded && error("ext_loaded set")
10521063
using HasDepWithExtensions
10531064
$ew using HasDepWithExtensions
1065+
$ew test_ext(HasExtensions, :Extension)
10541066
$ew Base.get_extension(HasExtensions, :Extension).extvar == 1 || error("extvar in Extension not set")
10551067
$ew HasExtensions.ext_loaded || error("ext_loaded not set")
10561068
$ew HasExtensions.ext_folder_loaded && error("ext_folder_loaded set")
@@ -1102,13 +1114,14 @@ end
11021114

11031115
test_ext_proj = """
11041116
begin
1117+
$test_ext
11051118
using HasExtensions
11061119
using ExtDep
1107-
Base.get_extension(HasExtensions, :Extension) isa Module || error("expected extension to load")
1120+
test_ext(HasExtensions, :Extension)
11081121
using ExtDep2
1109-
Base.get_extension(HasExtensions, :ExtensionFolder) isa Module || error("expected extension to load")
1122+
test_ext(HasExtensions, :ExtensionFolder)
11101123
using ExtDep3
1111-
Base.get_extension(HasExtensions, :ExtensionDep) isa Module || error("expected extension to load")
1124+
test_ext(HasExtensions, :ExtensionDep)
11121125
end
11131126
"""
11141127
for compile in (`--compiled-modules=no`, ``)

0 commit comments

Comments
 (0)