From 12bf8dbe21027c0b3aae46be29cf89d8a69dc8d0 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 9 Sep 2024 00:19:47 -0400 Subject: [PATCH 1/7] fix pkgdir for extensions --- base/loading.jl | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/base/loading.jl b/base/loading.jl index e0a2563dd0178..023d69c73accd 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -508,6 +508,8 @@ package root. To get the root directory of the package that implements the current module the form `pkgdir(@__MODULE__)` can be used. +If an extension module is given, the root of the parent package is returned. + ```julia-repl julia> pkgdir(Foo) "/path/to/Foo.jl" @@ -525,7 +527,23 @@ function pkgdir(m::Module, paths::String...) rootmodule = moduleroot(m) path = pathof(rootmodule) path === nothing && return nothing - return joinpath(dirname(dirname(path)), paths...) + original = path + path = dirname(path) + if endswith(path, "src") + path = dirname(path) + elseif contains(path, "ext") + # extensions can reside at `../ext/FooExt.jl` or `../ext/FooExt/FooExt.jl` + if endswith(path, "ext") + path = dirname(path) + else + path = dirname(path) + endswith(path, "ext") || error("Unexpected path structure for extension module: $original") + path = dirname(path) + end + else + error("Unexpected path structure for module source: $original") + end + return joinpath(path, paths...) end function get_pkgversion_from_path(path) From 28e616211a74d2679b5fdbbc1689887267eec91a Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 9 Sep 2024 09:09:50 -0400 Subject: [PATCH 2/7] add tests --- test/loading.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/loading.jl b/test/loading.jl index fe6f800276547..919743b58a0a1 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -1051,6 +1051,9 @@ end $ew HasExtensions.ext_loaded && error("ext_loaded set") using HasDepWithExtensions $ew using HasDepWithExtensions + $ew Base.get_extension(HasExtensions, :Extension) isa Module || error("expected extension to load") + $ew _pkgdir = pkgdir(Base.get_extension(HasExtensions, :Extension)) + $ew _pkgdir == pkgdir(HasExtensions) != nothing || error("unexpected extension pkgdir path") $ew Base.get_extension(HasExtensions, :Extension).extvar == 1 || error("extvar in Extension not set") $ew HasExtensions.ext_loaded || error("ext_loaded not set") $ew HasExtensions.ext_folder_loaded && error("ext_folder_loaded set") @@ -1105,10 +1108,16 @@ end using HasExtensions using ExtDep Base.get_extension(HasExtensions, :Extension) isa Module || error("expected extension to load") + _pkgdir = pkgdir(Base.get_extension(HasExtensions, :Extension)) + _pkgdir == pkgdir(HasExtensions) != nothing || error("unexpected extension pkgdir path: $_pkgdir") using ExtDep2 Base.get_extension(HasExtensions, :ExtensionFolder) isa Module || error("expected extension to load") + _pkgdir = pkgdir(Base.get_extension(HasExtensions, :ExtensionFolder)) + _pkgdir == pkgdir(HasExtensions) != nothing || error("unexpected extension pkgdir path: $_pkgdir") using ExtDep3 Base.get_extension(HasExtensions, :ExtensionDep) isa Module || error("expected extension to load") + _pkgdir = pkgdir(Base.get_extension(HasExtensions, :ExtensionDep)) + _pkgdir == pkgdir(HasExtensions) != nothing || error("unexpected extension pkgdir path: $_pkgdir") end """ for compile in (`--compiled-modules=no`, ``) From b5d832829c960009bc8c1b147143ee7d249f86e0 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 9 Sep 2024 09:54:06 -0400 Subject: [PATCH 3/7] tidy tests avoiding duplication --- test/loading.jl | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/test/loading.jl b/test/loading.jl index 919743b58a0a1..8db8405ef2a83 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -1034,6 +1034,16 @@ end end @testset "Extensions" begin + test_ext = """ + function test_ext(parent::Module, ext::Symbol) + _ext = Base.get_extension(parent, ext) + _ext isa Module || error("expected extension \$ext to be loaded") + _pkgdir = pkgdir(_ext) + _pkgdir == pkgdir(parent) != nothing || error("unexpected extension \$ext pkgdir path: \$_pkgdir") + _pkgversion = pkgversion(_ext) + _pkgversion == pkgversion(parent) || error("unexpected extension \$ext version: \$_pkgversion") + end + """ depot_path = mktempdir() try proj = joinpath(@__DIR__, "project", "Extensions", "HasDepWithExtensions.jl") @@ -1044,6 +1054,7 @@ end cmd = """ $load_distr begin + $ew $test_ext $ew push!(empty!(DEPOT_PATH), $(repr(depot_path))) using HasExtensions $ew using HasExtensions @@ -1051,9 +1062,7 @@ end $ew HasExtensions.ext_loaded && error("ext_loaded set") using HasDepWithExtensions $ew using HasDepWithExtensions - $ew Base.get_extension(HasExtensions, :Extension) isa Module || error("expected extension to load") - $ew _pkgdir = pkgdir(Base.get_extension(HasExtensions, :Extension)) - $ew _pkgdir == pkgdir(HasExtensions) != nothing || error("unexpected extension pkgdir path") + $ew test_ext(HasExtensions, :Extension) $ew Base.get_extension(HasExtensions, :Extension).extvar == 1 || error("extvar in Extension not set") $ew HasExtensions.ext_loaded || error("ext_loaded not set") $ew HasExtensions.ext_folder_loaded && error("ext_folder_loaded set") @@ -1105,19 +1114,14 @@ end test_ext_proj = """ begin + $test_ext using HasExtensions using ExtDep - Base.get_extension(HasExtensions, :Extension) isa Module || error("expected extension to load") - _pkgdir = pkgdir(Base.get_extension(HasExtensions, :Extension)) - _pkgdir == pkgdir(HasExtensions) != nothing || error("unexpected extension pkgdir path: $_pkgdir") + test_ext(HasExtensions, :Extension) using ExtDep2 - Base.get_extension(HasExtensions, :ExtensionFolder) isa Module || error("expected extension to load") - _pkgdir = pkgdir(Base.get_extension(HasExtensions, :ExtensionFolder)) - _pkgdir == pkgdir(HasExtensions) != nothing || error("unexpected extension pkgdir path: $_pkgdir") + test_ext(HasExtensions, :ExtensionFolder) using ExtDep3 - Base.get_extension(HasExtensions, :ExtensionDep) isa Module || error("expected extension to load") - _pkgdir = pkgdir(Base.get_extension(HasExtensions, :ExtensionDep)) - _pkgdir == pkgdir(HasExtensions) != nothing || error("unexpected extension pkgdir path: $_pkgdir") + test_ext(HasExtensions, :ExtensionDep) end """ for compile in (`--compiled-modules=no`, ``) From 69a879584bb8af28ed8ecf567d94de32a8f3c5c8 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 9 Sep 2024 11:23:15 -0400 Subject: [PATCH 4/7] fix dir check --- base/loading.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/loading.jl b/base/loading.jl index 023d69c73accd..c90928b17e83b 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -529,15 +529,15 @@ function pkgdir(m::Module, paths::String...) path === nothing && return nothing original = path path = dirname(path) - if endswith(path, "src") + if splitpath(path)[end] == "src" path = dirname(path) elseif contains(path, "ext") # extensions can reside at `../ext/FooExt.jl` or `../ext/FooExt/FooExt.jl` - if endswith(path, "ext") + if splitpath(path)[end] == "ext" path = dirname(path) else path = dirname(path) - endswith(path, "ext") || error("Unexpected path structure for extension module: $original") + splitpath(path)[end] == "ext" || error("Unexpected path structure for extension module: $original") path = dirname(path) end else From 2417012f631be5451262979bd9ed57af902ae476 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 9 Sep 2024 11:25:04 -0400 Subject: [PATCH 5/7] more fix --- base/loading.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/loading.jl b/base/loading.jl index c90928b17e83b..20302cb5b70af 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -531,7 +531,7 @@ function pkgdir(m::Module, paths::String...) path = dirname(path) if splitpath(path)[end] == "src" path = dirname(path) - elseif contains(path, "ext") + elseif "ext" in splitpath(path) # extensions can reside at `../ext/FooExt.jl` or `../ext/FooExt/FooExt.jl` if splitpath(path)[end] == "ext" path = dirname(path) From 5c3b0d0e393938e0fb39c616359bddcc4e04d567 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 9 Sep 2024 12:48:46 -0400 Subject: [PATCH 6/7] simplify Co-Authored-By: Max Horn <241512+fingolfin@users.noreply.github.com> --- base/loading.jl | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/base/loading.jl b/base/loading.jl index 20302cb5b70af..4b4a5dc906df3 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -528,18 +528,14 @@ function pkgdir(m::Module, paths::String...) path = pathof(rootmodule) path === nothing && return nothing original = path - path = dirname(path) - if splitpath(path)[end] == "src" + path, base = splitdir(path) + if base == "src" + # package source in `../src/Foo.jl` + elseif base == "ext" + # extension source in `../ext/FooExt.jl` + elseif basename(path) == "ext" + # extension source in `../ext/FooExt/FooExt.jl` path = dirname(path) - elseif "ext" in splitpath(path) - # extensions can reside at `../ext/FooExt.jl` or `../ext/FooExt/FooExt.jl` - if splitpath(path)[end] == "ext" - path = dirname(path) - else - path = dirname(path) - splitpath(path)[end] == "ext" || error("Unexpected path structure for extension module: $original") - path = dirname(path) - end else error("Unexpected path structure for module source: $original") end From 41c47affa53e45a52cb0bb3f59de9a66ec81ef24 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Mon, 9 Sep 2024 13:47:22 -0400 Subject: [PATCH 7/7] oh for the want of testing before pushing --- base/loading.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/loading.jl b/base/loading.jl index 4b4a5dc906df3..fe86a8c198461 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -528,7 +528,7 @@ function pkgdir(m::Module, paths::String...) path = pathof(rootmodule) path === nothing && return nothing original = path - path, base = splitdir(path) + path, base = splitdir(dirname(path)) if base == "src" # package source in `../src/Foo.jl` elseif base == "ext"