Skip to content

Commit 89beca7

Browse files
Adjective-ObjectMax Huang-Hobbs
authored andcommitted
Fix cargo_build_script env vars from cargo_toml_env_vars for build script execution
Problem: Build scripts for several crates were failing when using crate_universe with generate_cargo_toml_env_vars=True (the default). The failures occurred because CARGO_PKG_* environment variables extracted from Cargo.toml were not being passed to build script execution. Affected crates included: - rav1e 0.7.1: Its build script depends on the built crate (0.7.7), which calls std::env::var("CARGO_PKG_AUTHORS") at runtime and panics if the variable is missing. - typenum 1.19.0: Build script failed with "Wrong environment file format" because CARGO_PKG_DESCRIPTION contained embedded newlines escaped with backslashes. Root causes: 1. Missing build_script_env_files in CargoBuildScript: The render_one_build_file() function in rendering.rs correctly populates build_script_attrs.build_script_env_files with the ":cargo_toml_env_vars" target. However, the CargoBuildScript struct in starlark.rs lacked a build_script_env_files field, so this data was never serialized to the generated BUILD.bazel files. Note: rustc_env_files passes env vars to rustc for compile-time env!() macro expansion. build_script_env_files passes env vars to the build script binary at runtime for std::env::var() access. 2. Line continuation parsing in cargo_build_script_runner: The cargo_toml_variable_extractor correctly escapes newlines in values using backslash continuations (e.g., "value\\nmore"). However, the build script runner's env file parser split on '\n' without handling continuations, causing lines like " compile time." to fail the KEY=value format check. Fix: 1. Add build_script_env_files field to CargoBuildScript struct and populate it from build_script_attrs in make_cargo_build_script(). 2. Update the env file parser to accumulate lines ending with '\' before splitting on '=' to extract the key-value pair. Fixes build failures for crates whose build scripts read CARGO_PKG_* variables at runtime, including those with multiline descriptions.
1 parent ca4915c commit 89beca7

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

cargo/private/cargo_build_script_runner/bin.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,25 @@ fn run_buildrs() -> Result<(), String> {
100100

101101
for dep_env_path in input_dep_env_paths.iter() {
102102
if let Ok(contents) = read_to_string(dep_env_path) {
103+
// Handle line continuations: lines ending with '\' continue on the next line
104+
let mut current_line = String::new();
103105
for line in contents.split('\n') {
104-
// split on empty contents will still produce a single empty string in iterable.
105-
if line.is_empty() {
106+
// Skip empty lines when we're not in a continuation
107+
if line.is_empty() && current_line.is_empty() {
106108
continue;
107109
}
108-
match line.split_once('=') {
110+
111+
if let Some(prefix) = line.strip_suffix('\\') {
112+
// Line continues on the next line
113+
current_line.push_str(prefix);
114+
current_line.push('\n');
115+
continue;
116+
}
117+
118+
// Complete line (either standalone or end of continuation)
119+
current_line.push_str(line);
120+
121+
match current_line.split_once('=') {
109122
Some((key, value)) => {
110123
command.env(key, value.replace("${pwd}", &exec_root.to_string_lossy()));
111124
}
@@ -115,6 +128,7 @@ fn run_buildrs() -> Result<(), String> {
115128
)
116129
}
117130
}
131+
current_line.clear();
118132
}
119133
} else {
120134
return Err("error: Dependency environment file unreadable".to_owned());

crate_universe/src/rendering.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,12 @@ impl Renderer {
641641
.unwrap_or_default(),
642642
platforms,
643643
),
644+
build_script_env_files: SelectSet::new(
645+
attrs
646+
.map(|attrs| attrs.build_script_env_files.clone())
647+
.unwrap_or_default(),
648+
platforms,
649+
),
644650
rustc_flags: SelectList::new(
645651
// In most cases, warnings in 3rd party crates are not
646652
// interesting as they're out of the control of consumers. The

crate_universe/src/utils/starlark.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub(crate) struct CargoBuildScript {
9898
pub(crate) aliases: SelectDict<Label, String>,
9999
#[serde(skip_serializing_if = "SelectDict::is_empty")]
100100
pub(crate) build_script_env: SelectDict<String, String>,
101+
#[serde(skip_serializing_if = "SelectSet::is_empty")]
102+
pub(crate) build_script_env_files: SelectSet<String>,
101103
#[serde(skip_serializing_if = "Data::is_empty")]
102104
pub(crate) compile_data: Data,
103105
#[serde(skip_serializing_if = "SelectDict::is_empty")]

0 commit comments

Comments
 (0)