Skip to content

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

@ashi009

Description

@ashi009

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions