Skip to content

Commit 86d809e

Browse files
authored
fix: Return repo_metadata from repository rules in rust/repositories.bzl (#3855)
This allows repos produced by `rust_toolchain_tools_repository` to use the remote repo content cache. Using Bazel 9.0.0 and the remote repo content cache, the external directory in the output base is much smaller when using this patch. Downloading the toolchain, which comes in at almost 800 MB, can be entirely skipped when using remote execution. Before: ```shell external # du -h -d1 | sort -h | grep rules_rust <SNIP> 4.0K ./rules_rust++i+rules_rust_tinyjson 32K ./rules_rust++rust+rust_toolchains 196K ./rules_rust_prost+ 6.1M ./rules_rust+ 787M ./rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools ``` After: ```shell external # du -h -d1 | sort -h | grep rules_rust <SNIP> 4.0K ./rules_rust++i+rules_rust_tinyjson 4.0K ./rules_rust++rust+rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools 32K ./rules_rust++rust+rust_toolchains 196K ./rules_rust_prost+ 6.1M ./rules_rust+ ```
1 parent 42b098b commit 86d809e

1 file changed

Lines changed: 27 additions & 3 deletions

File tree

rust/repositories.bzl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,15 @@ def _rust_toolchain_tools_repository_impl(ctx):
564564
repro[key] = getattr(ctx.attr, key)
565565
repro["sha256s"] = sha256s
566566

567-
return repro
567+
# Bazel <8.3.0 lacks ctx.repo_metadata
568+
if not hasattr(ctx, "repo_metadata"):
569+
return repro
570+
571+
reproducible = sha256s == dict(ctx.attr.sha256s)
572+
return ctx.repo_metadata(
573+
reproducible = reproducible,
574+
attrs_for_reproducibility = {} if reproducible else repro,
575+
)
568576

569577
rust_toolchain_tools_repository = repository_rule(
570578
doc = (
@@ -834,7 +842,15 @@ def _rust_analyzer_toolchain_tools_repository_impl(repository_ctx):
834842
repro[key] = getattr(repository_ctx.attr, key)
835843
repro["sha256s"] = sha256s
836844

837-
return repro
845+
# Bazel <8.3.0 lacks ctx.repo_metadata
846+
if not hasattr(repository_ctx, "repo_metadata"):
847+
return repro
848+
849+
reproducible = sha256s == dict(repository_ctx.attr.sha256s)
850+
return repository_ctx.repo_metadata(
851+
reproducible = reproducible,
852+
attrs_for_reproducibility = {} if reproducible else repro,
853+
)
838854

839855
rust_analyzer_toolchain_tools_repository = repository_rule(
840856
doc = "A repository rule for defining a rust_analyzer_toolchain with a `rust-src` artifact.",
@@ -979,7 +995,15 @@ def _rustfmt_toolchain_tools_repository_impl(repository_ctx):
979995
repro[key] = getattr(repository_ctx.attr, key)
980996
repro["sha256s"] = sha256s
981997

982-
return repro
998+
# Bazel <8.3.0 lacks ctx.repo_metadata
999+
if not hasattr(repository_ctx, "repo_metadata"):
1000+
return repro
1001+
1002+
reproducible = sha256s == dict(repository_ctx.attr.sha256s)
1003+
return repository_ctx.repo_metadata(
1004+
reproducible = reproducible,
1005+
attrs_for_reproducibility = {} if reproducible else repro,
1006+
)
9831007

9841008
rustfmt_toolchain_tools_repository = repository_rule(
9851009
doc = "A repository rule for defining a rustfmt_toolchain.",

0 commit comments

Comments
 (0)