Skip to content

Commit 98098f2

Browse files
KristofferCKristofferC
andauthored
only try download artifacts from pkg server if the corresponding package is in a registry from it (#4297)
Co-authored-by: KristofferC <[email protected]>
1 parent 4197dd7 commit 98098f2

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

src/Artifacts.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ Ensures an artifact is installed, downloading it via the download information st
434434
function ensure_artifact_installed(name::String, artifacts_toml::String;
435435
platform::AbstractPlatform = HostPlatform(),
436436
pkg_uuid::Union{Base.UUID,Nothing}=nothing,
437+
pkg_server_eligible::Bool=true,
437438
verbose::Bool = false,
438439
quiet_download::Bool = false,
439440
progress::Union{Function,Nothing} = nothing,
@@ -444,23 +445,23 @@ function ensure_artifact_installed(name::String, artifacts_toml::String;
444445
end
445446

446447
return ensure_artifact_installed(name, meta, artifacts_toml;
447-
platform, verbose, quiet_download, progress, io)
448+
pkg_server_eligible, platform, verbose, quiet_download, progress, io)
448449
end
449450

450451
function ensure_artifact_installed(name::String, meta::Dict, artifacts_toml::String;
452+
pkg_server_eligible::Bool=true,
451453
platform::AbstractPlatform = HostPlatform(),
452454
verbose::Bool = false,
453455
quiet_download::Bool = false,
454456
progress::Union{Function,Nothing} = nothing,
455457
io::IO=stderr_f())
456-
457458
hash = SHA1(meta["git-tree-sha1"])
458459
if !artifact_exists(hash)
459460
if isnothing(progress) || verbose == true
460-
return try_artifact_download_sources(name, hash, meta, artifacts_toml; platform, verbose, quiet_download, io)
461+
return try_artifact_download_sources(name, hash, meta, artifacts_toml; pkg_server_eligible, platform, verbose, quiet_download, io)
461462
else
462463
# if a custom progress handler is given it is taken to mean the caller wants to handle the download scheduling
463-
return () -> try_artifact_download_sources(name, hash, meta, artifacts_toml; platform, quiet_download=true, io, progress)
464+
return () -> try_artifact_download_sources(name, hash, meta, artifacts_toml; pkg_server_eligible, platform, quiet_download=true, io, progress)
464465
end
465466
else
466467
return artifact_path(hash)
@@ -469,16 +470,16 @@ end
469470

470471
function try_artifact_download_sources(
471472
name::String, hash::SHA1, meta::Dict, artifacts_toml::String;
473+
pkg_server_eligible::Bool=true,
472474
platform::AbstractPlatform=HostPlatform(),
473475
verbose::Bool=false,
474476
quiet_download::Bool=false,
475477
io::IO=stderr_f(),
476478
progress::Union{Function,Nothing}=nothing)
477479

478480
errors = Any[]
479-
# first try downloading from Pkg server
480-
# TODO: only do this if Pkg server knows about this package
481-
if (server = pkg_server()) !== nothing
481+
# first try downloading from Pkg server if the Pkg server knows about this package
482+
if pkg_server_eligible && (server = pkg_server()) !== nothing
482483
url = "$server/artifact/$hash"
483484
download_success = let url = url
484485
@debug "Downloading artifact from Pkg server" name artifacts_toml platform url

src/Operations.jl

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -936,15 +936,23 @@ function download_artifacts(ctx::Context;
936936
env = ctx.env
937937
io = ctx.io
938938
fancyprint = can_fancyprint(io)
939-
pkg_roots = String[]
939+
pkg_info = Tuple{String, Union{Base.UUID, Nothing}}[]
940940
for (uuid, pkg) in env.manifest
941941
pkg = manifest_info(env.manifest, uuid)
942942
pkg_root = source_path(env.manifest_file, pkg, julia_version)
943-
pkg_root === nothing || push!(pkg_roots, pkg_root)
943+
pkg_root === nothing || push!(pkg_info, (pkg_root, uuid))
944944
end
945-
push!(pkg_roots, dirname(env.project_file))
945+
push!(pkg_info, (dirname(env.project_file), env.pkg !== nothing ? env.pkg.uuid : nothing))
946946
download_jobs = Dict{SHA1, Function}()
947947

948+
# Check what registries the current pkg server tracks
949+
# Disable if precompiling to not access internet
950+
server_registry_info = if Base.JLOptions().incremental == 0
951+
Registry.pkg_server_registry_info()
952+
else
953+
nothing
954+
end
955+
948956
print_lock = Base.ReentrantLock() # for non-fancyprint printing
949957

950958
download_states = Dict{SHA1, DownloadState}()
@@ -958,12 +966,17 @@ function download_artifacts(ctx::Context;
958966
ansi_enablecursor = "\e[?25h"
959967
ansi_disablecursor = "\e[?25l"
960968

961-
all_collected_artifacts = reduce(vcat, map(pkg_root -> collect_artifacts(pkg_root; platform, include_lazy), pkg_roots))
962-
used_artifact_tomls = Set{String}(map(first, all_collected_artifacts))
963-
longest_name_length = maximum(all_collected_artifacts; init=0) do (artifacts_toml, artifacts)
964-
maximum(textwidth, keys(artifacts); init=0)
969+
all_collected_artifacts = reduce(
970+
vcat, map(
971+
((pkg_root, pkg_uuid),) ->
972+
map(ca -> (ca[1], ca[2], pkg_uuid), collect_artifacts(pkg_root; platform, include_lazy)), pkg_info
973+
)
974+
)
975+
used_artifact_tomls = Set{String}(map(ca -> ca[1], all_collected_artifacts))
976+
longest_name_length = maximum(all_collected_artifacts; init = 0) do (artifacts_toml, artifacts, pkg_uuid)
977+
maximum(textwidth, keys(artifacts); init = 0)
965978
end
966-
for (artifacts_toml, artifacts) in all_collected_artifacts
979+
for (artifacts_toml, artifacts, pkg_uuid) in all_collected_artifacts
967980
# For each Artifacts.toml, install each artifact we've collected from it
968981
for name in keys(artifacts)
969982
local rname = rpad(name, longest_name_length)
@@ -981,9 +994,12 @@ function download_artifacts(ctx::Context;
981994
dstate.status_update_time = t
982995
end
983996
end
997+
# Check if the current package is eligible for PkgServer artifact downloads
998+
local pkg_server_eligible = pkg_uuid !== nothing && Registry.is_pkg_in_pkgserver_registry(pkg_uuid, server_registry_info, ctx.registries)
999+
9841000
# returns a string if exists, or function that downloads the artifact if not
9851001
local ret = ensure_artifact_installed(name, artifacts[name], artifacts_toml;
986-
verbose, quiet_download=!(usable_io(io)), io, progress)
1002+
pkg_server_eligible, verbose, quiet_download=!(usable_io(io)), io, progress)
9871003
if ret isa Function
9881004
download_states[hash] = dstate
9891005
download_jobs[hash] =
@@ -1175,17 +1191,10 @@ function download_source(ctx::Context, pkgs; readonly=true)
11751191
archive_urls = Pair{String,Bool}[]
11761192
# Check if the current package is available in one of the registries being tracked by the pkg server
11771193
# In that case, download from the package server
1178-
if server_registry_info !== nothing
1194+
if Registry.is_pkg_in_pkgserver_registry(pkg.uuid, server_registry_info, ctx.registries)
11791195
server, registry_info = server_registry_info
1180-
for reg in ctx.registries
1181-
if reg.uuid in keys(registry_info)
1182-
if haskey(reg, pkg.uuid)
1183-
url = "$server/package/$(pkg.uuid)/$(pkg.tree_hash)"
1184-
push!(archive_urls, url => true)
1185-
break
1186-
end
1187-
end
1188-
end
1196+
url = "$server/package/$(pkg.uuid)/$(pkg.tree_hash)"
1197+
push!(archive_urls, url => true)
11891198
end
11901199
for repo_url in urls
11911200
url = get_archive_url_for_version(repo_url, pkg.tree_hash)

src/Registry/Registry.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ end
103103

104104
pkg_server_url_hash(url::String) = Base.SHA1(split(url, '/')[end])
105105

106+
"""
107+
is_pkg_in_pkgserver_registry(pkg_uuid::Base.UUID, server_registry_info, registries)
108+
109+
Check if a package UUID is tracked by the PkgServer by verifying it exists in
110+
a registry that is known to the PkgServer.
111+
"""
112+
function is_pkg_in_pkgserver_registry(pkg_uuid::Base.UUID, server_registry_info, registries)
113+
server_registry_info === nothing && return false
114+
registries === nothing && return false
115+
116+
server, registry_info = server_registry_info
117+
for reg in registries
118+
if reg.uuid in keys(registry_info)
119+
if haskey(reg, pkg_uuid)
120+
return true
121+
end
122+
end
123+
end
124+
return false
125+
end
126+
106127
function download_default_registries(io::IO; only_if_empty::Bool = true, depots::Union{String, Vector{String}}=depots())
107128
# Check the specified depots for installed registries
108129
installed_registries = reachable_registries(; depots)

0 commit comments

Comments
 (0)