Skip to content

Commit b86812d

Browse files
IanButterworthKristofferC
authored and
KristofferC
committed
Fix pkgdir for extensions (#55720)
Fixes #55719 --------- Co-authored-by: Max Horn <[email protected]> (cherry picked from commit 99b8868)
1 parent 6270b90 commit b86812d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

base/loading.jl

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

530544
function get_pkgversion_from_path(path)

test/loading.jl

+15-2
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")
@@ -1100,11 +1112,12 @@ end
11001112

11011113
test_ext_proj = """
11021114
begin
1115+
$test_ext
11031116
using HasExtensions
11041117
using ExtDep
1105-
Base.get_extension(HasExtensions, :Extension) isa Module || error("expected extension to load")
1118+
test_ext(HasExtensions, :Extension)
11061119
using ExtDep2
1107-
Base.get_extension(HasExtensions, :ExtensionFolder) isa Module || error("expected extension to load")
1120+
test_ext(HasExtensions, :ExtensionFolder)
11081121
end
11091122
"""
11101123
for compile in (`--compiled-modules=no`, ``)

0 commit comments

Comments
 (0)