Skip to content

Commit 37d88e3

Browse files
StefanKarpinskiKristofferC
authored andcommitted
move extra deps into [extras] section (#531)
1 parent 1fd3d43 commit 37d88e3

File tree

5 files changed

+75
-15
lines changed

5 files changed

+75
-15
lines changed

stdlib/Pkg/docs/src/index.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -689,13 +689,18 @@ Testing...
689689

690690
#### Test-specific dependencies
691691

692-
Sometimes one might want to use some packages only at testing time but not enforce a dependency on them when the package is used.
693-
This is possible by adding dependencies to a "test target" to the Project file. Here we add the `Test` standard library as a
694-
test-only dependency by adding the following to the Project file:
692+
Sometimes one might want to use some packages only at testing time but not
693+
enforce a dependency on them when the package is used. This is possible by
694+
adding `[extra]` dependencies and adding a a "test target" to the Project file.
695+
Here we add the `Test` standard library as a test-only dependency by adding the
696+
following to the Project file:
695697

696698
```
697-
[targets.test.deps]
699+
[extras]
698700
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
701+
702+
[targets]
703+
test = ["Test"]
699704
```
700705

701706
We can now use `Test` in the test script and we can see that it gets installed on testing:

stdlib/Pkg/src/Operations.jl

+4-8
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,10 @@ function collect_target_deps!(ctx::Context, pkgs::Vector{PackageSpec}, pkg::Pack
874874
# Collect target deps from Project
875875
if project !== nothing
876876
targets = project["targets"]
877-
haskey(targets, target) || return pkgs
878-
targets = project["targets"]
879-
target_info = targets[target]
880-
haskey(target_info, "deps") || return pkgs
881-
targets = project["targets"]
882-
deps = target_info["deps"]
883-
for (pkg, uuid) in deps
884-
push!(pkgs, PackageSpec(pkg, UUID(uuid)))
877+
haskey(targets, target) || return
878+
for pkg in targets[target]
879+
uuid = UUID(ctx.env.project["extras"][pkg])
880+
push!(pkgs, PackageSpec(pkg, uuid))
885881
end
886882
end
887883
return nothing

stdlib/Pkg/src/Types.jl

+31
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,37 @@ function Context!(ctx::Context; kwargs...)
348348
end
349349
end
350350

351+
# target === nothing : main dependencies
352+
# target === "*" : main + all extras
353+
# target === "name" : named target deps
354+
355+
function deps_names(project::Dict, target::Union{Nothing,String}=nothing)::Vector{String}
356+
deps = sort!(collect(keys(project["deps"])))
357+
target == "*" && return !haskey(project, "extras") ? deps :
358+
sort!(union!(deps, collect(keys(project["extras"]))))
359+
haskey(project, "targets") || return deps
360+
targets = project["targets"]
361+
haskey(targets, target) || return deps
362+
return sort!(union!(deps, targets[target]))
363+
end
364+
365+
function get_deps(project::Dict, target::Union{Nothing,String}=nothing)
366+
names = deps_names(project, target)
367+
deps = filter(((dep, _),) -> dep in names, project["deps"])
368+
extras = get(project, "extras", Dict{String,Any}())
369+
for name in names
370+
haskey(deps, name) && continue
371+
haskey(extras, name) ||
372+
cmderror("target `$target` has unlisted dependency `$name`")
373+
deps[name] = extras[name]
374+
end
375+
return deps
376+
end
377+
get_deps(env::EnvCache, target::Union{Nothing,String}=nothing) =
378+
get_deps(env.project, target)
379+
get_deps(ctx::Context, target::Union{Nothing,String}=nothing) =
380+
get_deps(ctx.env, target)
381+
351382
function project_compatibility(ctx::Context, name::String)
352383
v = VersionSpec()
353384
project = ctx.env.project

stdlib/Pkg/test/pkg.jl

+26
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,32 @@ temp_pkg_dir() do project_path
414414
end
415415
end
416416

417+
temp_pkg_dir() do project_path
418+
cd(project_path) do
419+
project = """
420+
[deps]
421+
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
422+
423+
[extras]
424+
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
425+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
426+
427+
[targets]
428+
test = ["Markdown", "Test"]
429+
"""
430+
write("Project.toml", project)
431+
Pkg.activate(".")
432+
@testset "resolve ignores extras" begin
433+
Pkg.resolve()
434+
@test read("Manifest.toml", String) == """
435+
[[UUIDs]]
436+
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
437+
"""
438+
end
439+
Pkg.activate()
440+
end
441+
end
442+
417443
include("repl.jl")
418444

419445
end # module

stdlib/Pkg/test/test_packages/BigProject/Project.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ uuid = "da7e1942-2519-11e8-2822-f5508ce758f0"
33
authors = ["Some One <[email protected]>"]
44
version = "0.1.0"
55

6-
[deps]
7-
8-
[targets.test.deps]
6+
[extras]
97
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
8+
Example = "7876af07-990d-54b4-ab0e-23690620f79a"
9+
10+
[targets]
11+
test = ["Example", "Test"]

0 commit comments

Comments
 (0)