Skip to content

Commit 67f8ad1

Browse files
committed
allow loading extensions of the active project
1 parent ce81774 commit 67f8ad1

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

base/loading.jl

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,24 @@ function manifest_deps_get(env::String, where::PkgId, name::String)::Union{Nothi
623623
pkg_uuid = explicit_project_deps_get(project_file, name)
624624
return PkgId(pkg_uuid, name)
625625
end
626+
d = parsed_toml(project_file)
627+
exts = get(d, "extensions", nothing)::Union{Dict{String, Any}, Nothing}
628+
if exts !== nothing
629+
# Check if `where` is an extension of the project
630+
if where.name in keys(exts) && where.uuid == uuid5(proj.uuid, where.name)
631+
# Extensions can load weak deps...
632+
weakdeps = get(d, "weakdeps", nothing)::Union{Dict{String, Any}, Nothing}
633+
if weakdeps !== nothing
634+
wuuid = get(weakdeps, name, nothing)::Union{String, Nothing}
635+
if wuuid !== nothing
636+
return PkgId(UUID(wuuid), name)
637+
end
638+
# ... and they can load same deps as the project itself
639+
mby_uuid = explicit_project_deps_get(project_file, name)
640+
mby_uuid === nothing || return PkgId(mby_uuid, name)
641+
end
642+
end
643+
end
626644
# look for manifest file and `where` stanza
627645
return explicit_manifest_deps_get(project_file, where, name)
628646
elseif project_file
@@ -640,6 +658,8 @@ function manifest_uuid_path(env::String, pkg::PkgId)::Union{Nothing,String,Missi
640658
# if `pkg` matches the project, return the project itself
641659
return project_file_path(project_file)
642660
end
661+
mby_ext = project_file_ext_path(project_file, pkg.name)
662+
mby_ext === nothing || return mby_ext
643663
# look for manifest file and `where` stanza
644664
return explicit_manifest_uuid_path(project_file, pkg)
645665
elseif project_file
@@ -649,6 +669,25 @@ function manifest_uuid_path(env::String, pkg::PkgId)::Union{Nothing,String,Missi
649669
return nothing
650670
end
651671

672+
673+
function find_ext_path(project_path::String, extname::String)
674+
extfiledir = joinpath(project_path, "ext", extname, extname * ".jl")
675+
isfile(extfiledir) && return extfiledir
676+
return joinpath(project_path, "ext", extname * ".jl")
677+
end
678+
679+
function project_file_ext_path(project_file::String, name::String)
680+
d = parsed_toml(project_file)
681+
p = project_file_path(project_file)
682+
exts = get(d, "extensions", nothing)::Union{Dict{String, Any}, Nothing}
683+
if exts !== nothing
684+
if name in keys(exts)
685+
return find_ext_path(p, name)
686+
end
687+
end
688+
return nothing
689+
end
690+
652691
# find project file's top-level UUID entry (or nothing)
653692
function project_file_name_uuid(project_file::String, name::String)::PkgId
654693
d = parsed_toml(project_file)
@@ -880,9 +919,7 @@ function explicit_manifest_uuid_path(project_file::String, pkg::PkgId)::Union{No
880919
error("failed to find source of parent package: \"$name\"")
881920
end
882921
p = normpath(dirname(parent_path), "..")
883-
extfiledir = joinpath(p, "ext", pkg.name, pkg.name * ".jl")
884-
isfile(extfiledir) && return extfiledir
885-
return joinpath(p, "ext", pkg.name * ".jl")
922+
return find_ext_path(p, pkg.name)
886923
end
887924
end
888925
end
@@ -1164,6 +1201,18 @@ end
11641201
function insert_extension_triggers(env::String, pkg::PkgId)::Union{Nothing,Missing}
11651202
project_file = env_project_file(env)
11661203
if project_file isa String
1204+
# Look in project for extensions to insert
1205+
proj_pkg = project_file_name_uuid(project_file, pkg.name)
1206+
if pkg == proj_pkg
1207+
d_proj = parsed_toml(project_file)
1208+
weakdeps = get(d_proj, "weakdeps", nothing)::Union{Nothing, Vector{String}, Dict{String,Any}}
1209+
extensions = get(d_proj, "extensions", nothing)::Union{Nothing, Dict{String, Any}}
1210+
extensions === nothing && return
1211+
weakdeps === nothing && return
1212+
return _insert_extension_triggers(pkg, extensions, weakdeps)
1213+
end
1214+
1215+
# Now look in manifest
11671216
manifest_file = project_file_manifest_path(project_file)
11681217
manifest_file === nothing && return
11691218
d = get_deps(parsed_toml(manifest_file))

test/loading.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,6 @@ end
10561056
envs = [joinpath(@__DIR__, "project", "Extensions", "EnvWithHasExtensionsv2"), joinpath(@__DIR__, "project", "Extensions", "EnvWithHasExtensions")]
10571057
cmd = addenv(```$(Base.julia_cmd()) --startup-file=no -e '
10581058
begin
1059-
1060-
10611059
push!(empty!(DEPOT_PATH), '$(repr(depot_path))')
10621060
using HasExtensions
10631061
using ExtDep
@@ -1067,6 +1065,22 @@ end
10671065
'
10681066
```, "JULIA_LOAD_PATH" => join(envs, sep))
10691067
@test success(cmd)
1068+
1069+
test_ext_proj = """
1070+
begin
1071+
using HasExtensions
1072+
using ExtDep
1073+
Base.get_extension(HasExtensions, :Extension) isa Module || error("expected extension to load")
1074+
using ExtDep2
1075+
Base.get_extension(HasExtensions, :ExtensionFolder) isa Module || error("expected extension to load")
1076+
end
1077+
"""
1078+
for compile in (`--compiled-modules=no`, ``)
1079+
cmd_proj_ext = `$(Base.julia_cmd()) $compile --startup-file=no -e $test_ext_proj`
1080+
proj = joinpath(@__DIR__, "project", "Extensions")
1081+
cmd_proj_ext = addenv(cmd_proj_ext, "JULIA_LOAD_PATH" => join([joinpath(proj, "HasExtensions.jl"), joinpath(proj, "EnvWithDeps")], sep))
1082+
run(cmd_proj_ext)
1083+
end
10701084
finally
10711085
try
10721086
rm(depot_path, force=true, recursive=true)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
3+
julia_version = "1.9.0-rc3"
4+
manifest_format = "2.0"
5+
project_hash = "ec25ff8df3a5e2212a173c3de2c7d716cc47cd36"
6+
7+
[[deps.ExtDep]]
8+
deps = ["SomePackage"]
9+
path = "../ExtDep.jl"
10+
uuid = "fa069be4-f60b-4d4c-8b95-f8008775090c"
11+
version = "0.1.0"
12+
13+
[[deps.ExtDep2]]
14+
path = "../ExtDep2"
15+
uuid = "55982ee5-2ad5-4c40-8cfe-5e9e1b01500d"
16+
version = "0.1.0"
17+
18+
[[deps.SomePackage]]
19+
path = "../SomePackage"
20+
uuid = "678608ae-7bb3-42c7-98b1-82102067a3d8"
21+
version = "0.1.0"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[deps]
2+
ExtDep = "fa069be4-f60b-4d4c-8b95-f8008775090c"
3+
ExtDep2 = "55982ee5-2ad5-4c40-8cfe-5e9e1b01500d"
4+
SomePackage = "678608ae-7bb3-42c7-98b1-82102067a3d8"

0 commit comments

Comments
 (0)