Skip to content

Commit

Permalink
Add bazel mirror as a backup URL
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorcloudy committed Sep 5, 2023
1 parent 00dd74b commit 26cae5c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
9 changes: 5 additions & 4 deletions distdir.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,22 @@ def _repo_cache_tar_impl(ctx):
archive_files = []
readme_content = "This directory contains repository cache artifacts for the following URLs:\n\n"
for artifact in http_artifacts:
urls = artifact["urls"]
if "integrity" in artifact:
# ./tempfile could be a hard link if --experimental_repository_cache_hardlinks is used,
# therefore we must delete it before creating or writing it again.
ctx.delete("./tempfile")
checksum = ctx.download(artifact["url"], "./tempfile", executable = False, integrity = artifact["integrity"])
checksum = ctx.download(urls, "./tempfile", executable = False, integrity = artifact["integrity"])
artifact["sha256"] = checksum.sha256

if "sha256" in artifact:
sha256 = artifact["sha256"]
output_file = "content_addressable/sha256/%s/file" % sha256
ctx.download(artifact["url"], output_file, sha256, executable = False)
ctx.download(urls, output_file, sha256, executable = False)
archive_files.append(output_file)
readme_content += "- %s (SHA256: %s)\n" % (artifact["url"], sha256)
readme_content += "- %s (SHA256: %s)\n" % (urls[0], sha256)
else:
fail("Could not find integrity or sha256 hash for artifact %s" % artifact["url"])
fail("Could not find integrity or sha256 hash for artifact %s" % urls[0])

ctx.file("WORKSPACE", "")
ctx.file("README.md", readme_content)
Expand Down
2 changes: 1 addition & 1 deletion extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _test_repo_extension_impl(ctx):
# Define one http_file for each artifact so that we can fetch them in parallel.
http_file(
name = name + str(cnt),
url = artifact["url"],
urls = artifact["urls"],
sha256 = artifact["sha256"] if "sha256" in artifact else None,
integrity = artifact["integrity"] if "integrity" in artifact else None,
)
Expand Down
10 changes: 7 additions & 3 deletions src/tools/bzlmod/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def get_canonical_repo_name(apparent_repo_name):
apparent_repo_name = "@" + apparent_repo_name
return Label(apparent_repo_name).workspace_name

def with_bazel_mirror(url):
"""Adds the bazel mirror URL as a backup."""
return [url, "https://mirror.bazel.build/" + url.split("://")[1]]

def extract_url(attributes):
"""Extracts the url from the given attributes."""
if "urls" in attributes:
Expand Down Expand Up @@ -59,13 +63,13 @@ def extract_http_artifacts(ctx, lockfile_path, required_repos):

http_artifacts.append({
"integrity": attributes["integrity"][2:],
"url": extract_url(attributes),
"urls": with_bazel_mirror(extract_url(attributes)),
})
if "remote_patches" in attributes:
for patch, integrity in attributes["remote_patches"].items():
http_artifacts.append({
"integrity": integrity[2:],
"url": patch[2:],
"urls": with_bazel_mirror(patch[2:]),
})

for _, extension in lockfile["moduleExtensions"].items():
Expand All @@ -81,7 +85,7 @@ def extract_http_artifacts(ctx, lockfile_path, required_repos):

http_artifacts.append({
"sha256": attributes["sha256"][2:],
"url": extract_url(attributes),
"urls": with_bazel_mirror(extract_url(attributes)),
})

missing_repos = [repo for repo in required_repos if repo not in found_repos]
Expand Down

0 comments on commit 26cae5c

Please sign in to comment.