crate_universe: emit build_script_env_files on rendered cargo_build_script targets#4044
Open
ashi009 wants to merge 1 commit into
Open
crate_universe: emit build_script_env_files on rendered cargo_build_script targets#4044ashi009 wants to merge 1 commit into
ashi009 wants to merge 1 commit into
Conversation
…cript targets Fixes bazelbuild#4037. bazelbuild#3632 added the build_script_env_files attribute to cargo_build_script and wired it into BuildScriptAttributes + a `.insert(":cargo_toml_env_vars", ...)` call in the renderer when generate_cargo_toml_env_vars is set, but did not add a matching field to the `CargoBuildScript` starlark struct that gets Serialize-d into BUILD.bazel. The inserted value is silently dropped on the way to the rendered output, so build scripts that read CARGO_PKG_* at execution time (e.g. `built`, used by rav1e) still panic with `Missing expected environment variable "CARGO_PKG_AUTHORS"`. This change adds the field and populates it from attrs.build_script_env_files in make_cargo_build_script, mirroring the existing rustc_env_files plumbing. No new attributes; the rule and macro already accept build_script_env_files, the rendered BUILD.bazel just wasn't emitting it. Reproducer: https://github.com/ashi009/rules-rust-4037-repro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4037. Reproducer: https://github.com/ashi009/rules-rust-4037-repro.
Problem
#3632 added a
build_script_env_filesattribute tocargo_build_script, threaded it throughBuildScriptAttributes, and inserted":cargo_toml_env_vars"into it whengenerate_cargo_toml_env_vars = True:rules_rust/crate_universe/src/rendering.rs
Lines 386 to 394 in 56176ea
But the
CargoBuildScriptstarlark struct incrate_universe/src/utils/starlark.rs— the one whose#[derive(Serialize)]emitscargo_build_script(...)into the rendered BUILD.bazel — was not updated to include abuild_script_env_filesfield. Serde drops the value on the way out, so the rendered output looks like:rustc_env_filessets env when compilingbuild.rs(soenv!(...)works).build_script_env_filessets env when executing the compiled binary (soenv::var(...)works). Thebuiltcrate usesenv::var("CARGO_PKG_AUTHORS")at execution time, so anything depending onbuilt(e.g.rav1e) panics:#3632's regression test (
test_data/build_script_env_files/) exercises a hand-writtencargo_build_script(build_script_env_files = [...]), which works fine because the user writes the field directly. The crate_universe rendering path was never covered, so the gap went unnoticed.Fix
Add
build_script_env_filestoCargoBuildScriptand populate it fromattrs.build_script_env_filesinmake_cargo_build_script, mirroring the existingrustc_env_filesplumbing. No new attributes — the rule, macro, and context struct already support it.Verification
Repro: https://github.com/ashi009/rules-rust-4037-repro (tiny workspace with
rav1e = "0.7.1").bazel build @crates//:rav1epanics with theCARGO_PKG_AUTHORSerror above.CARGO_BAZEL_GENERATOR_URL): build succeeds. GeneratedBUILD.rav1e-0.7.1.bazelemitsbuild_script_env_files = [":cargo_toml_env_vars"]on thecargo_build_scripttarget.Tested against Bazel 7.7.1, macOS arm64.