Skip to content

Commit d830b45

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 b2be809 commit d830b45

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
@@ -536,6 +536,8 @@ package root.
536536
To get the root directory of the package that implements the current module
537537
the form `pkgdir(@__MODULE__)` can be used.
538538
539+
If an extension module is given, the root of the parent package is returned.
540+
539541
```julia-repl
540542
julia> pkgdir(Foo)
541543
"/path/to/Foo.jl"
@@ -551,7 +553,19 @@ function pkgdir(m::Module, paths::String...)
551553
rootmodule = moduleroot(m)
552554
path = pathof(rootmodule)
553555
path === nothing && return nothing
554-
return joinpath(dirname(dirname(path)), paths...)
556+
original = path
557+
path, base = splitdir(dirname(path))
558+
if base == "src"
559+
# package source in `../src/Foo.jl`
560+
elseif base == "ext"
561+
# extension source in `../ext/FooExt.jl`
562+
elseif basename(path) == "ext"
563+
# extension source in `../ext/FooExt/FooExt.jl`
564+
path = dirname(path)
565+
else
566+
error("Unexpected path structure for module source: $original")
567+
end
568+
return joinpath(path, paths...)
555569
end
556570

557571
function get_pkgversion_from_path(path)

test/loading.jl

+15-2
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,16 @@ end
10041004
end
10051005

10061006
@testset "Extensions" begin
1007+
test_ext = """
1008+
function test_ext(parent::Module, ext::Symbol)
1009+
_ext = Base.get_extension(parent, ext)
1010+
_ext isa Module || error("expected extension \$ext to be loaded")
1011+
_pkgdir = pkgdir(_ext)
1012+
_pkgdir == pkgdir(parent) != nothing || error("unexpected extension \$ext pkgdir path: \$_pkgdir")
1013+
_pkgversion = pkgversion(_ext)
1014+
_pkgversion == pkgversion(parent) || error("unexpected extension \$ext version: \$_pkgversion")
1015+
end
1016+
"""
10071017
depot_path = mktempdir()
10081018
try
10091019
proj = joinpath(@__DIR__, "project", "Extensions", "HasDepWithExtensions.jl")
@@ -1014,13 +1024,15 @@ end
10141024
cmd = """
10151025
$load_distr
10161026
begin
1027+
$ew $test_ext
10171028
$ew push!(empty!(DEPOT_PATH), $(repr(depot_path)))
10181029
using HasExtensions
10191030
$ew using HasExtensions
10201031
$ew Base.get_extension(HasExtensions, :Extension) === nothing || error("unexpectedly got an extension")
10211032
$ew HasExtensions.ext_loaded && error("ext_loaded set")
10221033
using HasDepWithExtensions
10231034
$ew using HasDepWithExtensions
1035+
$ew test_ext(HasExtensions, :Extension)
10241036
$ew Base.get_extension(HasExtensions, :Extension).extvar == 1 || error("extvar in Extension not set")
10251037
$ew HasExtensions.ext_loaded || error("ext_loaded not set")
10261038
$ew HasExtensions.ext_folder_loaded && error("ext_folder_loaded set")
@@ -1070,11 +1082,12 @@ end
10701082

10711083
test_ext_proj = """
10721084
begin
1085+
$test_ext
10731086
using HasExtensions
10741087
using ExtDep
1075-
Base.get_extension(HasExtensions, :Extension) isa Module || error("expected extension to load")
1088+
test_ext(HasExtensions, :Extension)
10761089
using ExtDep2
1077-
Base.get_extension(HasExtensions, :ExtensionFolder) isa Module || error("expected extension to load")
1090+
test_ext(HasExtensions, :ExtensionFolder)
10781091
end
10791092
"""
10801093
for compile in (`--compiled-modules=no`, ``)

0 commit comments

Comments
 (0)