Skip to content

Commit 1ae6310

Browse files
committed
Auto merge of #13311 - weihanglo:pkgid-json-message, r=epage
fix(json-msg): use pkgid spec in in JSON messages
2 parents a383063 + ad1a3b3 commit 1ae6310

20 files changed

+83
-56
lines changed

src/cargo/core/compiler/custom_build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ fn emit_build_output(
229229
.collect::<Vec<_>>();
230230

231231
let msg = machine_message::BuildScript {
232-
package_id,
232+
package_id: package_id.to_spec(),
233233
linked_libs: &output.library_links,
234234
linked_paths: &library_paths,
235235
cfgs: &output.cfgs,

src/cargo/core/compiler/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ fn link_targets(cx: &mut Context<'_, '_>, unit: &Unit, fresh: bool) -> CargoResu
578578
};
579579

580580
let msg = machine_message::Artifact {
581-
package_id,
581+
package_id: package_id.to_spec(),
582582
manifest_path,
583583
target: &target,
584584
profile: art_profile,
@@ -1766,7 +1766,7 @@ fn on_stderr_line_inner(
17661766
}
17671767

17681768
let msg = machine_message::FromCompiler {
1769-
package_id,
1769+
package_id: package_id.to_spec(),
17701770
manifest_path,
17711771
target,
17721772
message: compiler_message,

src/cargo/core/compiler/timings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'cfg> Timings<'cfg> {
222222
.extend(unlocked.iter().cloned().cloned());
223223
if self.report_json {
224224
let msg = machine_message::TimingInfo {
225-
package_id: unit_time.unit.pkg.package_id(),
225+
package_id: unit_time.unit.pkg.package_id().to_spec(),
226226
target: &unit_time.unit.target,
227227
mode: unit_time.unit.mode,
228228
duration: unit_time.duration,

src/cargo/util/machine_message.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use std::path::{Path, PathBuf};
22

3+
use cargo_util_schemas::core::PackageIdSpec;
34
use serde::ser;
45
use serde::Serialize;
56
use serde_json::{self, json, value::RawValue};
67

7-
use crate::core::{compiler::CompileMode, PackageId, Target};
8+
use crate::core::compiler::CompileMode;
9+
use crate::core::Target;
810

911
pub trait Message: ser::Serialize {
1012
fn reason(&self) -> &str;
@@ -19,7 +21,7 @@ pub trait Message: ser::Serialize {
1921

2022
#[derive(Serialize)]
2123
pub struct FromCompiler<'a> {
22-
pub package_id: PackageId,
24+
pub package_id: PackageIdSpec,
2325
pub manifest_path: &'a Path,
2426
pub target: &'a Target,
2527
pub message: Box<RawValue>,
@@ -33,7 +35,7 @@ impl<'a> Message for FromCompiler<'a> {
3335

3436
#[derive(Serialize)]
3537
pub struct Artifact<'a> {
36-
pub package_id: PackageId,
38+
pub package_id: PackageIdSpec,
3739
pub manifest_path: PathBuf,
3840
pub target: &'a Target,
3941
pub profile: ArtifactProfile,
@@ -71,7 +73,7 @@ pub enum ArtifactDebuginfo {
7173

7274
#[derive(Serialize)]
7375
pub struct BuildScript<'a> {
74-
pub package_id: PackageId,
76+
pub package_id: PackageIdSpec,
7577
pub linked_libs: &'a [String],
7678
pub linked_paths: &'a [String],
7779
pub cfgs: &'a [String],
@@ -87,7 +89,7 @@ impl<'a> Message for BuildScript<'a> {
8789

8890
#[derive(Serialize)]
8991
pub struct TimingInfo<'a> {
90-
pub package_id: PackageId,
92+
pub package_id: PackageIdSpec,
9193
pub target: &'a Target,
9294
pub mode: CompileMode,
9395
pub duration: f64,

src/doc/man/cargo-metadata.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,6 @@ The JSON output has the following format:
334334
Notes:
335335
- For `"id"` field syntax, see [Package ID Specifications] in the reference.
336336

337-
[Package ID Specifications]: ../reference/pkgid-spec.html
338-
339337
## OPTIONS
340338

341339
### Output Options
@@ -393,6 +391,8 @@ reproduction of the information within `Cargo.toml`.
393391
cargo metadata --format-version=1
394392
395393
## SEE ALSO
396-
{{man "cargo" 1}}, {{man "cargo-pkgid" 1}}, [Package ID Specifications]
394+
395+
{{man "cargo" 1}}, {{man "cargo-pkgid" 1}}, [Package ID Specifications], [JSON messages]
397396
398397
[Package ID Specifications]: ../reference/pkgid-spec.html
398+
[JSON messages]: ../reference/external-tools.html#json-messages

src/doc/man/cargo-pkgid.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ fetched.
2121

2222
A package specifier consists of a name, version, and source URL. You are
2323
allowed to use partial specifiers to succinctly match a specific package as
24-
long as it matches only one package. The format of a _spec_ can be one of the
25-
following:
24+
long as it matches only one package. This specifier is also used by other parts
25+
in Cargo, such as {{man "cargo-metadata" 1}} and [JSON messages] emitted by Cargo.
26+
27+
The format of a _spec_ can be one of the following:
2628

2729
SPEC Structure | Example SPEC
2830
---------------------------|--------------
@@ -88,6 +90,9 @@ Get the package ID for the given package instead of the current package.
8890
cargo pkgid file:///path/to/local/package#foo
8991

9092
## SEE ALSO
91-
{{man "cargo" 1}}, {{man "cargo-generate-lockfile" 1}}, {{man "cargo-metadata" 1}}, [Package ID Specifications]
93+
94+
{{man "cargo" 1}}, {{man "cargo-generate-lockfile" 1}}, {{man "cargo-metadata" 1}},
95+
[Package ID Specifications], [JSON messages]
9296

9397
[Package ID Specifications]: ../reference/pkgid-spec.html
98+
[JSON messages]: ../reference/external-tools.html#json-messages

src/doc/man/generated_txt/cargo-metadata.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -483,5 +483,7 @@ EXAMPLES
483483

484484
SEE ALSO
485485
cargo(1), cargo-pkgid(1), Package ID Specifications
486-
<https://doc.rust-lang.org/cargo/reference/pkgid-spec.html>
486+
<https://doc.rust-lang.org/cargo/reference/pkgid-spec.html>, JSON
487+
messages
488+
<https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
487489

src/doc/man/generated_txt/cargo-pkgid.txt

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ DESCRIPTION
1818

1919
A package specifier consists of a name, version, and source URL. You are
2020
allowed to use partial specifiers to succinctly match a specific package
21-
as long as it matches only one package. The format of a spec can be one
22-
of the following:
21+
as long as it matches only one package. This specifier is also used by
22+
other parts in Cargo, such as cargo-metadata(1) and JSON messages
23+
<https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
24+
emitted by Cargo.
25+
26+
The format of a spec can be one of the following:
2327

2428
+-----------------+--------------------------------------------------+
2529
| SPEC Structure | Example SPEC |
@@ -172,5 +176,7 @@ EXAMPLES
172176
SEE ALSO
173177
cargo(1), cargo-generate-lockfile(1), cargo-metadata(1), Package ID
174178
Specifications
175-
<https://doc.rust-lang.org/cargo/reference/pkgid-spec.html>
179+
<https://doc.rust-lang.org/cargo/reference/pkgid-spec.html>, JSON
180+
messages
181+
<https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
176182

src/doc/src/commands/cargo-metadata.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,6 @@ The JSON output has the following format:
334334
Notes:
335335
- For `"id"` field syntax, see [Package ID Specifications] in the reference.
336336

337-
[Package ID Specifications]: ../reference/pkgid-spec.html
338-
339337
## OPTIONS
340338

341339
### Output Options
@@ -512,6 +510,8 @@ details on environment variables that Cargo reads.
512510
cargo metadata --format-version=1
513511

514512
## SEE ALSO
515-
[cargo(1)](cargo.html), [cargo-pkgid(1)](cargo-pkgid.html), [Package ID Specifications]
513+
514+
[cargo(1)](cargo.html), [cargo-pkgid(1)](cargo-pkgid.html), [Package ID Specifications], [JSON messages]
516515

517516
[Package ID Specifications]: ../reference/pkgid-spec.html
517+
[JSON messages]: ../reference/external-tools.html#json-messages

src/doc/src/commands/cargo-pkgid.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ fetched.
2121

2222
A package specifier consists of a name, version, and source URL. You are
2323
allowed to use partial specifiers to succinctly match a specific package as
24-
long as it matches only one package. The format of a _spec_ can be one of the
25-
following:
24+
long as it matches only one package. This specifier is also used by other parts
25+
in Cargo, such as [cargo-metadata(1)](cargo-metadata.html) and [JSON messages] emitted by Cargo.
26+
27+
The format of a _spec_ can be one of the following:
2628

2729
SPEC Structure | Example SPEC
2830
---------------------------|--------------
@@ -183,6 +185,9 @@ details on environment variables that Cargo reads.
183185
cargo pkgid file:///path/to/local/package#foo
184186

185187
## SEE ALSO
186-
[cargo(1)](cargo.html), [cargo-generate-lockfile(1)](cargo-generate-lockfile.html), [cargo-metadata(1)](cargo-metadata.html), [Package ID Specifications]
188+
189+
[cargo(1)](cargo.html), [cargo-generate-lockfile(1)](cargo-generate-lockfile.html), [cargo-metadata(1)](cargo-metadata.html),
190+
[Package ID Specifications], [JSON messages]
187191

188192
[Package ID Specifications]: ../reference/pkgid-spec.html
193+
[JSON messages]: ../reference/external-tools.html#json-messages

src/doc/src/reference/external-tools.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ information during the build:
4242

4343
The output goes to stdout in the JSON object per line format. The `reason` field
4444
distinguishes different kinds of messages.
45+
The `package_id` field is a unique identifier for referring to the package, and
46+
as the `--package` argument to many commands. The syntax grammar can be found in
47+
chapter [Package ID Specifications].
4548

4649
The `--message-format` option can also take additional formatting values which
4750
alter the way the JSON messages are computed and rendered. See the description
@@ -53,6 +56,7 @@ messages.
5356

5457
[build command documentation]: ../commands/cargo-build.md
5558
[cargo_metadata]: https://crates.io/crates/cargo_metadata
59+
[Package ID Specifications]: ./pkgid-spec.md
5660

5761
### Compiler messages
5862

@@ -66,7 +70,7 @@ structure:
6670
/* The "reason" indicates the kind of message. */
6771
"reason": "compiler-message",
6872
/* The Package ID, a unique identifier for referring to the package. */
69-
"package_id": "my-package 0.1.0 (path+file:///path/to/my-package)",
73+
"package_id": "file:///path/to/my-package#0.1.0",
7074
/* Absolute path to the package manifest. */
7175
"manifest_path": "/path/to/my-package/Cargo.toml",
7276
/* The Cargo target (lib, bin, example, etc.) that generated the message. */
@@ -135,7 +139,7 @@ following structure:
135139
/* The "reason" indicates the kind of message. */
136140
"reason": "compiler-artifact",
137141
/* The Package ID, a unique identifier for referring to the package. */
138-
"package_id": "my-package 0.1.0 (path+file:///path/to/my-package)",
142+
"package_id": "file:///path/to/my-package#0.1.0",
139143
/* Absolute path to the package manifest. */
140144
"manifest_path": "/path/to/my-package/Cargo.toml",
141145
/* The Cargo target (lib, bin, example, etc.) that generated the artifacts.
@@ -204,7 +208,7 @@ may be found in [the chapter on build scripts](build-scripts.md).
204208
/* The "reason" indicates the kind of message. */
205209
"reason": "build-script-executed",
206210
/* The Package ID, a unique identifier for referring to the package. */
207-
"package_id": "my-package 0.1.0 (path+file:///path/to/my-package)",
211+
"package_id": "file:///path/to/my-package#0.1.0",
208212
/* Array of libraries to link, as indicated by the `cargo::rustc-link-lib`
209213
instruction. Note that this may include a "KIND=" prefix in the string
210214
where KIND is the library kind.

src/etc/man/cargo-metadata.1

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ The JSON output has the following format:
5555
"name": "my\-package",
5656
/* The version of the package. */
5757
"version": "0.1.0",
58-
/* The Package ID, an opaque and unique identifier for referring to the
59-
package. See "Compatibility" above for the stability guarantee.
58+
/* The Package ID for referring to the
59+
package within the document and as the `\-\-package` argument to many commands
6060
*/
6161
"id": "file:///path/to/my\-package#0.1.0",
6262
/* The license value from the manifest, or null. */
@@ -529,4 +529,4 @@ cargo metadata \-\-format\-version=1
529529
.RE
530530
.RE
531531
.SH "SEE ALSO"
532-
\fBcargo\fR(1), \fBcargo\-pkgid\fR(1), \fIPackage ID Specifications\fR <https://doc.rust\-lang.org/cargo/reference/pkgid\-spec.html>
532+
\fBcargo\fR(1), \fBcargo\-pkgid\fR(1), \fIPackage ID Specifications\fR <https://doc.rust\-lang.org/cargo/reference/pkgid\-spec.html>, \fIJSON messages\fR <https://doc.rust\-lang.org/cargo/reference/external\-tools.html#json\-messages>

src/etc/man/cargo-pkgid.1

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ fetched.
1919
.sp
2020
A package specifier consists of a name, version, and source URL. You are
2121
allowed to use partial specifiers to succinctly match a specific package as
22-
long as it matches only one package. The format of a \fIspec\fR can be one of the
23-
following:
22+
long as it matches only one package. This specifier is also used by other parts
23+
in Cargo, such as \fBcargo\-metadata\fR(1) and \fIJSON messages\fR <https://doc.rust\-lang.org/cargo/reference/external\-tools.html#json\-messages> emitted by Cargo.
24+
.sp
25+
The format of a \fIspec\fR can be one of the following:
2426

2527
.TS
2628
allbox tab(:);
@@ -242,4 +244,5 @@ cargo pkgid file:///path/to/local/package#foo
242244
.RE
243245
.RE
244246
.SH "SEE ALSO"
245-
\fBcargo\fR(1), \fBcargo\-generate\-lockfile\fR(1), \fBcargo\-metadata\fR(1), \fIPackage ID Specifications\fR <https://doc.rust\-lang.org/cargo/reference/pkgid\-spec.html>
247+
\fBcargo\fR(1), \fBcargo\-generate\-lockfile\fR(1), \fBcargo\-metadata\fR(1),
248+
\fIPackage ID Specifications\fR <https://doc.rust\-lang.org/cargo/reference/pkgid\-spec.html>, \fIJSON messages\fR <https://doc.rust\-lang.org/cargo/reference/external\-tools.html#json\-messages>

tests/testsuite/bench.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ fn json_artifact_includes_executable_for_benchmark() {
16491649
"features": [],
16501650
"filenames": "{...}",
16511651
"fresh": false,
1652-
"package_id": "foo 0.0.1 ([..])",
1652+
"package_id": "path+file:///[..]/foo#0.0.1",
16531653
"manifest_path": "[..]",
16541654
"profile": "{...}",
16551655
"reason": "compiler-artifact",

tests/testsuite/binary_name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn check_msg_format_json() {
280280
let output = r#"
281281
{
282282
"reason": "compiler-artifact",
283-
"package_id": "foo 0.0.1 [..]",
283+
"package_id": "path+file:///[..]/foo#0.0.1",
284284
"manifest_path": "[CWD]/Cargo.toml",
285285
"target": "{...}",
286286
"profile": "{...}",

tests/testsuite/build.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -4160,7 +4160,7 @@ fn compiler_json_error_format() {
41604160
r#"
41614161
{
41624162
"reason":"compiler-artifact",
4163-
"package_id":"foo 0.5.0 ([..])",
4163+
"package_id":"path+file:///[..]/foo#0.5.0",
41644164
"manifest_path": "[..]",
41654165
"target":{
41664166
"kind":["custom-build"],
@@ -4187,7 +4187,7 @@ fn compiler_json_error_format() {
41874187
41884188
{
41894189
"reason":"compiler-message",
4190-
"package_id":"bar 0.5.0 ([..])",
4190+
"package_id":"path+file:///[..]/bar#0.5.0",
41914191
"manifest_path": "[..]",
41924192
"target":{
41934193
"kind":["lib"],
@@ -4213,7 +4213,7 @@ fn compiler_json_error_format() {
42134213
},
42144214
"executable": null,
42154215
"features": [],
4216-
"package_id":"bar 0.5.0 ([..])",
4216+
"package_id":"path+file:///[..]/bar#0.5.0",
42174217
"manifest_path": "[..]",
42184218
"target":{
42194219
"kind":["lib"],
@@ -4234,7 +4234,7 @@ fn compiler_json_error_format() {
42344234
42354235
{
42364236
"reason":"build-script-executed",
4237-
"package_id":"foo 0.5.0 ([..])",
4237+
"package_id":"path+file:///[..]/foo#0.5.0",
42384238
"linked_libs":[],
42394239
"linked_paths":[],
42404240
"env":[],
@@ -4244,7 +4244,7 @@ fn compiler_json_error_format() {
42444244
42454245
{
42464246
"reason":"compiler-message",
4247-
"package_id":"foo 0.5.0 ([..])",
4247+
"package_id":"path+file:///[..]/foo#0.5.0",
42484248
"manifest_path": "[..]",
42494249
"target":{
42504250
"kind":["bin"],
@@ -4261,7 +4261,7 @@ fn compiler_json_error_format() {
42614261
42624262
{
42634263
"reason":"compiler-artifact",
4264-
"package_id":"foo 0.5.0 ([..])",
4264+
"package_id":"path+file:///[..]/foo#0.5.0",
42654265
"manifest_path": "[..]",
42664266
"target":{
42674267
"kind":["bin"],
@@ -4332,7 +4332,7 @@ fn message_format_json_forward_stderr() {
43324332
r#"
43334333
{
43344334
"reason":"compiler-message",
4335-
"package_id":"foo 0.5.0 ([..])",
4335+
"package_id":"path+file:///[..]/foo#0.5.0",
43364336
"manifest_path": "[..]",
43374337
"target":{
43384338
"kind":["bin"],
@@ -4349,7 +4349,7 @@ fn message_format_json_forward_stderr() {
43494349
43504350
{
43514351
"reason":"compiler-artifact",
4352-
"package_id":"foo 0.5.0 ([..])",
4352+
"package_id":"path+file:///[..]/foo#0.5.0",
43534353
"manifest_path": "[..]",
43544354
"target":{
43554355
"kind":["bin"],

0 commit comments

Comments
 (0)