-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathbackwards_compatible_isolation.jl
609 lines (565 loc) · 22.8 KB
/
backwards_compatible_isolation.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
function _update_manifest(ctx::Context, pkg::PackageSpec, hash::Union{SHA1, Nothing})
env = ctx.env
uuid, name, version, path, special_action, repo = pkg.uuid, pkg.name, pkg.version, pkg.path, pkg.special_action, pkg.repo
hash === nothing && @assert (path !== nothing || pkg.uuid in keys(ctx.stdlibs) || pkg.repo.source !== nothing)
# TODO I think ^ assertion is wrong, add-repo should have a hash
entry = get!(env.manifest, uuid, Types.PackageEntry())
entry.name = name
is_stdlib = uuid in keys(ctx.stdlibs)
if !is_stdlib
entry.version = version
entry.tree_hash = hash
entry.path = path
if special_action == PKGSPEC_DEVELOPED
entry.pinned = false
entry.repo.source = nothing
entry.repo.rev = nothing
elseif special_action == PKGSPEC_FREED
if entry.pinned
entry.pinned = false
else
entry.repo.source = nothing
entry.repo.rev = nothing
end
elseif special_action == PKGSPEC_PINNED
entry.pinned = true
elseif special_action == PKGSPEC_REPO_ADDED
entry.repo.source = repo.source
entry.repo.rev = repo.rev
path = find_installed(name, uuid, hash)
end
if entry.repo.source !== nothing
path = find_installed(name, uuid, hash)
end
end
empty!(entry.deps)
if path !== nothing || is_stdlib
if is_stdlib
path = Types.stdlib_path(name)
else
path = joinpath(dirname(ctx.env.project_file), path)
end
deps = Dict{String,UUID}()
# Check for deps in project file
project_file = projectfile_path(path; strict=true)
project_file === nothing && pkgerror("could not find project file for $(pkg.name)")
project = read_project(project_file)
entry.deps = project.deps
else
for path in registered_paths(ctx, uuid)
data = load_package_data(UUID, joinpath(path, "Deps.toml"), version)
if data !== nothing
entry.deps = data
break
end
end
end
return
end
# Resolve a set of versions given package version specs
function _resolve_versions!(
ctx::Context,
pkgs::Vector{PackageSpec},
target::Union{Nothing, String} = nothing,
)::Dict{UUID,VersionNumber}
printpkgstyle(ctx, :Resolving, "package versions...")
# anything not mentioned is fixed
uuids = UUID[pkg.uuid for pkg in pkgs]
uuid_to_name = Dict{UUID, String}(uuid => stdlib for (uuid, stdlib) in ctx.stdlibs)
for (name::String, uuid::UUID) in get_deps(ctx.env, target)
uuid_to_name[uuid] = name
uuid_idx = findfirst(isequal(uuid), uuids)
entry = manifest_info(ctx, uuid)
if entry !== nothing && entry.version !== nothing # stdlibs might not have a version
ver = VersionSpec(entry.version)
else
ver = VersionSpec()
end
if uuid_idx !== nothing
pkg = pkgs[uuid_idx]
if entry !== nothing && pkg.special_action != PKGSPEC_FREED && entry.pinned
# This is a pinned package, fix its version
pkg.version = ver
end
else
push!(pkgs, PackageSpec(name, uuid, ver))
end
end
# construct data structures for resolver and call it
# this also sets pkg.version for fixed packages
fixed = _collect_fixed!(ctx, pkgs, uuid_to_name)
# compatibility
proj_compat = Types.project_compatibility(ctx, "julia")
v = intersect(VERSION, proj_compat)
if isempty(v)
@warn "julia version requirement for project not satisfied" _module=nothing _file=nothing
end
for pkg in pkgs
proj_compat = Types.project_compatibility(ctx, pkg.name)
v = intersect(pkg.version, proj_compat)
if isempty(v)
pkgerror(string("empty intersection between $(pkg.name)@$(pkg.version) and project ",
"compatibility $(proj_compat)"))
end
# Work around not clobbering 0.x.y+ for checked out old type of packages
if !(pkg.version isa VersionNumber)
pkg.version = v
end
end
reqs = Resolve.Requires(pkg.uuid => VersionSpec(pkg.version) for pkg in pkgs)
graph = deps_graph(ctx, uuid_to_name, reqs, fixed)
Resolve.simplify_graph!(graph)
vers = resolve(graph)
find_registered!(ctx, collect(keys(vers)))
# update vector of package versions
for pkg in pkgs
# Fixed packages are not returned by resolve (they already have their version set)
haskey(vers, pkg.uuid) && (pkg.version = vers[pkg.uuid])
end
uuids = UUID[pkg.uuid for pkg in pkgs]
for (uuid, ver) in vers
uuid in uuids && continue
name = (uuid in keys(ctx.stdlibs)) ? ctx.stdlibs[uuid] : registered_name(ctx, uuid)
push!(pkgs, PackageSpec(;name=name, uuid=uuid, version=ver))
end
return vers
end
# This also sets the .path field for fixed packages in `pkgs`
function _collect_fixed!(ctx::Context, pkgs::Vector{PackageSpec}, uuid_to_name::Dict{UUID, String})
fixed_pkgs = PackageSpec[]
fix_deps_map = Dict{UUID,Vector{PackageSpec}}()
uuid_to_pkg = Dict{UUID,PackageSpec}()
for pkg in pkgs
local path
entry = manifest_info(ctx, pkg.uuid)
if pkg.special_action == PKGSPEC_FREED && !entry.pinned
continue
elseif pkg.special_action == PKGSPEC_DEVELOPED
@assert pkg.path !== nothing
path = pkg.path
elseif pkg.special_action == PKGSPEC_REPO_ADDED
@assert pkg.tree_hash !== nothing
path = find_installed(pkg.name, pkg.uuid, pkg.tree_hash)
elseif entry !== nothing && entry.path !== nothing
path = pkg.path = entry.path
elseif entry !== nothing && entry.repo.source !== nothing
path = find_installed(pkg.name, pkg.uuid, entry.tree_hash)
pkg.repo = entry.repo
pkg.tree_hash = entry.tree_hash
else
continue
end
path = project_rel_path(ctx, path)
if !isdir(path)
pkgerror("path $(path) for package $(pkg.name) no longer exists. Remove the package or `develop` it at a new path")
end
uuid_to_pkg[pkg.uuid] = pkg
uuid_to_name[pkg.uuid] = pkg.name
collect_project!(ctx, pkg, path, fix_deps_map)
end
fixed = Dict{UUID,Resolve.Fixed}()
# Collect the dependencies for the fixed packages
for (uuid, fixed_pkgs) in fix_deps_map
fix_pkg = uuid_to_pkg[uuid]
v = Dict{VersionNumber,Dict{UUID,VersionSpec}}()
q = Dict{UUID, VersionSpec}()
for deppkg in fixed_pkgs
uuid_to_name[deppkg.uuid] = deppkg.name
q[deppkg.uuid] = deppkg.version
end
fixed[uuid] = Resolve.Fixed(fix_pkg.version, q)
end
return fixed
end
# install & update manifest
function apply_versions(ctx::Context, pkgs::Vector{PackageSpec}; mode=:add, update=true)::Vector{UUID}
hashes, urls = _version_data!(ctx, pkgs)
apply_versions(ctx, pkgs, hashes, urls; mode=mode, update=update)
end
function apply_versions(ctx::Context, pkgs::Vector{PackageSpec}, hashes::Dict{UUID,SHA1}, urls::Dict{UUID,Vector{String}}; mode=:add, update=true)
probe_platform_engines!()
new_versions = UUID[]
pkgs_to_install = Tuple{PackageSpec, String}[]
for pkg in pkgs
!is_stdlib(ctx, pkg.uuid) || continue
pkg.path === nothing || continue
pkg.repo.source === nothing || continue
path = find_installed(pkg.name, pkg.uuid, hashes[pkg.uuid])
if !ispath(path)
push!(pkgs_to_install, (pkg, path))
push!(new_versions, pkg.uuid)
end
end
widths = [textwidth(pkg.name) for (pkg, _) in pkgs_to_install]
max_name = length(widths) == 0 ? 0 : maximum(widths)
########################################
# Install from archives asynchronously #
########################################
jobs = Channel(ctx.num_concurrent_downloads);
results = Channel(ctx.num_concurrent_downloads);
@async begin
for pkg in pkgs_to_install
put!(jobs, pkg)
end
end
for i in 1:ctx.num_concurrent_downloads
@async begin
for (pkg, path) in jobs
if ctx.use_libgit2_for_all_downloads
put!(results, (pkg, false, path))
continue
end
try
success = install_archive(urls[pkg.uuid], hashes[pkg.uuid], path)
if success && mode === :add
set_readonly(path) # In add mode, files should be read-only
end
if ctx.use_only_tarballs_for_downloads && !success
pkgerror("failed to get tarball from $(urls[pkg.uuid])")
end
put!(results, (pkg, success, path))
catch err
put!(results, (pkg, err, catch_backtrace()))
end
end
end
end
missed_packages = Tuple{PackageSpec, String}[]
for i in 1:length(pkgs_to_install)
pkg, exc_or_success, bt_or_path = take!(results)
exc_or_success isa Exception && pkgerror("Error when installing package $(pkg.name):\n",
sprint(Base.showerror, exc_or_success, bt_or_path))
success, path = exc_or_success, bt_or_path
if success
vstr = pkg.version !== nothing ? "v$(pkg.version)" : "[$h]"
printpkgstyle(ctx, :Installed, string(rpad(pkg.name * " ", max_name + 2, "─"), " ", vstr))
else
push!(missed_packages, (pkg, path))
end
end
##################################################
# Use LibGit2 to download any remaining packages #
##################################################
for (pkg, path) in missed_packages
uuid = pkg.uuid
install_git(ctx, pkg.uuid, pkg.name, hashes[uuid], urls[uuid], pkg.version::VersionNumber, path)
if mode === :add
set_readonly(path)
end
vstr = pkg.version !== nothing ? "v$(pkg.version)" : "[$h]"
printpkgstyle(ctx, :Installed, string(rpad(pkg.name * " ", max_name + 2, "─"), " ", vstr))
end
##########################################
# Installation done, update the manifest #
##########################################
if update
for pkg in pkgs
uuid = pkg.uuid
if pkg.path !== nothing || uuid in keys(ctx.stdlibs)
hash = nothing
elseif pkg.repo.source !== nothing
hash = pkg.tree_hash
else
hash = hashes[uuid]
end
_update_manifest(ctx, pkg, hash)
end
end
prune_manifest(ctx)
return new_versions
end
function _add_or_develop(ctx::Context, pkgs::Vector{PackageSpec}; new_git = UUID[], mode=:add)
# copy added name/UUIDs into project
for pkg in pkgs
ctx.env.project.deps[pkg.name] = pkg.uuid
end
# if a package is in the project file and
# the manifest version in the specified version set
# then leave the package as is at the installed version
for (name::String, uuid::UUID) in ctx.env.project.deps
entry = manifest_info(ctx, uuid)
entry !== nothing && entry.version !== nothing || continue
for pkg in pkgs
pkg.uuid == uuid && entry.version ∈ pkg.version || continue
pkg.version = entry.version
end
end
# resolve & apply package versions
_resolve_versions!(ctx, pkgs)
new_apply = apply_versions(ctx, pkgs; mode=mode)
Display.print_env_diff(ctx)
write_env(ctx.env) # write env before building
build_versions(ctx, union(new_apply, new_git))
end
# Find repos and hashes for each package UUID & version
function _version_data!(ctx::Context, pkgs::Vector{PackageSpec})
hashes = Dict{UUID,SHA1}()
clones = Dict{UUID,Vector{String}}()
for pkg in pkgs
!is_stdlib(pkg.uuid) || continue
pkg.repo.source === nothing || continue
pkg.path === nothing || continue
uuid = pkg.uuid
ver = pkg.version::VersionNumber
clones[uuid] = String[]
for path in registered_paths(ctx, uuid)
info = parse_toml(path, "Package.toml")
repo = info["repo"]
repo in clones[uuid] || push!(clones[uuid], repo)
vers = load_versions(path; include_yanked = true)
hash = get(vers, ver, nothing)
hash !== nothing || continue
if haskey(hashes, uuid)
hash == hashes[uuid] || @warn "$uuid: hash mismatch for version $ver!"
else
hashes[uuid] = hash
end
end
@assert haskey(hashes, uuid)
end
foreach(sort!, values(clones))
return hashes, clones
end
function collect_target_deps!(
ctx::Context,
pkgs::Vector{PackageSpec},
pkg::PackageSpec,
target::String,
)
# Find the path to the package
if pkg.uuid in keys(ctx.stdlibs)
path = Types.stdlib_path(pkg.name)
elseif Types.is_project_uuid(ctx, pkg.uuid)
path = dirname(ctx.env.project_file)
else
entry = manifest_info(ctx, pkg.uuid)
path = (entry.path !== nothing) ?
project_rel_path(ctx, entry.path) :
find_installed(pkg.name, pkg.uuid, entry.tree_hash)
end
project_path = nothing
for project_name in Base.project_names
project_path_cand = joinpath(path, project_name)
if isfile(project_path_cand)
project_path = project_path_cand
break
end
end
project = nothing
if project_path !== nothing
project = read_package(project_path)
end
# Collect target deps from Project
if project !== nothing
targets = project.targets
haskey(targets, target) || return
for pkg in targets[target]
uuid = project.extras[pkg]
push!(pkgs, PackageSpec(pkg, uuid))
end
end
return
end
# When testing or building a dependency, we want that dependency to be able to load its own dependencies
# at top level. Therefore we would like to execute the build or testing of a dependency using its own Project file as
# the current environment.
function with_dependencies_loadable_at_toplevel(f, mainctx::Context, pkg::PackageSpec; might_need_to_resolve=false)
# localctx is the context for the temporary environment we run the testing / building in
localctx = deepcopy(mainctx)
localctx.currently_running_target = true
# If pkg or its dependencies are checked out, we will need to resolve
# unless we already have resolved for the current environment, which the calleer indicates
# with `might_need_to_resolve`
need_to_resolve = false
is_project = Types.is_project(localctx, pkg)
target = nothing
if pkg.special_action == PKGSPEC_TESTED
target = "test"
end
# In order to fix dependencies at their current versions we need to
# add them as explicit dependencies to the project, but we need to
# remove them after the resolve to make them not-loadable.
# See issue https://github.com/JuliaLang/Pkg.jl/issues/1144
should_be_in_project = Set{UUID}()
should_be_in_manifest = Set{UUID}()
# Only put `pkg` and its deps + target deps (recursively) in the temp project
collect_deps!(seen, pkg; depth) = begin
# See issue https://github.com/JuliaLang/Pkg.jl/issues/1144
# depth = 0 - the package itself (should be in project)
# depth = 1 - direct dependencies/and or target dependencies (should be in project)
# depth > 1 - indirect dependencies that should only be in the manifest in the end
if depth <= 1
push!(should_be_in_project, pkg.uuid)
else
push!(should_be_in_manifest, pkg.uuid)
end
pkg.uuid in seen && return
push!(seen, pkg.uuid)
entry = manifest_info(localctx, pkg.uuid)
entry === nothing && return
need_to_resolve |= (entry.path !== nothing)
localctx.env.project.deps[pkg.name] = pkg.uuid
for (name, uuid) in entry.deps
collect_deps!(seen, PackageSpec(name, uuid); depth=depth+1)
end
end
if is_project # testing the project itself
# the project might have changes made to it so need to resolve
need_to_resolve = true
# Since we will create a temp environment in another place we need to extract the project
# and put it in the Project as a normal `deps` entry and in the Manifest with a path.
foreach(k->setfield!(localctx.env.project, k, nothing), (:name, :uuid, :version))
localctx.env.pkg = nothing
localctx.env.project.deps[pkg.name] = pkg.uuid
localctx.env.manifest[pkg.uuid] = Types.PackageEntry(
name=pkg.name,
deps=get_deps(mainctx.env, target),
path=dirname(localctx.env.project_file),
version=pkg.version,
)
else
# Only put `pkg` and its deps (recursively) in the temp project
empty!(localctx.env.project.deps)
localctx.env.project.deps[pkg.name] = pkg.uuid
end
# Only put `pkg` and its deps (recursively) in the temp project
seen_uuids = Set{UUID}()
collect_deps!(seen_uuids, pkg; depth=0)
pkgs = PackageSpec[]
if target !== nothing
collect_target_deps!(localctx, pkgs, pkg, target)
for dpkg in pkgs
# Also put eventual deps of target deps in new manifest
collect_deps!(seen_uuids, dpkg; depth=1)
end
end
mktempdir() do tmpdir
localctx.env.project_file = joinpath(tmpdir, "Project.toml")
localctx.env.manifest_file = joinpath(tmpdir, "Manifest.toml")
function rewrite_manifests(manifest)
# Rewrite paths in Manifest since relative paths won't work here due to the temporary environment
for (uuid, entry) in manifest
if uuid in keys(localctx.stdlibs)
entry.path = Types.stdlib_path(entry.name)
end
if entry.path !== nothing
entry.path = project_rel_path(mainctx, entry.path)
end
end
end
rewrite_manifests(localctx.env.manifest)
# Add target deps to deps (https://github.com/JuliaLang/Pkg.jl/issues/427)
if !isempty(pkgs)
target_deps = deepcopy(pkgs)
_add_or_develop(localctx, pkgs)
need_to_resolve = false # add resolves
entry = manifest_info(localctx, pkg.uuid)
for deppkg in target_deps
entry.deps[deppkg.name] = deppkg.uuid
end
end
# Might have added stdlibs in `add` above
rewrite_manifests(localctx.env.manifest)
local new
will_resolve = might_need_to_resolve && need_to_resolve
if will_resolve
_resolve_versions!(localctx, pkgs)
new = apply_versions(localctx, pkgs)
else
prune_manifest(localctx)
end
# Remove deps that we added to the project just to keep their versions fixed,
# since we know all of the packages should be in the manifest this should be
# a trivial modification of the project file only.
# See issue https://github.com/JuliaLang/Pkg.jl/issues/1144
not_loadable = setdiff(should_be_in_manifest, should_be_in_project)
Pkg.API.rm(localctx, [PackageSpec(uuid = uuid) for uuid in not_loadable])
write_env(localctx.env)
will_resolve && build_versions(localctx, new)
sep = Sys.iswindows() ? ';' : ':'
withenv("JULIA_LOAD_PATH" => "@$sep$tmpdir", "JULIA_PROJECT"=>nothing) do
f(localctx)
end
end
end
function backwards_compatibility_for_test(
ctx::Context, pkg::PackageSpec, testfile::String, pkgs_errored::Vector{String},
coverage; julia_args=``, test_args=``
)
printpkgstyle(ctx, :Testing, pkg.name)
code = """
$(Base.load_path_setup_code(false))
cd($(repr(dirname(testfile))))
append!(empty!(ARGS), $(repr(test_args.exec)))
include($(repr(testfile)))
"""
cmd = ```
$(Base.julia_cmd())
--code-coverage=$(coverage ? "user" : "none")
--color=$(Base.have_color ? "yes" : "no")
--compiled-modules=$(Bool(Base.JLOptions().use_compiled_modules) ? "yes" : "no")
--check-bounds=yes
--inline=$(Bool(Base.JLOptions().can_inline) ? "yes" : "no")
--startup-file=$(Base.JLOptions().startupfile == 1 ? "yes" : "no")
--track-allocation=$(("none", "user", "all")[Base.JLOptions().malloc_log + 1])
$(julia_args)
--eval $code
```
run_test = () -> begin
try
run(cmd)
printpkgstyle(ctx, :Testing, pkg.name * " tests passed ")
catch err
push!(pkgs_errored, pkg.name)
end
end
with_dependencies_loadable_at_toplevel(ctx, pkg; might_need_to_resolve=true) do localctx
if !Types.is_project_uuid(ctx, pkg.uuid)
Display.status(localctx, mode=PKGMODE_MANIFEST)
end
flush(stdout)
run_test()
end
end
function backwards_compat_for_build(ctx::Context, pkg::PackageSpec, build_file::String, verbose::Bool,
might_need_to_resolve::Bool, max_name::Int)
log_file = splitext(build_file)[1] * ".log"
printpkgstyle(ctx, :Building,
rpad(pkg.name * " ", max_name + 1, "─") * "→ " * Types.pathrepr(log_file))
code = """
$(Base.load_path_setup_code(false))
cd($(repr(dirname(build_file))))
include($(repr(build_file)))
"""
cmd = ```
$(Base.julia_cmd()) -O0 --color=no --history-file=no
--startup-file=$(Base.JLOptions().startupfile == 1 ? "yes" : "no")
--compiled-modules=$(Bool(Base.JLOptions().use_compiled_modules) ? "yes" : "no")
--eval $code
```
run_build = () -> begin
ok = open(log_file, "w") do log
std = verbose ? ctx.io : log
success(pipeline(cmd, stdout=std, stderr=std))
end
if !ok
n_lines = isinteractive() ? 100 : 5000
# TODO: Extract last n lines more efficiently
log_lines = readlines(log_file)
log_show = join(log_lines[max(1, length(log_lines) - n_lines):end], '\n')
full_log_at, last_lines =
if length(log_lines) > n_lines
"\n\nFull log at $log_file",
", showing the last $n_lines of log"
else
"", ""
end
@error "Error building `$(pkg.name)`$last_lines: \n$log_show$full_log_at"
end
end
with_dependencies_loadable_at_toplevel(ctx, pkg;
might_need_to_resolve=might_need_to_resolve) do localctx
flush(stdout)
run_build()
end
end