Skip to content

Commit 7eb5d44

Browse files
Merge pull request #26580 from JuliaLang/sk/loadfix
loading: fix two bugs in project file deps parsing + tests
2 parents 17e9abf + 1eb266b commit 7eb5d44

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

base/loading.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,8 @@ end
479479

480480
# find project file root or deps `name => uuid` mapping
481481
# - `false` means: did not find `name`
482-
# - `true` means: found `name` without UUID
482+
# - `true` means: found `name` without UUID (can't happen in explicit projects)
483483
# - `uuid` means: found `name` with `uuid` in project file
484-
#
485-
# `true` can only be returned in the case that `name` is the project's name
486-
# and the project file does not have a top-level `uuid` mapping
487-
# it is not currently supported to have a name in `deps` mapped to anything
488-
# besides a UUID, so otherwise the answer is `false` or a UUID value
489484

490485
function explicit_project_deps_get(project_file::String, name::String)::Union{Bool,UUID}
491486
open(project_file) do io
@@ -507,10 +502,10 @@ function explicit_project_deps_get(project_file::String, name::String)::Union{Bo
507502
m.captures[1] == name && return UUID(m.captures[2])
508503
end
509504
elseif occursin(re_section, line)
510-
state = :deps
505+
state = occursin(re_section_deps, line) ? :deps : :other
511506
end
512507
end
513-
return false
508+
return root_name == name && root_uuid
514509
end
515510
end
516511

stdlib/Pkg3/src/precompile.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,6 @@ precompile(Tuple{typeof(REPL.LineEdit.fixup_keymaps!), Base.Dict{Char, Any}, Int
781781
precompile(Tuple{typeof(REPL.LineEdit.region_active), REPL.LineEdit.PromptState})
782782
precompile(Tuple{typeof(REPL.LineEdit.setup_prefix_keymap), REPL.REPLHistoryProvider, REPL.LineEdit.Prompt})
783783
precompile(Tuple{typeof(REPL.LineEdit.setup_search_keymap), REPL.REPLHistoryProvider})
784-
precompile(Tuple{getfield(Pkg3.Types, Symbol("##printpkgstyle#56")), Bool, typeof(Pkg3.Types.printpkgstyle), Base.TTY, Symbol, String})
785784

786785
precompile(Tuple{typeof(REPL.LineEdit.complete_line), Pkg3.REPLMode.PkgCompletionProvider, REPL.LineEdit.PromptState})
787786

test/loading.jl

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,69 @@ mktempdir() do dir
7979
end
8080
end
8181

82+
## unit tests of project parsing ##
83+
8284
import Base: SHA1, PkgId, load_path, identify_package, locate_package, version_slug, dummy_uuid
8385
import UUIDs: UUID, uuid4, uuid_version
8486
import Random: shuffle, randstring
8587
using Test
8688

89+
function subset(v::Vector{T}, m::Int) where T
90+
T[v[j] for j = 1:length(v) if ((m >>> (j - 1)) & 1) == 1]
91+
end
92+
93+
function perm(p::Vector, i::Int)
94+
for j = length(p):-1:1
95+
i, k = divrem(i, j)
96+
p[j], p[k+1] = p[k+1], p[j]
97+
end
98+
return p
99+
end
100+
101+
@testset "explicit_project_deps_get" begin
102+
project_file = "$(tempname()).toml"
103+
touch(project_file) # dummy_uuid calls realpath
104+
proj_uuid = dummy_uuid(project_file)
105+
root_uuid = UUID("43306aae-ef21-43f3-9517-81724f2885ac")
106+
this_uuid = UUID("b36283d3-af40-4a18-9ee0-d12ee9c142ac")
107+
lines = split("""
108+
name = "Root"
109+
uuid = "$root_uuid"
110+
[deps]
111+
This = "$this_uuid"
112+
""", '\n')
113+
N = length(lines)
114+
for m = 0:2^N-1
115+
# for every subset of lines
116+
s = subset(lines, m)
117+
for i = 1:factorial(count_ones(m))
118+
# for every permutation of that subset
119+
p = perm(s, i)
120+
open(project_file, write=true) do io
121+
for line in p
122+
println(io, line)
123+
end
124+
end
125+
# look at lines and their order
126+
n = findfirst(line -> startswith(line, "name"), p)
127+
u = findfirst(line -> startswith(line, "uuid"), p)
128+
d = findfirst(line -> line == "[deps]", p)
129+
t = findfirst(line -> startswith(line, "This"), p)
130+
# look up various packages by name
131+
root = Base.explicit_project_deps_get(project_file, "Root")
132+
this = Base.explicit_project_deps_get(project_file, "This")
133+
that = Base.explicit_project_deps_get(project_file, "That")
134+
# test that the correct answers are given
135+
@test root == (coalesce(n, N+1) coalesce(d, N+1) ? false :
136+
coalesce(u, N+1) < coalesce(d, N+1) ? root_uuid : proj_uuid)
137+
@test this == (coalesce(d, N+1) < coalesce(t, N+1) N ? this_uuid : false)
138+
@test that == false
139+
end
140+
end
141+
end
142+
143+
## functional testing of package identification, location & loading ##
144+
87145
saved_load_path = copy(LOAD_PATH)
88146
saved_depot_path = copy(DEPOT_PATH)
89147
push!(empty!(LOAD_PATH), "project")
@@ -415,14 +473,14 @@ function test_find(
415473
end
416474
end
417475

418-
@testset "identify_package with one env in load path" begin
476+
@testset "find_package with one env in load path" begin
419477
for (env, (_, _, roots, graph, paths)) in envs
420478
push!(empty!(LOAD_PATH), env)
421479
test_find(roots, graph, paths)
422480
end
423481
end
424482

425-
@testset "identify_package with two envs in load path" begin
483+
@testset "find_package with two envs in load path" begin
426484
for x = false:true,
427485
(env1, (_, _, roots1, graph1, paths1)) in (x ? envs : rand(envs, 10)),
428486
(env2, (_, _, roots2, graph2, paths2)) in (x ? rand(envs, 10) : envs)
@@ -434,7 +492,7 @@ end
434492
end
435493
end
436494

437-
@testset "identify_package with three envs in load path" begin
495+
@testset "find_package with three envs in load path" begin
438496
for (env1, (_, _, roots1, graph1, paths1)) in rand(envs, 10),
439497
(env2, (_, _, roots2, graph2, paths2)) in rand(envs, 10),
440498
(env3, (_, _, roots3, graph3, paths3)) in rand(envs, 10)

0 commit comments

Comments
 (0)