Context
Follow-up to #3527. With generate_cargo_toml_env_vars = True set on the crate_universe extension, I expected CARGO_PKG_AUTHORS (and friends) to be available at build script execution time — but it's only available at build script compilation time. The result: rav1e's build.rs (which uses the built crate) panics with:
thread 'main' panicked at external/crate_index__built-0.7.5/src/environment.rs:53:9:
Missing expected environment variable "CARGO_PKG_AUTHORS"
Root cause
In crate_universe/src/rendering.rs:380 (0.70.0):
if self.config.generate_cargo_toml_env_vars {
krate.common_attrs.rustc_env_files.insert(":cargo_toml_env_vars".to_owned(), None);
if let Some(ref mut build_script_attrs) = &mut krate.build_script_attrs {
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); // <-- this line is dead
}
}
The build_script_env_files insertion is supposed to wire cargo_toml_env_vars into the cargo_build_script's execution environment. But the CargoBuildScript starlark struct in crate_universe/src/utils/starlark.rs:95 has no build_script_env_files field — only rustc_env_files is serialized. So the insertion never reaches the rendered BUILD.bazel.
#[derive(Debug, Serialize)]
#[serde(rename = \"cargo_build_script\")]
pub(crate) struct CargoBuildScript {
...
#[serde(skip_serializing_if = \"SelectSet::is_empty\")]
pub(crate) rustc_env_files: SelectSet<String>, // present
// missing: build_script_env_files
...
}
Compare with cargo/private/cargo_build_script_wrapper.bzl which does accept build_script_env_files.
Reproducer
Add rav1e = \"0.7\" to a Cargo.toml, render with crate_universe (default generate_cargo_toml_env_vars = True), try to build it. Build script panics as above.
Proposed fix
Add build_script_env_files: SelectSet<Label> to the CargoBuildScript struct, populate it from attrs.build_script_env_files in make_cargo_build_script, and ensure the template emits it.
We carry a workaround patch that hardcodes empty defaults for CARGO_PKG_AUTHORS/DESCRIPTION/HOMEPAGE/LICENSE/REPOSITORY in cargo/private/cargo_build_script.bzl — not great because it doesn't use the actual Cargo.toml values. The proper fix would let the existing cargo_toml_env_vars rule do its job.
Versions
- rules_rust: 0.70.0
- bazel: 7.7.1
Context
Follow-up to #3527. With
generate_cargo_toml_env_vars = Trueset on the crate_universe extension, I expectedCARGO_PKG_AUTHORS(and friends) to be available at build script execution time — but it's only available at build script compilation time. The result:rav1e'sbuild.rs(which uses thebuiltcrate) panics with:Root cause
In
crate_universe/src/rendering.rs:380(0.70.0):The
build_script_env_filesinsertion is supposed to wirecargo_toml_env_varsinto the cargo_build_script's execution environment. But theCargoBuildScriptstarlark struct incrate_universe/src/utils/starlark.rs:95has nobuild_script_env_filesfield — onlyrustc_env_filesis serialized. So the insertion never reaches the rendered BUILD.bazel.Compare with
cargo/private/cargo_build_script_wrapper.bzlwhich does acceptbuild_script_env_files.Reproducer
Add
rav1e = \"0.7\"to aCargo.toml, render with crate_universe (defaultgenerate_cargo_toml_env_vars = True), try to build it. Build script panics as above.Proposed fix
Add
build_script_env_files: SelectSet<Label>to theCargoBuildScriptstruct, populate it fromattrs.build_script_env_filesinmake_cargo_build_script, and ensure the template emits it.We carry a workaround patch that hardcodes empty defaults for
CARGO_PKG_AUTHORS/DESCRIPTION/HOMEPAGE/LICENSE/REPOSITORYincargo/private/cargo_build_script.bzl— not great because it doesn't use the actual Cargo.toml values. The proper fix would let the existingcargo_toml_env_varsrule do its job.Versions