Subject: [PATCH bazel-zig-cc 0/1] Move Label dependencies above archive fetching to reduce restarts

From: ~kmicklas <[email-masked]>
Date: Thu, 15 Sep 2022 18:30:04 +0000

Before this, when running the repository rule, you might notice the
Bazel output say it was extracting the SDK archive ~7 times. Each time a
label dependency is used, it would restart the whole rule with the new
dependency. Now, the restarts technically still happen, but are not
noticeable since they occur before doing any substantial work.

Ken Micklas (1):
  Move Label dependencies above archive fetching to reduce restarts

 toolchain/defs.bzl | 53 +++++++++++++++++++++++++---------------------
 1 file changed, 29 insertions(+), 24 deletions(-)

-- 
2.34.4

---

From: ~kmicklas <[email-masked]>
Date: Thu, 15 Sep 2022 19:20:21 +0100

From: Ken Micklas <[email-masked]>

---

diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl
index 71214f6..ceddb70 100644
--- a/toolchain/defs.bzl
+++ b/toolchain/defs.bzl
@@ -170,6 +170,35 @@ def _zig_repository_impl(repository_ctx):
         "host_platform": host_platform,
     }
 
+    # Fetch Label dependencies before doing download/extract.
+    # The Bazel docs are not very clear about this behavior but see:
+    # https://bazel.build/extending/repo#when_is_the_implementation_function_executed
+    # and a related rules_go PR:
+    # https://github.com/bazelbuild/bazel-gazelle/pull/1206
+    for dest, src in {
+        "platform/BUILD": "//toolchain/platform:BUILD",
+        "toolchain/BUILD": "//toolchain/toolchain:BUILD",
+        "libc/BUILD": "//toolchain/libc:BUILD",
+        "libc_aware/platform/BUILD": "//toolchain/libc_aware/platform:BUILD",
+        "libc_aware/toolchain/BUILD": "//toolchain/libc_aware/toolchain:BUILD",
+    }.items():
+        repository_ctx.symlink(Label(src), dest)
+
+    for dest, src in {
+        "BUILD": "//toolchain:BUILD.sdk.bazel",
+        "private/BUILD": "//toolchain/private:BUILD.sdk.bazel",
+    }.items():
+        repository_ctx.template(
+            dest,
+            Label(src),
+            executable = False,
+            substitutions = {
+                "{absolute_path}": _quote(str(repository_ctx.path(""))),
+                "{os}": _quote(os),
+                "{zig_include_root}": _quote(zig_include_root),
+            },
+        )
+
     urls = [uf.format(**format_vars) for uf in repository_ctx.attr.url_formats]
     repository_ctx.download_and_extract(
         auth = use_netrc(read_user_netrc(repository_ctx), urls, {}),
@@ -200,30 +229,6 @@ def _zig_repository_impl(repository_ctx):
         content = _fcntl_h,
     )
 
-    for dest, src in {
-        "platform/BUILD": "//toolchain/platform:BUILD",
-        "toolchain/BUILD": "//toolchain/toolchain:BUILD",
-        "libc/BUILD": "//toolchain/libc:BUILD",
-        "libc_aware/platform/BUILD": "//toolchain/libc_aware/platform:BUILD",
-        "libc_aware/toolchain/BUILD": "//toolchain/libc_aware/toolchain:BUILD",
-    }.items():
-        repository_ctx.symlink(Label(src), dest)
-
-    for dest, src in {
-        "BUILD": "//toolchain:BUILD.sdk.bazel",
-        "private/BUILD": "//toolchain/private:BUILD.sdk.bazel",
-    }.items():
-        repository_ctx.template(
-            dest,
-            Label(src),
-            executable = False,
-            substitutions = {
-                "{absolute_path}": _quote(str(repository_ctx.path(""))),
-                "{os}": _quote(os),
-                "{zig_include_root}": _quote(zig_include_root),
-            },
-        )
-
 zig_repository = repository_rule(
     attrs = {
         "version": attr.string(),
-- 
2.34.4

---

From: "builds.sr.ht" <[email-masked]>
Date: Thu, 15 Sep 2022 18:46:59 +0000

bazel-zig-cc/patches/.build.yml: SUCCESS in 16m52s

[Move Label dependencies above archive fetching to reduce restarts][0] from [~kmicklas][1]

[0]: https://lists.sr.ht/~motiejus/bazel-zig-cc/patches/35345
[1]: mailto:[email-masked]

✓ #845473 SUCCESS bazel-zig-cc/patches/.build.yml https://builds.sr.ht/~motiejus/job/845473

---

From: Motiejus Jakštys <[email-masked]>
Date: Thu, 15 Sep 2022 22:20:27 +0300

>  1 file changed, 29 insertions(+), 24 deletions(-)

Applied, thanks!

Motiejus