Skip to content

Commit 3861f60

Browse files
authored
fix(sbom): Don't set CARGO_SBOM_PATH when empty (like stable) (#16419)
### What does this PR try to resolve? Noticed `CARGO_SBOM_PATH` was being set when playing around with artifact-deps. I could have gated the env by whether the nightly feature was enabled but figured it would be better to not set if it there aren't SBOMs. ### How to test and review this PR?
2 parents 1f5feec + 9755978 commit 3861f60

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

src/cargo/core/compiler/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,11 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
780780

781781
if is_primary {
782782
base.env("CARGO_PRIMARY_PACKAGE", "1");
783-
let file_list = std::env::join_paths(build_runner.sbom_output_files(unit)?)?;
784-
base.env("CARGO_SBOM_PATH", file_list);
783+
let file_list = build_runner.sbom_output_files(unit)?;
784+
if !file_list.is_empty() {
785+
let file_list = std::env::join_paths(file_list)?;
786+
base.env("CARGO_SBOM_PATH", file_list);
787+
}
785788
}
786789

787790
if unit.target.is_test() || unit.target.is_bench() {

tests/testsuite/sbom.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,25 @@ fn append_sbom_suffix(link: &PathBuf) -> PathBuf {
2222
fn warn_without_passing_unstable_flag() {
2323
let p = project()
2424
.file("Cargo.toml", &basic_bin_manifest("foo"))
25-
.file("src/main.rs", r#"fn main() {}"#)
25+
.file(
26+
"src/main.rs",
27+
r#"fn main() {
28+
eprintln!("{:?}", option_env!("CARGO_SBOM_PATH"));
29+
}"#,
30+
)
2631
.build();
2732

28-
p.cargo("build")
33+
p.cargo("run")
2934
.env("CARGO_BUILD_SBOM", "true")
3035
.masquerade_as_nightly_cargo(&["sbom"])
31-
.with_stderr_data(
32-
"\
33-
[WARNING] ignoring 'sbom' config, pass `-Zsbom` to enable it\n\
34-
[COMPILING] foo v0.5.0 ([..])\n\
35-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]\n",
36-
)
36+
.with_stderr_data(snapbox::str![[r#"
37+
[WARNING] ignoring 'sbom' config, pass `-Zsbom` to enable it
38+
[COMPILING] foo v0.5.0 ([ROOT]/foo)
39+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
40+
[RUNNING] `target/debug/foo[EXE]`
41+
None
42+
43+
"#]])
3744
.run();
3845

3946
let file = append_sbom_suffix(&p.bin("foo"));

0 commit comments

Comments
 (0)