Skip to content

Commit 60036d4

Browse files
committed
fix(crate_universe): fix OUTPUT_BASE env var value format in crates_vendor
The OUTPUT_BASE env var override inserted `format!("output_base: {}", path)` as the HashMap value. However, the `bazel info` output parser splits each line on `:` and stores only the part after the colon as the value: let (k, v) = line.split_at(line.find(':')?); Ok((k.to_string(), (v[1..]).trim().to_string())) So for `bazel info` output like "output_base: /data/output", the HashMap contains {"output_base" => "/data/output"}. But the OUTPUT_BASE override stored the FULL formatted string as the value: {"output_base" => "output_base: /data/output"}. When TryFrom later converts this to a PathBuf via `.map(Into::into)`, it produces PathBuf("output_base: /data/output") — an invalid path. This means the OUTPUT_BASE env var has never worked correctly. Fix by storing the raw path string without the prefix. Also extract `parse_bazel_info` from `try_new` so the parsing and env var override logic can be unit-tested without spawning a subprocess.
1 parent 39a22fe commit 60036d4

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

crate_universe/src/cli/vendor.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ impl BazelInfo {
168168
}
169169

170170
let output = String::from_utf8_lossy(output.stdout.as_slice());
171+
Self::parse_bazel_info(&output)
172+
}
173+
174+
/// Parse `bazel info` output into a `BazelInfo`. The `OUTPUT_BASE`
175+
/// environment variable, when set, overrides the parsed output_base.
176+
fn parse_bazel_info(output: &str) -> anyhow::Result<Self> {
171177
let mut bazel_info: HashMap<String, String> = output
172178
.trim()
173179
.split('\n')
@@ -183,7 +189,7 @@ impl BazelInfo {
183189
// Allow a predefined environment variable to take precedent. This
184190
// solves for the specific needs of Bazel CI on Github.
185191
if let Ok(path) = env::var("OUTPUT_BASE") {
186-
bazel_info.insert("output_base".to_owned(), format!("output_base: {}", path));
192+
bazel_info.insert("output_base".to_owned(), path);
187193
};
188194

189195
BazelInfo::try_from(bazel_info)
@@ -409,4 +415,21 @@ mod tests {
409415
);
410416
assert_eq!(PathBuf::from("/tmp/output_base"), info.output_base);
411417
}
418+
419+
#[test]
420+
fn test_parse_bazel_info_output_base_env_override() {
421+
let bazel_info_output = "release: 8.0.0\noutput_base: /original/path";
422+
423+
// Without OUTPUT_BASE set, parse_bazel_info uses the parsed value.
424+
env::remove_var("OUTPUT_BASE");
425+
let info = BazelInfo::parse_bazel_info(bazel_info_output).unwrap();
426+
assert_eq!(PathBuf::from("/original/path"), info.output_base);
427+
428+
// With OUTPUT_BASE set, it overrides the parsed value.
429+
env::set_var("OUTPUT_BASE", "/override/path");
430+
let info = BazelInfo::parse_bazel_info(bazel_info_output).unwrap();
431+
assert_eq!(PathBuf::from("/override/path"), info.output_base);
432+
433+
env::remove_var("OUTPUT_BASE");
434+
}
412435
}

0 commit comments

Comments
 (0)