Skip to content

Commit da115be

Browse files
committed
rename submodule -> subpackage; allow loading submodule without parent
1 parent 1e1a8ac commit da115be

File tree

15 files changed

+138
-76
lines changed

15 files changed

+138
-76
lines changed

base/exports.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ export
864864
evalfile,
865865
include_string,
866866
include_dependency,
867-
@submodule_using,
867+
@subpackage_using,
868868

869869
# RTS internals
870870
GC,

base/loading.jl

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,15 +1029,15 @@ function explicit_manifest_deps_get(project_file::String, where::PkgId, name::St
10291029
return identify_package(PkgId(UUID(uuid), dep_name), name)
10301030
end
10311031
end
1032-
# Check for submodules
1033-
submodules = get(entry, "submodules", nothing)
1034-
if submodules !== nothing
1035-
if haskey(submodules, where.name) && where.uuid == uuid5(UUID(uuid), where.name)
1032+
# Check for subpackages
1033+
subpackages = get(entry, "subpackages", nothing)
1034+
if subpackages !== nothing
1035+
if haskey(subpackages, where.name) && where.uuid == uuid5(UUID(uuid), where.name)
10361036
found_where = true
10371037
if name == dep_name
10381038
return PkgId(UUID(uuid), name)
10391039
end
1040-
subs = submodules[where.name]::Union{String, Vector{String}}
1040+
subs = subpackages[where.name]::Union{String, Vector{String}}
10411041
weakdeps = get(entry, "weakdeps", nothing)::Union{Vector{String}, Dict{String, Any}, Nothing}
10421042
if (subs isa String && name == subs) || (subs isa Vector{String} && name in subs)
10431043
for deps′ in [weakdeps, deps]
@@ -1057,7 +1057,7 @@ function explicit_manifest_deps_get(project_file::String, where::PkgId, name::St
10571057
end
10581058
end
10591059
end
1060-
# `name` is not a submodule, do standard lookup as if this was the parent
1060+
# `name` is not a subpackage, do standard lookup as if this was the parent
10611061
return identify_package(PkgId(UUID(uuid), dep_name), name)
10621062
end
10631063
end
@@ -1110,10 +1110,10 @@ function explicit_manifest_uuid_path(project_file::String, pkg::PkgId)::Union{No
11101110
return find_ext_path(p, pkg.name)
11111111
end
11121112
end
1113-
# Submodules
1114-
let submodules = get(entry, "submodules", nothing)::Union{Nothing, Dict{String, Any}}
1115-
if submodules !== nothing
1116-
if haskey(submodules, pkg.name) && uuid !== nothing && uuid5(UUID(uuid), pkg.name) == pkg.uuid
1113+
# Subpackages
1114+
let subpackages = get(entry, "subpackages", nothing)::Union{Nothing, Dict{String, Any}}
1115+
if subpackages !== nothing
1116+
if haskey(subpackages, pkg.name) && uuid !== nothing && uuid5(UUID(uuid), pkg.name) == pkg.uuid
11171117
parent_path = locate_package(PkgId(UUID(uuid), name))
11181118
if parent_path === nothing
11191119
error("failed to find source of parent package: \"$name\"")
@@ -1722,34 +1722,61 @@ end
17221722
# End extensions
17231723

17241724

1725-
##############
1726-
# Submodules #
1727-
##############
1728-
function load_submodule(mod::Module, subname::Union{Symbol, String})
1725+
###############
1726+
# Subpackages #
1727+
###############
1728+
function load_subpackage(into::Module, parentname::Symbol, subname::Union{Symbol, String})
1729+
str_parentname = string(parentname)
1730+
str_subname = string(subname)
1731+
parent_uuid = @lock require_lock begin
1732+
uuidkey_env = identify_package_env(into, String(parentname))
1733+
if uuidkey_env === nothing
1734+
where = PkgId(into)
1735+
if where.uuid === nothing
1736+
throw(ArgumentError("Package $parentname not found in current path."))
1737+
else
1738+
manifest_warnings = collect_manifest_warnings()
1739+
throw(ArgumentError("""
1740+
Package $(where.name) does not have $parentname in its dependencies:
1741+
$manifest_warnings- You may have a partially installed environment. Try `Pkg.instantiate()`
1742+
to ensure all packages in the environment are installed.
1743+
- Or, if you have $(where.name) checked out for development and have
1744+
added $parentname as a dependency but haven't updated your primary
1745+
environment's manifest file, try `Pkg.resolve()`.
1746+
- Otherwise you may need to report an issue with $(where.name)"""))
1747+
end
1748+
end
1749+
uuidkey, env = uuidkey_env
1750+
uuidkey.uuid
1751+
end
1752+
sub_id = PkgId(uuid5(parent_uuid, str_subname), str_subname)
1753+
__require(sub_id)
1754+
end
1755+
function load_subpackage(mod::Module, subname::Union{Symbol, String})
17291756
parent_id = PkgId(mod)
17301757
str_subname = string(subname)
1731-
id = PkgId(uuid5(parent_id.uuid, str_subname), str_subname)
1732-
__require(id)
1758+
sub_id = PkgId(uuid5(parent_id.uuid, str_subname), str_subname)
1759+
__require(sub_id)
17331760
end
17341761

17351762
"""
1736-
@submodule_using Parent.Sub
1763+
@subpackage_using Parent.Sub
17371764
1738-
Load the `Sub` submodule from the `Parent` package.
1765+
Load the `Sub` subpackage from the `Parent` package.
17391766
"""
1740-
macro submodule_using(parent_sub)
1767+
macro subpackage_using(parent_sub)
17411768
if isexpr(parent_sub, :(.), 2)
17421769
parent, sub = parent_sub.args
17431770
@gensym mod
17441771
esc(quote
1745-
$mod = $load_submodule($parent, $sub)
1772+
$mod = $load_subpackage($__module__, $(QuoteNode(parent)), $sub)
17461773
using .$mod
17471774
end)
17481775
else
1749-
throw(ArgumentError("Malformed expression, `@submodule_using` only takes expressions of the form `Parent.Submodule`"))
1776+
throw(ArgumentError("Malformed expression, `@subpackage_using` only takes expressions of the form `Parent.Subpackage`"))
17501777
end
17511778
end
1752-
# End submodules
1779+
# End subpackages
17531780

17541781

17551782
struct CacheFlags

test/loading.jl

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,35 +1264,44 @@ end
12641264
end
12651265
end
12661266

1267-
@testset "Submodules" begin
1267+
@testset "Subpackages" begin
12681268
old_depot_path = copy(DEPOT_PATH)
12691269
try
12701270
tmp = mktempdir()
12711271
push!(empty!(DEPOT_PATH), joinpath(tmp, "depot"))
1272-
proj = joinpath(@__DIR__, "project", "Submodules", "HasDepWithSubmodules")
1272+
proj = joinpath(@__DIR__, "project", "Subpackages", "HasDepWithSubpackages")
12731273
for i in 1:2 # Once when requiring precomilation, once where it is already precompiled
12741274
cmd = `$(Base.julia_cmd()) --project=$proj --startup-file=no -e '
12751275
begin
1276-
using HasSubmodules
1277-
HasSubmodules.sub1_loaded && error("sub1_loaded set")
1278-
using HasDepWithSubmodules
1279-
HasSubmodules.sub1_loaded || error("sub1_loaded not set")
1280-
HasDepWithSubmodules.g(1) == 1 || error("g failed")
1276+
using HasSubpackages
1277+
HasSubpackages.sub1_loaded && error("sub1_loaded set")
1278+
using HasDepWithSubpackages
1279+
HasSubpackages.sub1_loaded || error("sub1_loaded not set")
1280+
HasDepWithSubpackages.g(1) == 1 || error("g failed")
12811281
end
12821282
'`
12831283
@test success(cmd)
12841284
end
12851285
let cmd = `$(Base.julia_cmd()) --project=$proj --startup-file=no -e '
12861286
begin
1287-
using HasSubmodules
1287+
using HasSubpackages
12881288
using ADep
1289-
HasSubmodules.sub2_loaded && error("sub2_loaded set!")
1290-
@submodule_using HasSubmodules.Submodule2 # currently broken
1291-
HasSubmodules.sub2_loaded && error("sub2_loaded set!")
1289+
HasSubpackages.sub2_loaded && error("sub2_loaded set!")
1290+
@submodule_using HasSubpackages.Subpackage2 # currently broken
1291+
HasSubpackages.sub2_loaded && error("sub2_loaded set!")
12921292
end
12931293
'`
12941294
@test_broken success(cmd)
12951295
end
1296+
let cmd = `$(Base.julia_cmd()) --project=$proj --startup-file=no -e '
1297+
begin
1298+
@subpackage_using HasSubpackages.Subpackage3
1299+
Subpackage3.x == 1 || error("Something went wrong with HasSubpackages.Subpackage3")
1300+
"HasSubpackages" ∉ string.(values(Base.loaded_modules)) || error("HasSubpackages was loaded when it was not needed!")
1301+
end
1302+
'`
1303+
@test success(cmd)
1304+
end
12961305
finally
12971306
copy!(DEPOT_PATH, old_depot_path)
12981307
end

test/project/Submodules/HasDepWithSubmodules/Project.toml

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/project/Submodules/HasDepWithSubmodules/src/HasDepWithSubmodules.jl

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/project/Submodules/HasSubmodules/sub/Submodule1.jl

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "ADep"
2+
uuid = "81016d05-d0fa-4c22-8125-5c67d6cf9f2f"
3+
authors = ["Mason Protter <mason.protter@icloud.com>"]
4+
version = "0.1.0"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module ADep
2+
3+
foo(x) = x^2
4+
5+
export foo
6+
7+
end # module ADep

test/project/Submodules/HasDepWithSubmodules/Manifest.toml renamed to test/project/Subpackages/HasDepWithSubpackages/Manifest.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ julia_version = "1.13.0-DEV"
22
manifest_format = "2.0"
33
project_hash = "cd51eae9e59902fd893210e4f8a4d4dc2ac81722"
44

5-
[[deps.HasDepWithSubmodules]]
6-
deps = ["HasSubmodules"]
5+
[[deps.HasDepWithSubpackages]]
6+
deps = ["HasSubpackages"]
77
path = "."
88
uuid = "0237f425-1828-4bb9-8eeb-a4f4cdb55107"
99
version = "0.1.0"
1010

11-
[[deps.HasSubmodules]]
12-
path = "../HasSubmodules"
11+
[[deps.HasSubpackages]]
12+
path = "../HasSubpackages"
1313
uuid = "0f595f7f-271a-49d7-b31d-0f5ff0a9189a"
1414
version = "0.1.0"
1515

16-
[deps.HasSubmodules.submodules]
17-
Submodule1 = ""
18-
Submodule2 = "ADep"
16+
[deps.HasSubpackages.subpackages]
17+
Subpackage1 = "HasSubpackages"
18+
Subpackage2 = "ADep"
19+
Subpackage3 = ""
1920

2021
[[deps.ADep]]
2122
path = "../ADep"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name = "HasDepWithSubpackages"
2+
uuid = "0237f425-1828-4bb9-8eeb-a4f4cdb55107"
3+
version = "0.1.0"
4+
5+
[deps]
6+
HasSubpackages = "0f595f7f-271a-49d7-b31d-0f5ff0a9189a"
7+
ADep = "81016d05-d0fa-4c22-8125-5c67d6cf9f2f"

0 commit comments

Comments
 (0)