-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
Operations.jl
2626 lines (2420 loc) · 108 KB
/
Operations.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
module Operations
using UUIDs
using Random: randstring
import LibGit2, Dates, TOML
import REPL
using REPL.TerminalMenus
using ..Types, ..Resolve, ..PlatformEngines, ..GitTools, ..MiniProgressBars
import ..depots, ..depots1, ..devdir, ..set_readonly, ..Types.PackageEntry
import ..Artifacts: ensure_artifact_installed, artifact_names, extract_all_hashes,
artifact_exists, select_downloadable_artifacts
using Base.BinaryPlatforms
import ...Pkg
import ...Pkg: pkg_server, Registry, pathrepr, can_fancyprint, printpkgstyle, stderr_f, OFFLINE_MODE
import ...Pkg: UPDATED_REGISTRY_THIS_SESSION, RESPECT_SYSIMAGE_VERSIONS, should_autoprecompile
#########
# Utils #
#########
function default_preserve()
if Base.get_bool_env("JULIA_PKG_PRESERVE_TIERED_INSTALLED", false)
PRESERVE_TIERED_INSTALLED
else
PRESERVE_TIERED
end
end
function find_installed(name::String, uuid::UUID, sha1::SHA1)
slug_default = Base.version_slug(uuid, sha1)
# 4 used to be the default so look there first
for slug in (slug_default, Base.version_slug(uuid, sha1, 4))
for depot in depots()
path = abspath(depot, "packages", name, slug)
ispath(path) && return path
end
end
return abspath(depots1(), "packages", name, slug_default)
end
# more accurate name is `should_be_tracking_registered_version`
# the only way to know for sure is to key into the registries
tracking_registered_version(pkg::Union{PackageSpec, PackageEntry}, julia_version=VERSION) =
!is_stdlib(pkg.uuid, julia_version) && pkg.path === nothing && pkg.repo.source === nothing
function source_path(manifest_file::String, pkg::Union{PackageSpec, PackageEntry}, julia_version = VERSION)
pkg.tree_hash !== nothing ? find_installed(pkg.name, pkg.uuid, pkg.tree_hash) :
pkg.path !== nothing ? joinpath(dirname(manifest_file), pkg.path) :
is_or_was_stdlib(pkg.uuid, julia_version) ? Types.stdlib_path(pkg.name) :
nothing
end
#TODO rename
function load_version(version, fixed, preserve::PreserveLevel)
if version === nothing
return VersionSpec() # some stdlibs dont have a version
elseif fixed
return version # dont change state if a package is fixed
elseif preserve == PRESERVE_ALL || preserve == PRESERVE_ALL_INSTALLED || preserve == PRESERVE_DIRECT
return something(version, VersionSpec())
elseif preserve == PRESERVE_SEMVER && version != VersionSpec()
return Types.semver_spec("$(version.major).$(version.minor).$(version.patch)")
elseif preserve == PRESERVE_NONE
return VersionSpec()
end
end
function load_direct_deps(env::EnvCache, pkgs::Vector{PackageSpec}=PackageSpec[];
preserve::PreserveLevel=PRESERVE_DIRECT)
pkgs = copy(pkgs)
for (name::String, uuid::UUID) in env.project.deps
findfirst(pkg -> pkg.uuid == uuid, pkgs) === nothing || continue # do not duplicate packages
entry = manifest_info(env.manifest, uuid)
push!(pkgs, entry === nothing ?
PackageSpec(;uuid=uuid, name=name) :
PackageSpec(;
uuid = uuid,
name = name,
path = entry.path,
repo = entry.repo,
pinned = entry.pinned,
tree_hash = entry.tree_hash, # TODO should tree_hash be changed too?
version = load_version(entry.version, isfixed(entry), preserve),
))
end
return pkgs
end
function load_manifest_deps(manifest::Manifest, pkgs::Vector{PackageSpec}=PackageSpec[];
preserve::PreserveLevel=PRESERVE_ALL)
pkgs = copy(pkgs)
for (uuid, entry) in manifest
findfirst(pkg -> pkg.uuid == uuid, pkgs) === nothing || continue # do not duplicate packages
push!(pkgs, PackageSpec(
uuid = uuid,
name = entry.name,
path = entry.path,
pinned = entry.pinned,
repo = entry.repo,
tree_hash = entry.tree_hash, # TODO should tree_hash be changed too?
version = load_version(entry.version, isfixed(entry), preserve),
))
end
return pkgs
end
function load_all_deps(env::EnvCache, pkgs::Vector{PackageSpec}=PackageSpec[];
preserve::PreserveLevel=PRESERVE_ALL)
pkgs = load_manifest_deps(env.manifest, pkgs; preserve=preserve)
return load_direct_deps(env, pkgs; preserve=preserve)
end
function is_instantiated(env::EnvCache; platform = HostPlatform())::Bool
# Load everything
pkgs = load_all_deps(env)
# If the top-level project is a package, ensure it is instantiated as well
if env.pkg !== nothing
# Top-level project may already be in the manifest (cyclic deps)
# so only add it if it isn't there
idx = findfirst(x -> x.uuid == env.pkg.uuid, pkgs)
if idx === nothing
push!(pkgs, Types.PackageSpec(name=env.pkg.name, uuid=env.pkg.uuid, version=env.pkg.version, path=dirname(env.project_file)))
end
else
# Make sure artifacts for project exist even if it is not a package
check_artifacts_downloaded(dirname(env.project_file); platform) || return false
end
# Make sure all paths/artifacts exist
return all(pkg -> is_package_downloaded(env.manifest_file, pkg; platform), pkgs)
end
function update_manifest!(env::EnvCache, pkgs::Vector{PackageSpec}, deps_map, julia_version)
manifest = env.manifest
empty!(manifest)
# if we're updating env.pkg that refers to another manifest, we want to change
# pkg.path, if present, to be relative to the manifest instead of an abspath
if env.project.manifest !== nothing && env.pkg.path !== nothing
env.pkg.path = Types.relative_project_path(env.manifest_file,
project_rel_path(env, source_path(env.manifest_file, env.pkg)))
end
if env.pkg !== nothing
pkgs = push!(copy(pkgs), env.pkg::PackageSpec)
end
for pkg in pkgs
entry = PackageEntry(;name = pkg.name, version = pkg.version, pinned = pkg.pinned,
tree_hash = pkg.tree_hash, path = pkg.path, repo = pkg.repo, uuid=pkg.uuid)
if is_stdlib(pkg.uuid, julia_version)
# Only set stdlib versions for versioned (external) stdlibs
entry.version = stdlib_version(pkg.uuid, julia_version)
end
if Types.is_project(env, pkg)
entry.deps = env.project.deps
else
entry.deps = deps_map[pkg.uuid]
end
env.manifest[pkg.uuid] = entry
end
prune_manifest(env)
record_project_hash(env)
end
# This has to be done after the packages have been downloaded
# since we need access to the Project file to read the information
# about extensions
function fixup_ext!(env, pkgs)
for pkg in pkgs
v = joinpath(source_path(env.manifest_file, pkg), "Project.toml")
if haskey(env.manifest, pkg.uuid)
entry = env.manifest[pkg.uuid]
if isfile(v)
p = Types.read_project(v)
entry.weakdeps = p.weakdeps
entry.exts = p.exts
for (name, _) in p.weakdeps
if !haskey(p.deps, name)
delete!(entry.deps, name)
end
end
end
end
end
end
####################
# Registry Loading #
####################
function load_tree_hash!(registries::Vector{Registry.RegistryInstance}, pkg::PackageSpec, julia_version)
tracking_registered_version(pkg, julia_version) || return pkg
hash = nothing
for reg in registries
reg_pkg = get(reg, pkg.uuid, nothing)
reg_pkg === nothing && continue
pkg_info = Registry.registry_info(reg_pkg)
version_info = get(pkg_info.version_info, pkg.version, nothing)
version_info === nothing && continue
hash′ = version_info.git_tree_sha1
if hash !== nothing
hash == hash′ || pkgerror("hash mismatch in registries for $(pkg.name) at version $(pkg.version)")
end
hash = hash′
end
pkg.tree_hash = hash
return pkg
end
#######################################
# Dependency gathering and resolution #
#######################################
get_compat(proj::Project, name::String) = haskey(proj.compat, name) ? proj.compat[name].val : Types.VersionSpec()
get_compat_str(proj::Project, name::String) = haskey(proj.compat, name) ? proj.compat[name].str : nothing
function set_compat(proj::Project, name::String, compat::String)
semverspec = Types.semver_spec(compat, throw = false)
isnothing(semverspec) && return false
proj.compat[name] = Types.Compat(semverspec, compat)
return true
end
function set_compat(proj::Project, name::String, ::Nothing)
delete!(proj.compat, name)
return true
end
function reset_all_compat!(proj::Project)
for name in keys(proj.compat)
compat = proj.compat[name]
if compat.val != Types.semver_spec(compat.str)
proj.compat[name] = Types.Compat(Types.semver_spec(compat.str), compat.str)
end
end
return nothing
end
function collect_project(pkg::PackageSpec, path::String)
deps = PackageSpec[]
weakdeps = Set{UUID}()
project_file = projectfile_path(path; strict=true)
if project_file === nothing
pkgerror("could not find project file for package $(err_rep(pkg)) at `$path`")
end
project = read_package(project_file)
julia_compat = get_compat(project, "julia")
if !isnothing(julia_compat) && !(VERSION in julia_compat)
pkgerror("julia version requirement from Project.toml's compat section not satisfied for package $(err_rep(pkg)) at `$path`")
end
for (name, uuid) in project.deps
vspec = get_compat(project, name)
push!(deps, PackageSpec(name, uuid, vspec))
end
for (name, uuid) in project.weakdeps
vspec = get_compat(project, name)
push!(deps, PackageSpec(name, uuid, vspec))
push!(weakdeps, uuid)
end
if project.version !== nothing
pkg.version = project.version
else
# @warn("project file for $(pkg.name) is missing a `version` entry")
pkg.version = VersionNumber(0)
end
return deps, weakdeps
end
is_tracking_path(pkg) = pkg.path !== nothing
is_tracking_repo(pkg) = pkg.repo.source !== nothing
is_tracking_registry(pkg) = !is_tracking_path(pkg) && !is_tracking_repo(pkg)
isfixed(pkg) = !is_tracking_registry(pkg) || pkg.pinned
function collect_developed!(env::EnvCache, pkg::PackageSpec, developed::Vector{PackageSpec})
source = project_rel_path(env, source_path(env.manifest_file, pkg))
source_env = EnvCache(projectfile_path(source))
pkgs = load_all_deps(source_env)
for pkg in filter(is_tracking_path, pkgs)
if any(x -> x.uuid == pkg.uuid, developed)
continue
end
# normalize path
pkg.path = Types.relative_project_path(env.manifest_file,
project_rel_path(source_env,
source_path(source_env.manifest_file, pkg)))
push!(developed, pkg)
collect_developed!(env, pkg, developed)
end
end
function collect_developed(env::EnvCache, pkgs::Vector{PackageSpec})
developed = PackageSpec[]
for pkg in filter(is_tracking_path, pkgs)
collect_developed!(env, pkg, developed)
end
return developed
end
function collect_fixed!(env::EnvCache, pkgs::Vector{PackageSpec}, names::Dict{UUID, String})
deps_map = Dict{UUID,Vector{PackageSpec}}()
weak_map = Dict{UUID,Set{UUID}}()
if env.pkg !== nothing
pkg = env.pkg
deps, weakdeps = collect_project(pkg, dirname(env.project_file))
deps_map[pkg.uuid] = deps
weak_map[pkg.uuid] = weakdeps
names[pkg.uuid] = pkg.name
end
for pkg in pkgs
path = project_rel_path(env, source_path(env.manifest_file, pkg))
if !isdir(path)
pkgerror("expected package $(err_rep(pkg)) to exist at path `$path`")
end
deps, weakdeps = collect_project(pkg, path)
deps_map[pkg.uuid] = deps
weak_map[pkg.uuid] = weakdeps
end
fixed = Dict{UUID,Resolve.Fixed}()
# Collect the dependencies for the fixed packages
for (uuid, deps) in deps_map
q = Dict{UUID, VersionSpec}()
for dep in deps
names[dep.uuid] = dep.name
q[dep.uuid] = dep.version
end
if Types.is_project_uuid(env, uuid)
fix_pkg = env.pkg
else
idx = findfirst(pkg -> pkg.uuid == uuid, pkgs)
fix_pkg = pkgs[idx]
end
fixed[uuid] = Resolve.Fixed(fix_pkg.version, q, weak_map[uuid])
end
return fixed
end
# drops build detail in version but keeps the main prerelease context
# i.e. dropbuild(v"2.0.1-rc1.21321") == v"2.0.1-rc1"
dropbuild(v::VersionNumber) = VersionNumber(v.major, v.minor, v.patch, isempty(v.prerelease) ? () : (v.prerelease[1],))
# Resolve a set of versions given package version specs
# looks at uuid, version, repo/path,
# sets version to a VersionNumber
# adds any other packages which may be in the dependency graph
# all versioned packages should have a `tree_hash`
function resolve_versions!(env::EnvCache, registries::Vector{Registry.RegistryInstance}, pkgs::Vector{PackageSpec}, julia_version,
installed_only::Bool)
installed_only = installed_only || OFFLINE_MODE[]
# compatibility
if julia_version !== nothing
# only set the manifest julia_version if ctx.julia_version is not nothing
env.manifest.julia_version = dropbuild(VERSION)
v = intersect(julia_version, get_compat(env.project, "julia"))
if isempty(v)
@warn "julia version requirement for project not satisfied" _module=nothing _file=nothing
end
end
jll_fix = Dict{UUID, VersionNumber}()
for pkg in pkgs
if !is_stdlib(pkg.uuid) && endswith(pkg.name, "_jll") && pkg.version isa VersionNumber
jll_fix[pkg.uuid] = pkg.version
end
end
names = Dict{UUID, String}(uuid => name for (uuid, (name, version)) in stdlibs())
# recursive search for packages which are tracking a path
developed = collect_developed(env, pkgs)
# But we only want to use information for those packages that we don't know about
for pkg in developed
if !any(x -> x.uuid == pkg.uuid, pkgs)
push!(pkgs, pkg)
end
end
# this also sets pkg.version for fixed packages
fixed = collect_fixed!(env, filter(!is_tracking_registry, pkgs), names)
# non fixed packages are `add`ed by version: their version is either restricted or free
# fixed packages are `dev`ed or `add`ed by repo
# at this point, fixed packages have a version and `deps`
@assert length(Set(pkg.uuid::UUID for pkg in pkgs)) == length(pkgs)
# check compat
for pkg in pkgs
compat = get_compat(env.project, pkg.name)
v = intersect(pkg.version, compat)
if isempty(v)
throw(Resolve.ResolverError(
"empty intersection between $(pkg.name)@$(pkg.version) and project compatibility $(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
for pkg in pkgs
names[pkg.uuid] = pkg.name
end
# Unless using the unbounded or historical resolver, always allow stdlibs to update. Helps if the previous resolve
# happened on a different julia version / commit and the stdlib version in the manifest is not the current stdlib version
unbind_stdlibs = julia_version === VERSION
reqs = Resolve.Requires(pkg.uuid => is_stdlib(pkg.uuid) && unbind_stdlibs ? VersionSpec("*") : VersionSpec(pkg.version) for pkg in pkgs)
graph, compat_map = deps_graph(env, registries, names, reqs, fixed, julia_version, installed_only)
Resolve.simplify_graph!(graph)
vers = Resolve.resolve(graph)
# Fixup jlls that got their build numbers stripped
vers_fix = copy(vers)
for (uuid, vers) in vers
old_v = get(jll_fix, uuid, nothing)
# We only fixup a JLL if the old major/minor/patch matches the new major/minor/patch
if old_v !== nothing && Base.thispatch(old_v) == Base.thispatch(vers_fix[uuid])
vers_fix[uuid] = old_v
end
end
vers = vers_fix
# update vector of package versions
for (uuid, ver) in vers
idx = findfirst(p -> p.uuid == uuid, pkgs)
if idx !== nothing
pkg = pkgs[idx]
# Fixed packages are not returned by resolve (they already have their version set)
pkg.version = vers[pkg.uuid]
else
name = is_stdlib(uuid) ? first(stdlibs()[uuid]) : registered_name(registries, uuid)
push!(pkgs, PackageSpec(;name=name, uuid=uuid, version=ver))
end
end
final_deps_map = Dict{UUID, Dict{String, UUID}}()
for pkg in pkgs
load_tree_hash!(registries, pkg, julia_version)
deps = begin
if pkg.uuid in keys(fixed)
deps_fixed = Dict{String, UUID}()
for dep in keys(fixed[pkg.uuid].requires)
deps_fixed[names[dep]] = dep
end
deps_fixed
else
d = Dict{String, UUID}()
for (uuid, _) in compat_map[pkg.uuid][pkg.version]
d[names[uuid]] = uuid
end
d
end
end
# julia is an implicit dependency
filter!(d -> d.first != "julia", deps)
final_deps_map[pkg.uuid] = deps
end
return final_deps_map
end
get_or_make!(d::Dict{K,V}, k::K) where {K,V} = get!(d, k) do; V() end
const JULIA_UUID = UUID("1222c4b2-2114-5bfd-aeef-88e4692bbb3e")
const PKGORIGIN_HAVE_VERSION = :version in fieldnames(Base.PkgOrigin)
function deps_graph(env::EnvCache, registries::Vector{Registry.RegistryInstance}, uuid_to_name::Dict{UUID,String},
reqs::Resolve.Requires, fixed::Dict{UUID,Resolve.Fixed}, julia_version,
installed_only::Bool)
uuids = Set{UUID}()
union!(uuids, keys(reqs))
union!(uuids, keys(fixed))
for fixed_uuids in map(fx->keys(fx.requires), values(fixed))
union!(uuids, fixed_uuids)
end
stdlibs_for_julia_version = Types.get_last_stdlibs(julia_version)
seen = Set{UUID}()
# pkg -> version -> (dependency => compat):
all_compat = Dict{UUID,Dict{VersionNumber,Dict{UUID,VersionSpec}}}()
weak_compat = Dict{UUID,Dict{VersionNumber,Set{UUID}}}()
for (fp, fx) in fixed
all_compat[fp] = Dict(fx.version => Dict{UUID,VersionSpec}())
end
while true
unseen = setdiff(uuids, seen)
isempty(unseen) && break
for uuid in unseen
push!(seen, uuid)
uuid in keys(fixed) && continue
all_compat_u = get_or_make!(all_compat, uuid)
weak_compat_u = get_or_make!(weak_compat, uuid)
uuid_is_stdlib = false
stdlib_name = ""
stdlib_version = nothing
if haskey(stdlibs_for_julia_version, uuid)
uuid_is_stdlib = true
stdlib_name, stdlib_version = stdlibs_for_julia_version[uuid]
end
# If we're requesting resolution of a package that is an
# unregistered stdlib we must special-case it here. This is further
# complicated by the fact that we can ask this question relative to
# a Julia version.
if (julia_version != VERSION && is_unregistered_stdlib(uuid)) || uuid_is_stdlib
path = Types.stdlib_path(stdlibs_for_julia_version[uuid][1])
proj_file = projectfile_path(path; strict=true)
@assert proj_file !== nothing
proj = read_package(proj_file)
v = something(proj.version, VERSION)
# TODO look at compat section for stdlibs?
all_compat_u_vr = get_or_make!(all_compat_u, v)
for (_, other_uuid) in proj.deps
push!(uuids, other_uuid)
all_compat_u_vr[other_uuid] = VersionSpec()
end
if !isempty(proj.weakdeps)
weak_all_compat_u_vr = get_or_make!(weak_compat_u, v)
for (_, other_uuid) in proj.weakdeps
push!(uuids, other_uuid)
all_compat_u_vr[other_uuid] = VersionSpec()
push!(weak_all_compat_u_vr, other_uuid)
end
end
else
for reg in registries
pkg = get(reg, uuid, nothing)
pkg === nothing && continue
info = Registry.registry_info(pkg)
function add_compat!(d, cinfo)
for (v, compat_info) in cinfo
# Filter yanked and if we are in offline mode also downloaded packages
# TODO, pull this into a function
Registry.isyanked(info, v) && continue
if installed_only
pkg_spec = PackageSpec(name=pkg.name, uuid=pkg.uuid, version=v, tree_hash=Registry.treehash(info, v))
is_package_downloaded(env.manifest_file, pkg_spec) || continue
end
# Skip package version that are not the same as external packages in sysimage
if PKGORIGIN_HAVE_VERSION && RESPECT_SYSIMAGE_VERSIONS[] && julia_version == VERSION
pkgid = Base.PkgId(uuid, pkg.name)
if Base.in_sysimage(pkgid)
pkgorigin = get(Base.pkgorigins, pkgid, nothing)
if pkgorigin !== nothing && pkgorigin.version !== nothing
if v != pkgorigin.version
continue
end
end
end
end
dv = get_or_make!(d, v)
merge!(dv, compat_info)
union!(uuids, keys(compat_info))
end
end
add_compat!(all_compat_u, Registry.compat_info(info))
weak_compat_info = Registry.weak_compat_info(info)
if weak_compat_info !== nothing
add_compat!(all_compat_u, weak_compat_info)
# Version to Set
for (v, compat_info) in weak_compat_info
weak_compat_u[v] = keys(compat_info)
end
end
end
end
end
end
for uuid in uuids
uuid == JULIA_UUID && continue
if !haskey(uuid_to_name, uuid)
name = registered_name(registries, uuid)
name === nothing && pkgerror("cannot find name corresponding to UUID $(uuid) in a registry")
uuid_to_name[uuid] = name
entry = manifest_info(env.manifest, uuid)
entry ≡ nothing && continue
uuid_to_name[uuid] = entry.name
end
end
return Resolve.Graph(all_compat, weak_compat, uuid_to_name, reqs, fixed, false, julia_version),
all_compat
end
########################
# Package installation #
########################
function get_archive_url_for_version(url::String, ref)
if (m = match(r"https://github.com/(.*?)/(.*?).git", url)) !== nothing
return "https://api.github.com/repos/$(m.captures[1])/$(m.captures[2])/tarball/$(ref)"
end
return nothing
end
# Returns if archive successfully installed
function install_archive(
urls::Vector{Pair{String,Bool}},
hash::SHA1,
version_path::String;
io::IO=stderr_f()
)::Bool
tmp_objects = String[]
url_success = false
for (url, top) in urls
path = tempname() * randstring(6)
push!(tmp_objects, path) # for cleanup
url_success = true
try
PlatformEngines.download(url, path; verbose=false, io=io)
catch e
e isa InterruptException && rethrow()
url_success = false
end
url_success || continue
dir = joinpath(tempdir(), randstring(12))
push!(tmp_objects, dir) # for cleanup
# Might fail to extract an archive (https://github.com/JuliaPackaging/PkgServer.jl/issues/126)
try
unpack(path, dir; verbose=false)
catch e
e isa InterruptException && rethrow()
@warn "failed to extract archive downloaded from $(url)"
url_success = false
end
url_success || continue
if top
unpacked = dir
else
dirs = readdir(dir)
# 7z on Win might create this spurious file
filter!(x -> x != "pax_global_header", dirs)
@assert length(dirs) == 1
unpacked = joinpath(dir, dirs[1])
end
# Assert that the tarball unpacked to the tree sha we wanted
# TODO: Enable on Windows when tree_hash handles
# executable bits correctly, see JuliaLang/julia #33212.
if !Sys.iswindows()
if SHA1(GitTools.tree_hash(unpacked)) != hash
@warn "tarball content does not match git-tree-sha1"
url_success = false
end
url_success || continue
end
# Move content to version path
!isdir(version_path) && mkpath(version_path)
mv(unpacked, version_path; force=true)
break # successful install
end
# Clean up and exit
foreach(x -> Base.rm(x; force=true, recursive=true), tmp_objects)
return url_success
end
const refspecs = ["+refs/*:refs/remotes/cache/*"]
function install_git(
io::IO,
uuid::UUID,
name::String,
hash::SHA1,
urls::Set{String},
version_path::String
)::Nothing
repo = nothing
tree = nothing
# TODO: Consolidate this with some of the repo handling in Types.jl
try
clones_dir = joinpath(depots1(), "clones")
ispath(clones_dir) || mkpath(clones_dir)
repo_path = joinpath(clones_dir, string(uuid))
repo = GitTools.ensure_clone(io, repo_path, first(urls); isbare=true,
header = "[$uuid] $name from $(first(urls))")
git_hash = LibGit2.GitHash(hash.bytes)
for url in urls
try LibGit2.with(LibGit2.GitObject, repo, git_hash) do g
end
break # object was found, we can stop
catch err
err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow()
end
GitTools.fetch(io, repo, url, refspecs=refspecs)
end
tree = try
LibGit2.GitObject(repo, git_hash)
catch err
err isa LibGit2.GitError && err.code == LibGit2.Error.ENOTFOUND || rethrow()
error("$name: git object $(string(hash)) could not be found")
end
tree isa LibGit2.GitTree ||
error("$name: git object $(string(hash)) should be a tree, not $(typeof(tree))")
mkpath(version_path)
GitTools.checkout_tree_to_path(repo, tree, version_path)
return
finally
repo !== nothing && LibGit2.close(repo)
tree !== nothing && LibGit2.close(tree)
end
end
function collect_artifacts(pkg_root::String; platform::AbstractPlatform=HostPlatform())
# Check to see if this package has an (Julia)Artifacts.toml
artifacts_tomls = Tuple{String,Base.TOML.TOMLDict}[]
for f in artifact_names
artifacts_toml = joinpath(pkg_root, f)
if isfile(artifacts_toml)
selector_path = joinpath(pkg_root, ".pkg", "select_artifacts.jl")
# If there is a dynamic artifact selector, run that in an appropriate sandbox to select artifacts
if isfile(selector_path)
# Despite the fact that we inherit the project, since the in-memory manifest
# has not been updated yet, if we try to load any dependencies, it may fail.
# Therefore, this project inheritance is really only for Preferences, not dependencies.
select_cmd = Cmd(`$(gen_build_code(selector_path; inherit_project=true)) -t1 --startup-file=no $(triplet(platform))`)
meta_toml = String(read(select_cmd))
res = TOML.tryparse(meta_toml)
if res isa TOML.ParserError
errstr = sprint(showerror, res; context=stderr)
pkgerror("failed to parse TOML output from running $(repr(selector_path)), got: \n$errstr")
else
push!(artifacts_tomls, (artifacts_toml, TOML.parse(meta_toml)))
end
else
# Otherwise, use the standard selector from `Artifacts`
artifacts = select_downloadable_artifacts(artifacts_toml; platform)
push!(artifacts_tomls, (artifacts_toml, artifacts))
end
break
end
end
return artifacts_tomls
end
function download_artifacts(env::EnvCache;
platform::AbstractPlatform=HostPlatform(),
julia_version = VERSION,
verbose::Bool=false,
io::IO=stderr_f())
pkg_roots = String[]
for (uuid, pkg) in env.manifest
pkg = manifest_info(env.manifest, uuid)
pkg_root = source_path(env.manifest_file, pkg, julia_version)
pkg_root === nothing || push!(pkg_roots, pkg_root)
end
push!(pkg_roots, dirname(env.project_file))
for pkg_root in pkg_roots
for (artifacts_toml, artifacts) in collect_artifacts(pkg_root; platform)
# For each Artifacts.toml, install each artifact we've collected from it
for name in keys(artifacts)
ensure_artifact_installed(name, artifacts[name], artifacts_toml;
verbose, quiet_download=!(io isa Base.TTY), io=io)
end
write_env_usage(artifacts_toml, "artifact_usage.toml")
end
end
end
function check_artifacts_downloaded(pkg_root::String; platform::AbstractPlatform=HostPlatform())
for (artifacts_toml, artifacts) in collect_artifacts(pkg_root; platform)
for name in keys(artifacts)
if !artifact_exists(Base.SHA1(artifacts[name]["git-tree-sha1"]))
return false
end
break
end
end
return true
end
function find_urls(registries::Vector{Registry.RegistryInstance}, uuid::UUID)
urls = Set{String}()
for reg in registries
reg_pkg = get(reg, uuid, nothing)
reg_pkg === nothing && continue
info = Registry.registry_info(reg_pkg)
repo = info.repo
repo === nothing && continue
push!(urls, repo)
end
return urls
end
function download_source(ctx::Context; readonly=true)
pkgs_to_install = NamedTuple{(:pkg, :urls, :path), Tuple{PackageEntry, Set{String}, String}}[]
for pkg in values(ctx.env.manifest)
tracking_registered_version(pkg, ctx.julia_version) || continue
path = source_path(ctx.env.manifest_file, pkg, ctx.julia_version)
path === nothing && continue
ispath(path) && continue
urls = find_urls(ctx.registries, pkg.uuid)
push!(pkgs_to_install, (;pkg, urls, path))
end
length(pkgs_to_install) == 0 && return Set{UUID}()
########################################
# Install from archives asynchronously #
########################################
missed_packages = eltype(pkgs_to_install)[]
widths = [textwidth(pkg.name) for (pkg, _) in pkgs_to_install]
max_name = maximum(widths; init=0)
# Check what registries the current pkg server tracks
# Disable if precompiling to not access internet
server_registry_info = if Base.JLOptions().incremental == 0
Registry.pkg_server_registry_info()
else
nothing
end
@sync begin
jobs = Channel{eltype(pkgs_to_install)}(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, urls, path) in jobs
if ctx.use_git_for_all_downloads
put!(results, (pkg, false, (urls, path)))
continue
end
try
archive_urls = Pair{String,Bool}[]
# Check if the current package is available in one of the registries being tracked by the pkg server
# In that case, download from the package server
if server_registry_info !== nothing
server, registry_info = server_registry_info
for reg in ctx.registries
if reg.uuid in keys(registry_info)
if haskey(reg, pkg.uuid)
url = "$server/package/$(pkg.uuid)/$(pkg.tree_hash)"
push!(archive_urls, url => true)
break
end
end
end
end
for repo_url in urls
url = get_archive_url_for_version(repo_url, pkg.tree_hash)
url !== nothing && push!(archive_urls, url => false)
end
success = install_archive(archive_urls, pkg.tree_hash, path, io=ctx.io)
if success && readonly
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)")
end
put!(results, (pkg, success, (urls, path)))
catch err
put!(results, (pkg, err, catch_backtrace()))
end
end
end
end
bar = MiniProgressBar(; indent=2, header = "Progress", color = Base.info_color(),
percentage=false, always_reprint=true)
bar.max = length(pkgs_to_install)
fancyprint = can_fancyprint(ctx.io)
try
for i in 1:length(pkgs_to_install)
pkg::PackageEntry, exc_or_success, bt_or_pathurls = take!(results)
exc_or_success isa Exception && pkgerror("Error when installing package $(pkg.name):\n",
sprint(Base.showerror, exc_or_success, bt_or_pathurls))
success, (urls, path) = exc_or_success, bt_or_pathurls
success || push!(missed_packages, (; pkg, urls, path))
bar.current = i
str = sprint(; context=ctx.io) do io
if success
fancyprint && print_progress_bottom(io)
vstr = if pkg.version !== nothing
"v$(pkg.version)"
else
short_treehash = string(pkg.tree_hash)[1:16]
"[$short_treehash]"
end
printpkgstyle(io, :Installed, string(rpad(pkg.name * " ", max_name + 2, "─"), " ", vstr))
fancyprint && show_progress(io, bar)
end
end
print(ctx.io, str)
end
finally
fancyprint && end_progress(ctx.io, bar)
close(jobs)
end
end
##################################################
# Use LibGit2 to download any remaining packages #
##################################################
for (pkg, urls, path) in missed_packages
uuid = pkg.uuid
install_git(ctx.io, pkg.uuid, pkg.name, pkg.tree_hash, urls, path)
readonly && set_readonly(path)
vstr = if pkg.version !== nothing
"v$(pkg.version)"
else
short_treehash = string(pkg.tree_hash)[1:16]
"[$short_treehash]"
end
printpkgstyle(ctx.io, :Installed, string(rpad(pkg.name * " ", max_name + 2, "─"), " ", vstr))
end
return Set{UUID}(entry.pkg.uuid for entry in pkgs_to_install)
end
################################
# Manifest update and pruning #
################################
project_rel_path(env::EnvCache, path::String) = normpath(joinpath(dirname(env.manifest_file), path))
function prune_manifest(env::EnvCache)
# if project uses another manifest, only prune project entry in manifest
if dirname(env.project_file) != dirname(env.manifest_file)
proj_entry = env.manifest[env.project.uuid]
proj_entry.deps = env.project.deps
else
keep = collect(values(env.project.deps))
env.manifest = prune_manifest(env.manifest, keep)
end
return env.manifest
end
function prune_manifest(manifest::Manifest, keep::Vector{UUID})
while !isempty(keep)
clean = true
for (uuid, entry) in manifest
uuid in keep || continue
for dep in values(entry.deps)
dep in keep && continue
push!(keep, dep)
clean = false
end
end
clean && break
end
manifest.deps = Dict(uuid => entry for (uuid, entry) in manifest if uuid in keep)
return manifest
end
function record_project_hash(env::EnvCache)
env.manifest.other["project_hash"] = Types.project_resolve_hash(env.project)
end
#########
# Build #
#########
get_deps(env::EnvCache, new_uuids::Set{UUID}) = _get_deps!(Set{UUID}(), env, new_uuids)
function _get_deps!(collected_uuids::Set{UUID}, env::EnvCache, new_uuids)
for uuid in new_uuids
is_stdlib(uuid) && continue
uuid in collected_uuids && continue
push!(collected_uuids, uuid)
children_uuids = if Types.is_project_uuid(env, uuid)
Set(values(env.project.deps))
else
info = manifest_info(env.manifest, uuid)
if info === nothing
pkgerror("could not find manifest entry for package with uuid $(uuid)")
end
Set(values(info.deps))
end
_get_deps!(collected_uuids, env, children_uuids)
end
return collected_uuids
end
# TODO: This function should be replaceable with `is_instantiated` but
# see https://github.com/JuliaLang/Pkg.jl/issues/2470
function any_package_not_installed(manifest::Manifest)
for (uuid, entry) in manifest
if Base.locate_package(Base.PkgId(uuid, entry.name)) === nothing
return true
end
end
return false
end
function build(ctx::Context, uuids::Set{UUID}, verbose::Bool)
if any_package_not_installed(ctx.env.manifest) || !isfile(ctx.env.manifest_file)
Pkg.instantiate(ctx, allow_build = false, allow_autoprecomp = false)
end
all_uuids = get_deps(ctx.env, uuids)
build_versions(ctx, all_uuids; verbose)
end
function dependency_order_uuids(env::EnvCache, uuids::Vector{UUID})::Dict{UUID,Int}