Skip to content

Commit 8d7a3c0

Browse files
Fix cargo_bootstrap_repository not triggering on changes to srcs (#959)
* Consume srcs of `cargo_bootstrap_repository` so changes trigger the rule * Regenerate documentation * Improve logging * Update cargo/cargo_bootstrap.bzl Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com> * Use formatted fail message over print Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
1 parent fe5ddf3 commit 8d7a3c0

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

cargo/cargo_bootstrap.bzl

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ _CARGO_BUILD_MODES = [
88
"debug",
99
]
1010

11+
_FAIL_MESSAGE = """\
12+
Process exited with code '{code}'
13+
# ARGV ########################################################################
14+
{argv}
15+
16+
# STDOUT ######################################################################
17+
{stdout}
18+
19+
# STDERR ######################################################################
20+
{stderr}
21+
"""
22+
1123
def cargo_bootstrap(
1224
repository_ctx,
1325
cargo_bin,
@@ -69,8 +81,11 @@ def cargo_bootstrap(
6981
)
7082

7183
if result.return_code != 0:
72-
fail("exit_code: {}".format(
73-
result.return_code,
84+
fail(_FAIL_MESSAGE.format(
85+
code = result.return_code,
86+
argv = args,
87+
stdout = result.stdout,
88+
stderr = result.stderr,
7489
))
7590

7691
extension = ""
@@ -134,7 +149,25 @@ def _collect_environ(repository_ctx, host_triple):
134149

135150
return dict(env_vars.items() + env_labels.items())
136151

152+
def _detect_changes(repository_ctx):
153+
"""Inspect files that are considered inputs to the build for changes
154+
155+
Args:
156+
repository_ctx (repository_ctx): The rule's context object.
157+
"""
158+
# Simply generating a `path` object consideres the file as 'tracked' or
159+
# 'consumed' which means changes to it will trigger rebuilds
160+
161+
for src in repository_ctx.attr.srcs:
162+
repository_ctx.path(src)
163+
164+
repository_ctx.path(repository_ctx.attr.cargo_lockfile)
165+
repository_ctx.path(repository_ctx.attr.cargo_toml)
166+
137167
def _cargo_bootstrap_repository_impl(repository_ctx):
168+
# Pretend to Bazel that this rule's input files have been used, so that it will re-run the rule if they change.
169+
_detect_changes(repository_ctx)
170+
138171
if repository_ctx.attr.version in ("beta", "nightly"):
139172
version_str = "{}-{}".format(repository_ctx.attr.version, repository_ctx.attr.iso_date)
140173
else:
@@ -223,9 +256,8 @@ cargo_bootstrap_repository = repository_rule(
223256
default = "rust_{system}_{arch}",
224257
),
225258
"srcs": attr.label_list(
226-
doc = "Souces to crate to build.",
259+
doc = "Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made",
227260
allow_files = True,
228-
mandatory = True,
229261
),
230262
"version": attr.string(
231263
doc = "The version of cargo the resolver should use",

crate_universe/bootstrap.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def crate_universe_bootstrap():
1111
name = "rules_rust_crate_universe_bootstrap",
1212
cargo_lockfile = Label("//crate_universe:Cargo.lock"),
1313
cargo_toml = Label("//crate_universe:Cargo.toml"),
14-
srcs = [Label("//crate_universe:resolver_srcs")],
1514
version = rust_common.default_version,
1615
binary = "crate_universe_resolver",
1716
env = {

docs/cargo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c
3131
| <a id="cargo_bootstrap_repository-iso_date"></a>iso_date | The iso_date of cargo binary the resolver should use. Note: This can only be set if <code>version</code> is <code>beta</code> or <code>nightly</code> | String | optional | "" |
3232
| <a id="cargo_bootstrap_repository-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
3333
| <a id="cargo_bootstrap_repository-rust_toolchain_repository_template"></a>rust_toolchain_repository_template | The template to use for finding the host <code>rust_toolchain</code> repository. <code>{version}</code> (eg. '1.53.0'), <code>{triple}</code> (eg. 'x86_64-unknown-linux-gnu'), <code>{system}</code> (eg. 'darwin'), and <code>{arch}</code> (eg. 'aarch64') will be replaced in the string if present. | String | optional | "rust_{system}_{arch}" |
34-
| <a id="cargo_bootstrap_repository-srcs"></a>srcs | Souces to crate to build. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | required | |
34+
| <a id="cargo_bootstrap_repository-srcs"></a>srcs | Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
3535
| <a id="cargo_bootstrap_repository-version"></a>version | The version of cargo the resolver should use | String | optional | "1.55.0" |
3636

3737

docs/flatten.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c
9292
| <a id="cargo_bootstrap_repository-iso_date"></a>iso_date | The iso_date of cargo binary the resolver should use. Note: This can only be set if <code>version</code> is <code>beta</code> or <code>nightly</code> | String | optional | "" |
9393
| <a id="cargo_bootstrap_repository-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
9494
| <a id="cargo_bootstrap_repository-rust_toolchain_repository_template"></a>rust_toolchain_repository_template | The template to use for finding the host <code>rust_toolchain</code> repository. <code>{version}</code> (eg. '1.53.0'), <code>{triple}</code> (eg. 'x86_64-unknown-linux-gnu'), <code>{system}</code> (eg. 'darwin'), and <code>{arch}</code> (eg. 'aarch64') will be replaced in the string if present. | String | optional | "rust_{system}_{arch}" |
95-
| <a id="cargo_bootstrap_repository-srcs"></a>srcs | Souces to crate to build. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | required | |
95+
| <a id="cargo_bootstrap_repository-srcs"></a>srcs | Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [] |
9696
| <a id="cargo_bootstrap_repository-version"></a>version | The version of cargo the resolver should use | String | optional | "1.55.0" |
9797

9898

0 commit comments

Comments
 (0)