Skip to content

Commit 425af55

Browse files
authored
Clarify package ID specifications in SBOMs are fully qualified (#15731)
### What does this PR try to resolve? cargo-auditable 0.7.0 will use the unstable Cargo SBOM precursor files if a user configures Cargo to generate the SBOM files. cargo-auditable assumes that the package ID specifiers in Cargo SBOM files are fully qualified. We'd like to enforce this assumption in Cargo so we can keep our package ID spec parsing simpler by not considering non-fully qualified package ID specs. This PR updates the cargo docs to state where fully qualified package ID specs are used, and also adds SBOMs to the existing `cargo pkgid` test that is currently enforcing consistency between the various usages of fully qualified package id specs. Previously raised at [#t-cargo > sbom missing name, version, source @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/sbom.20missing.20name.2C.20version.2C.20source/near/525443447) ### How to test and review this PR? Change doesn't affect current behaviour.
2 parents 1743cb8 + 6713b1a commit 425af55

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

src/doc/src/reference/pkgid-spec.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ is a string which is used to uniquely refer to one package within a graph of
99
packages.
1010

1111
The specification may be fully qualified, such as
12-
`https://github.com/rust-lang/crates.io-index#regex@1.4.3` or it may be
12+
`registry+https://github.com/rust-lang/crates.io-index#regex@1.4.3` or it may be
1313
abbreviated, such as `regex`. The abbreviated form may be used as long as it
1414
uniquely identifies a single package in the dependency graph. If there is
1515
ambiguity, additional qualifiers can be added to make it unique. For example,
1616
if there are two versions of the `regex` package in the graph, then it can be
1717
qualified with a version to make it unique, such as `regex@1.4.3`.
1818

19+
Package ID specifications output by cargo, for example in [cargo metadata](../commands/cargo-metadata.md) output, are fully qualified.
20+
1921
### Specification grammar
2022

2123
The formal grammar for a Package Id Specification is:

src/doc/src/reference/unstable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ that are uplifted into the target or artifact directories.
480480
// crate is compiled differently (different opt-level, features, etc).
481481
"crates": [
482482
{
483-
// Package ID specification
483+
// Fully qualified package ID specification
484484
"id": "path+file:///sample-package#0.1.0",
485485
// List of target kinds: bin, lib, rlib, dylib, cdylib, staticlib, proc-macro, example, test, bench, custom-build
486486
"kind": ["bin"],

tests/testsuite/pkgid.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Tests for the `cargo pkgid` command.
22
3+
use std::path::PathBuf;
4+
35
use crate::prelude::*;
6+
use cargo_test_support::basic_bin_manifest;
47
use cargo_test_support::basic_lib_manifest;
58
use cargo_test_support::compare::assert_e2e;
69
use cargo_test_support::git;
@@ -291,11 +294,12 @@ Please re-run this command with one of the following specifications:
291294
// * Package ID specifications
292295
// * machine-readable message via `--message-format=json`
293296
// * `cargo metadata` output
297+
// * SBOMs
294298
#[cargo_test]
295299
fn pkgid_json_message_metadata_consistency() {
296300
let p = project()
297-
.file("Cargo.toml", &basic_lib_manifest("foo"))
298-
.file("src/lib.rs", "fn unused() {}")
301+
.file("Cargo.toml", &basic_bin_manifest("foo"))
302+
.file("src/main.rs", "fn main() {}")
299303
.file("build.rs", "fn main() {}")
300304
.build();
301305

@@ -321,12 +325,6 @@ fn pkgid_json_message_metadata_consistency() {
321325
"reason": "build-script-executed",
322326
"...": "{...}"
323327
},
324-
{
325-
"manifest_path": "[ROOT]/foo/Cargo.toml",
326-
"package_id": "path+[ROOTURL]/foo#0.5.0",
327-
"reason": "compiler-message",
328-
"...": "{...}"
329-
},
330328
{
331329
"manifest_path": "[ROOT]/foo/Cargo.toml",
332330
"package_id": "path+[ROOTURL]/foo#0.5.0",
@@ -404,4 +402,53 @@ fn pkgid_json_message_metadata_consistency() {
404402
.is_json(),
405403
)
406404
.run();
405+
406+
p.cargo("build -Zsbom")
407+
.env("CARGO_BUILD_SBOM", "true")
408+
.masquerade_as_nightly_cargo(&["sbom"])
409+
.run();
410+
411+
let path = {
412+
let mut path = p.bin("foo").into_os_string();
413+
path.push(".cargo-sbom.json");
414+
PathBuf::from(path)
415+
};
416+
417+
assert!(path.is_file());
418+
let output = std::fs::read_to_string(&path).unwrap();
419+
assert_e2e().eq(
420+
output,
421+
snapbox::str![[r#"
422+
{
423+
"crates": [
424+
{
425+
"dependencies": [
426+
{
427+
"index": 1,
428+
"kind": "build"
429+
}
430+
],
431+
"features": [],
432+
"id": "path+[ROOTURL]/foo#0.5.0",
433+
"kind": [
434+
"bin"
435+
]
436+
},
437+
{
438+
"dependencies": [],
439+
"features": [],
440+
"id": "path+[ROOTURL]/foo#0.5.0",
441+
"kind": [
442+
"custom-build"
443+
]
444+
}
445+
],
446+
"root": 0,
447+
"rustc": "{...}",
448+
"target": "[HOST_TARGET]",
449+
"version": 1
450+
}
451+
"#]]
452+
.is_json(),
453+
);
407454
}

0 commit comments

Comments
 (0)