Skip to content

Commit

Permalink
simplify zig build workaround
Browse files Browse the repository at this point in the history
From ziglang/zig#14815 (comment):

> @motiejus to be clear, this unrelated issue is not affected by a cache
> clear, and can be resolved by simply rerunning the build (and not
> getting very unlucky again).
  • Loading branch information
motiejus committed May 17, 2023
1 parent f42e5ea commit b98a570
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions toolchain/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ stderr={stderr}
stdout={stdout}
You stumbled into a problem with Zig SDK that bazel-zig-cc was not able to fix.
Please file a new issue to github.com/uber/bazel-zig-cc with:
Please file a new issue to github.com/uber/hermetic_cc_toolchain with:
- Full output of this Bazel run, including the Bazel command.
- Version of the Zig SDK if you have a non-default.
- Version of bazel-zig-cc.
- Version of hermetic_cc_toolchain
Note: this *may* have been https://github.com/ziglang/zig/issues/14815, for
which bazel-zig-cc has a workaround and you may have been "struck by lightning"
three times in a row.
Note: this *may* have been https://github.com/ziglang/zig/issues/14978, for
which hermetic_cc_toolchain has a workaround and you may have been "struck by
lightning" three times in a row.
"""

def toolchains(
Expand Down Expand Up @@ -129,7 +129,6 @@ def _zig_repository_impl(repository_ctx):

for dest, src in {
"BUILD": "//toolchain:BUILD.sdk.bazel",
# "private/BUILD": "//toolchain/private:BUILD.sdk.bazel",
}.items():
repository_ctx.template(
dest,
Expand Down Expand Up @@ -177,27 +176,13 @@ def _zig_repository_impl(repository_ctx):
"launcher.zig",
] + (["-static"] if os == "linux" else [])

# The elaborate code below is a workaround for ziglang/zig#14815:
# Sometimes, when Zig's cache is empty, compiling the launcher may fail
# with `error: FileNotFound`. The remedy is to clear the cache and try
# again. Until this change, we have been asking users to clear the Zig
# cache themselves and re-run the Bazel command.
#
# We can do better than that: if we detect the launcher failed, we can
# purge the zig cache and retry the compilation. It will be retried for up
# to two times.
# The elaborate code below is a workaround for ziglang/zig#14978: a race in
# Windows where zig may error with `error: AccessDenied`.
launcher_success = True
launcher_err_msg = ""
for _ in range(3):
# Do not remove the cache_prefix itself, because it is not controlled
# by this script. Instead, clear the cache subdirs that we know Zig
# populates.
zig_cache_dirs = ["h", "o", "tmp", "z"]
if not launcher_success:
print("Launcher compilation failed. Clearing %s/{%s} and retrying" %
(cache_prefix, ",".join(zig_cache_dirs)))
for d in zig_cache_dirs:
repository_ctx.delete(_paths_join(cache_prefix, d))
print("Launcher compilation failed. Retrying build")

ret = repository_ctx.execute(
compile_cmd,
Expand Down

0 comments on commit b98a570

Please sign in to comment.