Skip to content

Commit c694096

Browse files
committed
Auto merge of #9022 - nagisa:nagisa/manifest_path, r=alexcrichton
Add the path to the manifest in json output This allows consumers of the json messages to avoid guessing where exactly the package root is. Having access to the package root is difficult by virtue of requiring logic to guess its location by e.g. walking filesystem from the source file. This guessing logic becomes further complicated in presence of workspaces and nigh impossible to implement correctly in instances where artifacts end up produced from paths above the package root (e.g. `../foo.rs`). Since Cargo has access to this data in the first place, there doesn't seem to be much reason to force consumers to invent their own, possibly flawed, logic.
2 parents 99e714c + 548300b commit c694096

File tree

7 files changed

+54
-5
lines changed

7 files changed

+54
-5
lines changed

src/cargo/core/compiler/mod.rs

+38-5
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ fn compile<'cfg>(
175175
let work = if unit.show_warnings(bcx.config) {
176176
replay_output_cache(
177177
unit.pkg.package_id(),
178+
PathBuf::from(unit.pkg.manifest_path()),
178179
&unit.target,
179180
cx.files().message_cache_path(unit),
180181
cx.bcx.build_config.message_format,
@@ -219,6 +220,7 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
219220
// Prepare the native lib state (extra `-L` and `-l` flags).
220221
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
221222
let current_id = unit.pkg.package_id();
223+
let manifest_path = PathBuf::from(unit.pkg.manifest_path());
222224
let build_scripts = cx.build_scripts.get(unit).cloned();
223225

224226
// If we are a binary and the package also contains a library, then we
@@ -316,7 +318,16 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
316318
&target,
317319
mode,
318320
&mut |line| on_stdout_line(state, line, package_id, &target),
319-
&mut |line| on_stderr_line(state, line, package_id, &target, &mut output_options),
321+
&mut |line| {
322+
on_stderr_line(
323+
state,
324+
line,
325+
package_id,
326+
&manifest_path,
327+
&target,
328+
&mut output_options,
329+
)
330+
},
320331
)
321332
.map_err(verbose_if_simple_exit_code)
322333
.chain_err(|| format!("could not compile `{}`", name))?;
@@ -414,6 +425,7 @@ fn link_targets(cx: &mut Context<'_, '_>, unit: &Unit, fresh: bool) -> CargoResu
414425
let outputs = cx.outputs(unit)?;
415426
let export_dir = cx.files().export_dir();
416427
let package_id = unit.pkg.package_id();
428+
let manifest_path = PathBuf::from(unit.pkg.manifest_path());
417429
let profile = unit.profile;
418430
let unit_mode = unit.mode;
419431
let features = unit.features.iter().map(|s| s.to_string()).collect();
@@ -467,6 +479,7 @@ fn link_targets(cx: &mut Context<'_, '_>, unit: &Unit, fresh: bool) -> CargoResu
467479

468480
let msg = machine_message::Artifact {
469481
package_id,
482+
manifest_path,
470483
target: &target,
471484
profile: art_profile,
472485
features,
@@ -618,10 +631,10 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
618631
let name = unit.pkg.name().to_string();
619632
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
620633
let package_id = unit.pkg.package_id();
634+
let manifest_path = PathBuf::from(unit.pkg.manifest_path());
621635
let target = Target::clone(&unit.target);
622636
let mut output_options = OutputOptions::new(cx, unit);
623637
let script_metadata = cx.find_build_script_metadata(unit);
624-
625638
Ok(Work::new(move |state| {
626639
if let Some(script_metadata) = script_metadata {
627640
if let Some(output) = build_script_outputs.lock().unwrap().get(script_metadata) {
@@ -638,7 +651,16 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
638651
rustdoc
639652
.exec_with_streaming(
640653
&mut |line| on_stdout_line(state, line, package_id, &target),
641-
&mut |line| on_stderr_line(state, line, package_id, &target, &mut output_options),
654+
&mut |line| {
655+
on_stderr_line(
656+
state,
657+
line,
658+
package_id,
659+
&manifest_path,
660+
&target,
661+
&mut output_options,
662+
)
663+
},
642664
false,
643665
)
644666
.chain_err(|| format!("could not document `{}`", name))?;
@@ -1139,10 +1161,11 @@ fn on_stderr_line(
11391161
state: &JobState<'_>,
11401162
line: &str,
11411163
package_id: PackageId,
1164+
manifest_path: &std::path::Path,
11421165
target: &Target,
11431166
options: &mut OutputOptions,
11441167
) -> CargoResult<()> {
1145-
if on_stderr_line_inner(state, line, package_id, target, options)? {
1168+
if on_stderr_line_inner(state, line, package_id, manifest_path, target, options)? {
11461169
// Check if caching is enabled.
11471170
if let Some((path, cell)) = &mut options.cache_cell {
11481171
// Cache the output, which will be replayed later when Fresh.
@@ -1160,6 +1183,7 @@ fn on_stderr_line_inner(
11601183
state: &JobState<'_>,
11611184
line: &str,
11621185
package_id: PackageId,
1186+
manifest_path: &std::path::Path,
11631187
target: &Target,
11641188
options: &mut OutputOptions,
11651189
) -> CargoResult<bool> {
@@ -1300,6 +1324,7 @@ fn on_stderr_line_inner(
13001324
// indicating which package it came from and then emit it.
13011325
let msg = machine_message::FromCompiler {
13021326
package_id,
1327+
manifest_path,
13031328
target,
13041329
message: compiler_message,
13051330
}
@@ -1314,6 +1339,7 @@ fn on_stderr_line_inner(
13141339

13151340
fn replay_output_cache(
13161341
package_id: PackageId,
1342+
manifest_path: PathBuf,
13171343
target: &Target,
13181344
path: PathBuf,
13191345
format: MessageFormat,
@@ -1343,7 +1369,14 @@ fn replay_output_cache(
13431369
break;
13441370
}
13451371
let trimmed = line.trim_end_matches(&['\n', '\r'][..]);
1346-
on_stderr_line(state, trimmed, package_id, &target, &mut options)?;
1372+
on_stderr_line(
1373+
state,
1374+
trimmed,
1375+
package_id,
1376+
&manifest_path,
1377+
&target,
1378+
&mut options,
1379+
)?;
13471380
line.clear();
13481381
}
13491382
Ok(())

src/cargo/util/machine_message.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub trait Message: ser::Serialize {
2020
#[derive(Serialize)]
2121
pub struct FromCompiler<'a> {
2222
pub package_id: PackageId,
23+
pub manifest_path: &'a Path,
2324
pub target: &'a Target,
2425
pub message: Box<RawValue>,
2526
}
@@ -33,6 +34,7 @@ impl<'a> Message for FromCompiler<'a> {
3334
#[derive(Serialize)]
3435
pub struct Artifact<'a> {
3536
pub package_id: PackageId,
37+
pub manifest_path: PathBuf,
3638
pub target: &'a Target,
3739
pub profile: ArtifactProfile,
3840
pub features: Vec<String>,

tests/testsuite/bench.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,7 @@ fn json_artifact_includes_executable_for_benchmark() {
17361736
"filenames": "{...}",
17371737
"fresh": false,
17381738
"package_id": "foo 0.0.1 ([..])",
1739+
"manifest_path": "[..]",
17391740
"profile": "{...}",
17401741
"reason": "compiler-artifact",
17411742
"target": {

tests/testsuite/build.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3140,6 +3140,7 @@ fn compiler_json_error_format() {
31403140
{
31413141
"reason":"compiler-artifact",
31423142
"package_id":"foo 0.5.0 ([..])",
3143+
"manifest_path": "[..]",
31433144
"target":{
31443145
"kind":["custom-build"],
31453146
"crate_types":["bin"],
@@ -3166,6 +3167,7 @@ fn compiler_json_error_format() {
31663167
{
31673168
"reason":"compiler-message",
31683169
"package_id":"bar 0.5.0 ([..])",
3170+
"manifest_path": "[..]",
31693171
"target":{
31703172
"kind":["lib"],
31713173
"crate_types":["lib"],
@@ -3191,6 +3193,7 @@ fn compiler_json_error_format() {
31913193
"executable": null,
31923194
"features": [],
31933195
"package_id":"bar 0.5.0 ([..])",
3196+
"manifest_path": "[..]",
31943197
"target":{
31953198
"kind":["lib"],
31963199
"crate_types":["lib"],
@@ -3221,6 +3224,7 @@ fn compiler_json_error_format() {
32213224
{
32223225
"reason":"compiler-message",
32233226
"package_id":"foo 0.5.0 ([..])",
3227+
"manifest_path": "[..]",
32243228
"target":{
32253229
"kind":["bin"],
32263230
"crate_types":["bin"],
@@ -3237,6 +3241,7 @@ fn compiler_json_error_format() {
32373241
{
32383242
"reason":"compiler-artifact",
32393243
"package_id":"foo 0.5.0 ([..])",
3244+
"manifest_path": "[..]",
32403245
"target":{
32413246
"kind":["bin"],
32423247
"crate_types":["bin"],
@@ -3307,6 +3312,7 @@ fn message_format_json_forward_stderr() {
33073312
{
33083313
"reason":"compiler-message",
33093314
"package_id":"foo 0.5.0 ([..])",
3315+
"manifest_path": "[..]",
33103316
"target":{
33113317
"kind":["bin"],
33123318
"crate_types":["bin"],
@@ -3323,6 +3329,7 @@ fn message_format_json_forward_stderr() {
33233329
{
33243330
"reason":"compiler-artifact",
33253331
"package_id":"foo 0.5.0 ([..])",
3332+
"manifest_path": "[..]",
33263333
"target":{
33273334
"kind":["bin"],
33283335
"crate_types":["bin"],

tests/testsuite/doc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,7 @@ fn doc_message_format() {
14141414
"spans": "{...}"
14151415
},
14161416
"package_id": "foo [..]",
1417+
"manifest_path": "[..]",
14171418
"reason": "compiler-message",
14181419
"target": "{...}"
14191420
}

tests/testsuite/metabuild.rs

+2
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ fn metabuild_json_artifact() {
691691
"filenames": "{...}",
692692
"fresh": false,
693693
"package_id": "foo [..]",
694+
"manifest_path": "[..]",
694695
"profile": "{...}",
695696
"reason": "compiler-artifact",
696697
"target": {
@@ -743,6 +744,7 @@ fn metabuild_failed_build_json() {
743744
"spans": "{...}"
744745
},
745746
"package_id": "foo [..]",
747+
"manifest_path": "[..]",
746748
"reason": "compiler-message",
747749
"target": {
748750
"crate_types": [

tests/testsuite/test.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3536,6 +3536,7 @@ fn json_artifact_includes_test_flag() {
35363536
"executable": "[..]/foo-[..]",
35373537
"features": [],
35383538
"package_id":"foo 0.0.1 ([..])",
3539+
"manifest_path": "[..]",
35393540
"target":{
35403541
"kind":["lib"],
35413542
"crate_types":["lib"],
@@ -3572,6 +3573,7 @@ fn json_artifact_includes_executable_for_library_tests() {
35723573
"filenames": "{...}",
35733574
"fresh": false,
35743575
"package_id": "foo 0.0.1 ([..])",
3576+
"manifest_path": "[..]",
35753577
"profile": "{...}",
35763578
"reason": "compiler-artifact",
35773579
"target": {
@@ -3610,6 +3612,7 @@ fn json_artifact_includes_executable_for_integration_tests() {
36103612
"filenames": "{...}",
36113613
"fresh": false,
36123614
"package_id": "foo 0.0.1 ([..])",
3615+
"manifest_path": "[..]",
36133616
"profile": "{...}",
36143617
"reason": "compiler-artifact",
36153618
"target": {

0 commit comments

Comments
 (0)