Skip to content

Commit 4cce534

Browse files
fix(uv): use astral urls for uv primary source, github as secondary (#3746)
For official releases, use the astral.sh mirror as the preferred url for binary downloads, with github.com as a fallback. For uv >= 0.11.0, read the checksums directly from the dist-manifest contents, so the only github.com SPOF is fetching the dist-manifest itself. --- It is not infrequent for `github.com` to give a 5xx error on one of the uv sha256 downloads, leading to a failed build. (The sha256 downloads cannot be cached by Bazel's repository cache, since we request them without a checksum.) See RobotLocomotion/drake#24140 for some examples, as well as bazelbuild/bazel-central-registry#8591 (comment) ([log](https://buildkite.com/bazel/bcr-presubmit/builds/33406/canvas?jid=019dd214-ce96-4eeb-b6e8-a720932ed816&tab=output)). This change limits github as a single point of failure. --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
1 parent 098cb29 commit 4cce534

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ END_UNRELEASED_TEMPLATE
7171
files ([#3126](https://github.com/bazel-contrib/rules_python/issues/3126)).
7272
* (pypi) Support `--experimental_isolated_extension_usages`
7373
([#3668](https://github.com/bazel-contrib/rules_python/issues/3668)).
74+
* (uv) use the astral.sh mirror as the preferred url for binary downloads,
75+
with github.com as a fallback; for uv >= 0.11.0, read the checksums directly
76+
from the dist-manifest contents.
7477

7578
{#v0-0-0-added}
7679
### Added

python/uv/private/uv.bzl

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ def _overlap(first_collection, second_collection):
380380

381381
return False
382382

383+
# See https://github.com/astral-sh/setup-uv/pull/809.
384+
GITHUB_RELEASES_PREFIX = "https://github.com/astral-sh/uv/releases/download/"
385+
ASTRAL_MIRROR_PREFIX = "https://releases.astral.sh/github/uv/releases/download/"
386+
383387
def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename, platforms, get_auth = get_auth, **auth_attrs):
384388
"""Download the results about remote tool sources.
385389
@@ -452,6 +456,8 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename
452456
}
453457
]
454458
checksum "uv-aarch64-apple-darwin.tar.gz.sha256"
459+
checksums
460+
sha256 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
455461
uv-aarch64-apple-darwin.tar.gz.sha256
456462
name "uv-aarch64-apple-darwin.tar.gz.sha256"
457463
kind "checksum"
@@ -471,29 +477,55 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename
471477
fail(result)
472478
dist_manifest = json.decode(module_ctx.read(dist_manifest))
473479

474-
base_url = (
480+
# Use the simple download_url from the manifest, when available.
481+
dist_base_url = (
475482
dist_manifest
476483
.get("releases", [{}])[0]
477484
.get("hosting", {})
478485
.get("simple", {})
479486
.get("download_url", base_url)
480487
)
481488

489+
# For official releases, add the astral mirror to improve availability.
490+
# See https://github.com/astral-sh/setup-uv/pull/809.
491+
if dist_base_url.startswith(GITHUB_RELEASES_PREFIX):
492+
astral_base_url = ASTRAL_MIRROR_PREFIX + dist_base_url[len(GITHUB_RELEASES_PREFIX):]
493+
base_urls = [
494+
astral_base_url,
495+
dist_base_url,
496+
]
497+
else:
498+
base_urls = [dist_base_url]
499+
482500
artifacts = dist_manifest["artifacts"]
483501
tool_sources = {}
484502
downloads = {}
485503
for fname, artifact in artifacts.items():
486504
if artifact.get("kind") != "executable-zip":
487505
continue
506+
target_triples = artifact["target_triples"]
507+
if not _overlap(target_triples, platforms):
508+
# We are not interested in this platform, so skip.
509+
continue
488510

489-
checksum = artifacts[artifact["checksum"]]
490-
if not _overlap(checksum["target_triples"], platforms):
491-
# we are not interested in this platform, so skip
511+
# Releases of uv >= 0.11.0 have the sha256 directly inline.
512+
sha256 = artifact.get("checksums", {}).get("sha256", "")
513+
if len(sha256) > 0:
514+
for platform in target_triples:
515+
tool_sources[platform] = struct(
516+
urls = [
517+
"{}/{}".format(base, fname)
518+
for base in base_urls
519+
],
520+
sha256 = sha256,
521+
)
492522
continue
493523

524+
# For uv < 0.11.0, we'll need to fetch the individual sha256 files.
525+
checksum = artifacts[artifact["checksum"]]
494526
checksum_fname = checksum["name"]
495527
checksum_path = module_ctx.path(checksum_fname)
496-
urls = ["{}/{}".format(base_url, checksum_fname)]
528+
urls = ["{}/{}".format(dist_base_url, checksum_fname)]
497529
downloads[checksum_path] = struct(
498530
download = module_ctx.download(
499531
url = urls,
@@ -502,7 +534,7 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename
502534
auth = get_auth(module_ctx, urls, ctx_attr = auth_attr),
503535
),
504536
archive_fname = fname,
505-
platforms = checksum["target_triples"],
537+
platforms = target_triples,
506538
)
507539

508540
for checksum_path, download in downloads.items():
@@ -522,7 +554,10 @@ def _get_tool_urls_from_dist_manifest(module_ctx, *, base_url, manifest_filename
522554

523555
for platform in download.platforms:
524556
tool_sources[platform] = struct(
525-
urls = ["{}/{}".format(base_url, archive_fname)],
557+
urls = [
558+
"{}/{}".format(base, archive_fname)
559+
for base in base_urls
560+
],
526561
sha256 = sha256,
527562
)
528563

tests/uv/uv/uv_tests.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def _uv_mock_mctx(*modules, download = None):
3434
x: {
3535
"checksum": x + ".sha256",
3636
"kind": "executable-zip",
37+
"target_triples": [x],
3738
}
3839
for x in ["linux", "osx"]
3940
} | {
@@ -47,6 +48,7 @@ def _uv_mock_mctx(*modules, download = None):
4748
x: {
4849
"checksum": x + ".sha256",
4950
"kind": "executable-zip",
51+
"target_triples": [x],
5052
}
5153
for x in ["linux", "os", "osx", "something_extra"]
5254
} | {

0 commit comments

Comments
 (0)