Skip to content

crate_universe: emit build_script_env_files on rendered cargo_build_script targets#4044

Open
ashi009 wants to merge 1 commit into
bazelbuild:mainfrom
ashi009:xs/fix-build-script-env-files
Open

crate_universe: emit build_script_env_files on rendered cargo_build_script targets#4044
ashi009 wants to merge 1 commit into
bazelbuild:mainfrom
ashi009:xs/fix-build-script-env-files

Conversation

@ashi009
Copy link
Copy Markdown

@ashi009 ashi009 commented May 15, 2026

Fixes #4037. Reproducer: https://github.com/ashi009/rules-rust-4037-repro.

Problem

#3632 added a build_script_env_files attribute to cargo_build_script, threaded it through BuildScriptAttributes, and inserted ":cargo_toml_env_vars" into it when generate_cargo_toml_env_vars = True:

build_script_attrs
.rustc_env_files
.insert(":cargo_toml_env_vars".to_owned(), None);
build_script_attrs
.build_script_env_files
.insert(":cargo_toml_env_vars".to_owned(), None);
}
}

But the CargoBuildScript starlark struct in crate_universe/src/utils/starlark.rs — the one whose #[derive(Serialize)] emits cargo_build_script(...) into the rendered BUILD.bazel — was not updated to include a build_script_env_files field. Serde drops the value on the way out, so the rendered output looks like:

cargo_build_script(
    name = "_bs",
    ...
    rustc_env_files = [":cargo_toml_env_vars"],
    # build_script_env_files = [":cargo_toml_env_vars"]  ← missing
    ...
)

rustc_env_files sets env when compiling build.rs (so env!(...) works). build_script_env_files sets env when executing the compiled binary (so env::var(...) works). The built crate uses env::var("CARGO_PKG_AUTHORS") at execution time, so anything depending on built (e.g. rav1e) panics:

thread 'main' panicked at .../built-0.7.7/src/environment.rs:54:9:
Missing expected environment variable "CARGO_PKG_AUTHORS"

#3632's regression test (test_data/build_script_env_files/) exercises a hand-written cargo_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_files to CargoBuildScript and populate it from attrs.build_script_env_files in make_cargo_build_script, mirroring the existing rustc_env_files plumbing. 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").

  • Without this PR: bazel build @crates//:rav1e panics with the CARGO_PKG_AUTHORS error above.
  • With this PR (cargo-bazel built from this branch via CARGO_BAZEL_GENERATOR_URL): build succeeds. Generated BUILD.rav1e-0.7.1.bazel emits build_script_env_files = [":cargo_toml_env_vars"] on the cargo_build_script target.

Tested against Bazel 7.7.1, macOS arm64.

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crate_universe: generate_cargo_toml_env_vars=True doesn't propagate CARGO_PKG_* env to build script execution (built crate, rav1e)

1 participant