Skip to content

Commit fa9681f

Browse files
committed
Fix up
1 parent 04bb04b commit fa9681f

File tree

4 files changed

+3195
-30
lines changed

4 files changed

+3195
-30
lines changed

go/private/extensions.bzl

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,27 @@ def _go_sdk_impl(ctx):
198198
host_detected_goos, host_detected_goarch = detect_host_platform(ctx)
199199
toolchains = []
200200

201-
sdks_by_version = getattr(ctx, "facts", None) or {}
202-
used_versions = {}
201+
all_sdks_by_version = {}
202+
used_sdks_by_version = {}
203+
facts = getattr(ctx, "facts", {})
204+
205+
def get_sdks_by_version_cached(version):
206+
# Avoid a download without a known digest in the SDK repo rule by fetching the SDKs filename
207+
# and digest here. When using a version of Bazel that supports module extension facts, this
208+
# info will be persisted in the lockfile, allowing for truly airgapped builds with an
209+
# up-to-date lockfile and download (formerly repository) cache.
210+
sdks = facts.get(version)
211+
if sdks == None:
212+
# Lazily fetch the information about all SDKs so that we avoid the download if the facts
213+
# already contain all the versions we care about.
214+
if not all_sdks_by_version:
215+
all_sdks_by_version.clear()
216+
all_sdks_by_version.update(fetch_sdks_by_version(ctx, allow_fail = True))
217+
sdks = all_sdks_by_version.get(version)
218+
if sdks == None:
219+
return None
220+
used_sdks_by_version[version] = sdks
221+
return sdks
203222

204223
for module in ctx.modules:
205224
# Apply wrapped toolchains first to override specific platforms from the
@@ -266,9 +285,7 @@ def _go_sdk_impl(ctx):
266285
)
267286

268287
_download_sdk(
269-
module_ctx = ctx,
270-
sdks_by_version = sdks_by_version,
271-
used_versions = used_versions,
288+
get_sdks_by_version = get_sdks_by_version_cached,
272289
name = name,
273290
goos = download_tag.goos,
274291
goarch = download_tag.goarch,
@@ -307,9 +324,7 @@ def _go_sdk_impl(ctx):
307324
)
308325

309326
_download_sdk(
310-
module_ctx = ctx,
311-
sdks_by_version = sdks_by_version,
312-
used_versions = used_versions,
327+
get_sdks_by_version = get_sdks_by_version_cached,
313328
name = default_name,
314329
goos = goos,
315330
goarch = goarch,
@@ -381,11 +396,7 @@ def _go_sdk_impl(ctx):
381396

382397
# See _download_sdk below for details on these facts.
383398
if hasattr(ctx, "facts"):
384-
kwargs["facts"] = {
385-
version: sdk_info
386-
for version, sdk_info in sdks_by_version.items()
387-
if version in used_versions
388-
}
399+
kwargs["facts"] = used_sdks_by_version
389400
return ctx.extension_metadata(**kwargs)
390401
else:
391402
return None
@@ -416,23 +427,11 @@ def _left_pad_zero(index, length):
416427
fail("index must be non-negative")
417428
return ("0" * length + str(index))[-length:]
418429

419-
def _download_sdk(*, module_ctx, sdks_by_version, used_versions, name, goos, goarch, download_tag):
430+
def _download_sdk(*, get_sdks_by_version, name, goos, goarch, download_tag):
420431
version = download_tag.version
421432
sdks = download_tag.sdks
422433
if version and not sdks:
423-
# Avoid a download without a known digest in the SDK repo rule by fetching the SDKs filename
424-
# and digest here. When using a version of Bazel that supports module extension facts, this
425-
# info will be persisted in the lockfile, allowing for truly airgapped builds with an
426-
# up-to-date lockfile and download (formerly repository) cache.
427-
if version not in sdks_by_version:
428-
# Lazily fetch the information about all SDKs so that we avoid the download if the facts
429-
# already contain all the versions we care about.
430-
sdks_by_version.clear()
431-
sdks_by_version.update(fetch_sdks_by_version(module_ctx))
432-
if version not in sdks_by_version:
433-
fail("go_sdk: no SDKs found for version {} requested by".format(version), download_tag)
434-
used_versions[version] = True
435-
sdks = sdks_by_version[version]
434+
sdks = get_sdks_by_version(version)
436435

437436
go_download_sdk_rule(
438437
name = name,

go/private/sdk.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,15 +570,20 @@ def _parse_versions_json(data):
570570
for sdk in sdks
571571
}
572572

573-
def fetch_sdks_by_version(ctx):
574-
ctx.download(
573+
def fetch_sdks_by_version(ctx, allow_fail = False):
574+
result = ctx.download(
575575
url = [
576576
"https://go.dev/dl/?mode=json&include=all",
577577
"https://golang.google.cn/dl/?mode=json&include=all",
578578
],
579579
output = "versions.json",
580+
allow_fail = allow_fail,
580581
)
582+
if not result.success:
583+
return {}
581584
data = ctx.read("versions.json")
585+
if (not data or data[0] != "[") and allow_fail:
586+
return {}
582587

583588
# module_ctx doesn't have delete, but its files are temporary anyway.
584589
if hasattr(ctx, "delete"):

tests/bcr/.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.4.1
1+
1079973bd513b69fb56becc7fe678c03449b30c3

0 commit comments

Comments
 (0)