Skip to content

Commit fc601ba

Browse files
Fix cargo-bazel recompile for MODULE.bazel (#2570)
- Addresses #2531 - Wrote a function, thats basically copy / pasted from `crates_universe/private/generate_utils.bzl` to account for the `module_ctx` Can probably make the original function more generic if thats more desired. I'm not sure if the maintainers would prefer a one off, since the `MODULE.bazel `feature set for `crates_universe` isn't exactly "Stable" yet. - Noticed theres no way to "bootstrap" the new `cargo` features in `MODULE.bazel` , like you can for `crates_universe`. May need to open an issue for this. Tested by using the `CARGO_BAZEL_GENERATOR_URL` and `CARGO_BAZEL_GENERATOR_SHA256` environmentals. This looks to cause some failing unit tests. Not sure how to get support on this - Failsafe of using the cargo buildstrap `cargo-bazel` if no URLs are found, and if theres no environmentals. --------- Co-authored-by: UebelAndre <[email protected]>
1 parent 98fab85 commit fc601ba

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

crate_universe/extension.bzl

+54-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
55
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
66
load("//crate_universe:defs.bzl", _crate_universe_crate = "crate")
77
load("//crate_universe/private:crates_vendor.bzl", "CRATES_VENDOR_ATTRS", "generate_config_file", "generate_splicing_manifest")
8-
load("//crate_universe/private:generate_utils.bzl", "render_config")
8+
load("//crate_universe/private:generate_utils.bzl", "CARGO_BAZEL_GENERATOR_SHA256", "CARGO_BAZEL_GENERATOR_URL", "GENERATOR_ENV_VARS", "render_config")
9+
load("//crate_universe/private:urls.bzl", "CARGO_BAZEL_SHA256S", "CARGO_BAZEL_URLS")
910
load("//crate_universe/private/module_extensions:cargo_bazel_bootstrap.bzl", "get_cargo_bazel_runner")
11+
load("//rust/platform:triple.bzl", "get_host_triple")
1012

1113
# A list of labels which may be relative (and if so, is within the repo the rule is generated in).
1214
#
@@ -220,8 +222,58 @@ def _package_to_json(p):
220222
if v
221223
})
222224

225+
def _get_generator(module_ctx):
226+
"""Query Network Resources to local a `cargo-bazel` binary.
227+
228+
Based off get_generator in crates_universe/private/generate_utils.bzl
229+
230+
Args:
231+
module_ctx (module_ctx): The rules context object
232+
233+
Returns:
234+
tuple(path, dict) The path to a 'cargo-bazel' binary. The pairing (dict)
235+
may be `None` if there is not need to update the attribute
236+
"""
237+
host_triple = get_host_triple(module_ctx)
238+
use_environ = False
239+
for var in GENERATOR_ENV_VARS:
240+
if var in module_ctx.os.environ:
241+
use_environ = True
242+
243+
if use_environ:
244+
generator_sha256 = module_ctx.os.environ.get(CARGO_BAZEL_GENERATOR_SHA256)
245+
generator_url = module_ctx.os.environ.get(CARGO_BAZEL_GENERATOR_URL)
246+
elif len(CARGO_BAZEL_URLS) == 0:
247+
return module_ctx.path(Label("@cargo_bazel_bootstrap//:cargo-bazel"))
248+
else:
249+
generator_sha256 = CARGO_BAZEL_SHA256S.get(host_triple)
250+
generator_url = CARGO_BAZEL_URLS.get(host_triple)
251+
252+
if not generator_url:
253+
fail((
254+
"No generator URL was found either in the `CARGO_BAZEL_GENERATOR_URL` " +
255+
"environment variable or for the `{}` triple in the `generator_urls` attribute"
256+
).format(host_triple))
257+
258+
output = module_ctx.path("cargo-bazel.exe" if "win" in module_ctx.os.name else "cargo-bazel")
259+
260+
# Download the file into place
261+
download_kwargs = {
262+
"executable": True,
263+
"output": output,
264+
"url": generator_url,
265+
}
266+
267+
if generator_sha256:
268+
download_kwargs.update({"sha256": generator_sha256})
269+
270+
module_ctx.download(**download_kwargs)
271+
return output
272+
223273
def _crate_impl(module_ctx):
224-
cargo_bazel = get_cargo_bazel_runner(module_ctx)
274+
cargo_bazel_output = _get_generator(module_ctx)
275+
cargo_bazel = get_cargo_bazel_runner(module_ctx, cargo_bazel_output)
276+
225277
all_repos = []
226278
for mod in module_ctx.modules:
227279
module_annotations = {}

crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ cargo_bazel_bootstrap = module_extension(
1515
doc = """Module extension to generate the cargo_bazel binary.""",
1616
)
1717

18-
def get_cargo_bazel_runner(module_ctx):
18+
def get_cargo_bazel_runner(module_ctx, cargo_bazel):
1919
"""A helper function to allow executing cargo_bazel in module extensions.
2020
2121
Args:
2222
module_ctx: The module extension's context.
23-
23+
cargo_bazel: Path The path to a `cargo-bazel` binary
2424
Returns:
2525
A function that can be called to execute cargo_bazel.
2626
"""
@@ -30,7 +30,6 @@ def get_cargo_bazel_runner(module_ctx):
3030

3131
cargo_path = str(module_ctx.path(Label("@rust_host_tools//:bin/cargo{}".format(binary_ext))))
3232
rustc_path = str(module_ctx.path(Label("@rust_host_tools//:bin/rustc{}".format(binary_ext))))
33-
cargo_bazel = module_ctx.path(Label("@cargo_bazel_bootstrap//:cargo-bazel"))
3433

3534
# Placing this as a nested function allows users to call this right at the
3635
# start of a module extension, thus triggering any restarts as early as

0 commit comments

Comments
 (0)