Skip to content

Commit d050945

Browse files
committed
Auto merge of #14497 - linyihai:issue-14227, r=epage
Normalize the `target` paths ### What does this PR try to resolve? The `targets` path of the `normalized_toml` could be relative, which isn't user-friendly. What is this PR doing? This PR applys the `paths::normalize_path` to remove the relative part. Fixes #14227 ### How should we test and review this PR? Add a test originted from the issue, and fixing it in the next commit. ### Additional information
2 parents 765a6f1 + a456c22 commit d050945

File tree

3 files changed

+221
-12
lines changed

3 files changed

+221
-12
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::core::summary::MissingDependencyError;
99
use crate::AlreadyPrintedError;
1010
use anyhow::{anyhow, bail, Context as _};
1111
use cargo_platform::Platform;
12-
use cargo_util::paths::{self, normalize_path};
12+
use cargo_util::paths;
1313
use cargo_util_schemas::manifest::{
1414
self, PackageName, PathBaseName, TomlDependency, TomlDetailedDependency, TomlManifest,
1515
};
@@ -2712,7 +2712,7 @@ fn prepare_toml_for_publish(
27122712
let mut package = me.package().unwrap().clone();
27132713
package.workspace = None;
27142714
if let Some(StringOrBool::String(path)) = &package.build {
2715-
let path = paths::normalize_path(Path::new(path));
2715+
let path = Path::new(path).to_path_buf();
27162716
let included = packaged_files.map(|i| i.contains(&path)).unwrap_or(true);
27172717
let build = if included {
27182718
let path = path
@@ -3017,7 +3017,7 @@ pub fn prepare_target_for_publish(
30173017
gctx: &GlobalContext,
30183018
) -> CargoResult<Option<manifest::TomlTarget>> {
30193019
let path = target.path.as_ref().expect("previously normalized");
3020-
let path = normalize_path(&path.0);
3020+
let path = &path.0;
30213021
if let Some(packaged_files) = packaged_files {
30223022
if !packaged_files.contains(&path) {
30233023
let name = target.name.as_ref().expect("previously normalized");
@@ -3030,7 +3030,7 @@ pub fn prepare_target_for_publish(
30303030
}
30313031

30323032
let mut target = target.clone();
3033-
let path = normalize_path_sep(path, context)?;
3033+
let path = normalize_path_sep(path.to_path_buf(), context)?;
30343034
target.path = Some(manifest::PathValue(path.into()));
30353035

30363036
Ok(Some(target))

src/cargo/util/toml/targets.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::fs::{self, DirEntry};
1515
use std::path::{Path, PathBuf};
1616

1717
use anyhow::Context as _;
18+
use cargo_util::paths;
1819
use cargo_util_schemas::manifest::{
1920
PathValue, StringOrBool, StringOrVec, TomlBenchTarget, TomlBinTarget, TomlExampleTarget,
2021
TomlLibTarget, TomlManifest, TomlTarget, TomlTestTarget,
@@ -133,7 +134,7 @@ pub fn normalize_lib(
133134
warnings: &mut Vec<String>,
134135
) -> CargoResult<Option<TomlLibTarget>> {
135136
if is_normalized(original_lib, autodiscover) {
136-
let Some(lib) = original_lib.cloned() else {
137+
let Some(mut lib) = original_lib.cloned() else {
137138
return Ok(None);
138139
};
139140

@@ -143,6 +144,10 @@ pub fn normalize_lib(
143144
validate_proc_macro(&lib, "library", edition, warnings)?;
144145
validate_crate_types(&lib, "library", edition, warnings)?;
145146

147+
if let Some(PathValue(path)) = &lib.path {
148+
lib.path = Some(PathValue(paths::normalize_path(path).into()));
149+
}
150+
146151
Ok(Some(lib))
147152
} else {
148153
let inferred = inferred_lib(package_root);
@@ -184,6 +189,10 @@ pub fn normalize_lib(
184189
}
185190
}
186191

192+
if let Some(PathValue(path)) = lib.path.as_ref() {
193+
lib.path = Some(PathValue(paths::normalize_path(&path).into()));
194+
}
195+
187196
Ok(Some(lib))
188197
}
189198
}
@@ -255,11 +264,15 @@ pub fn normalize_bins(
255264
has_lib: bool,
256265
) -> CargoResult<Vec<TomlBinTarget>> {
257266
if are_normalized(toml_bins, autodiscover) {
258-
let toml_bins = toml_bins.cloned().unwrap_or_default();
259-
for bin in &toml_bins {
267+
let mut toml_bins = toml_bins.cloned().unwrap_or_default();
268+
for bin in toml_bins.iter_mut() {
260269
validate_bin_name(bin, warnings)?;
261270
validate_bin_crate_types(bin, edition, warnings, errors)?;
262271
validate_bin_proc_macro(bin, edition, warnings, errors)?;
272+
273+
if let Some(PathValue(path)) = &bin.path {
274+
bin.path = Some(PathValue(paths::normalize_path(path).into()));
275+
}
263276
}
264277
Ok(toml_bins)
265278
} else {
@@ -300,7 +313,7 @@ pub fn normalize_bins(
300313
}
301314
});
302315
let path = match path {
303-
Ok(path) => path,
316+
Ok(path) => paths::normalize_path(&path).into(),
304317
Err(e) => anyhow::bail!("{}", e),
305318
};
306319
bin.path = Some(PathValue(path));
@@ -603,13 +616,17 @@ fn normalize_targets_with_legacy_path(
603616
autodiscover_flag_name: &str,
604617
) -> CargoResult<Vec<TomlTarget>> {
605618
if are_normalized(toml_targets, autodiscover) {
606-
let toml_targets = toml_targets.cloned().unwrap_or_default();
607-
for target in &toml_targets {
619+
let mut toml_targets = toml_targets.cloned().unwrap_or_default();
620+
for target in toml_targets.iter_mut() {
608621
// Check early to improve error messages
609622
validate_target_name(target, target_kind_human, target_kind, warnings)?;
610623

611624
validate_proc_macro(target, target_kind_human, edition, warnings)?;
612625
validate_crate_types(target, target_kind_human, edition, warnings)?;
626+
627+
if let Some(PathValue(path)) = &target.path {
628+
target.path = Some(PathValue(paths::normalize_path(path).into()));
629+
}
613630
}
614631
Ok(toml_targets)
615632
} else {
@@ -651,7 +668,7 @@ fn normalize_targets_with_legacy_path(
651668
continue;
652669
}
653670
};
654-
target.path = Some(PathValue(path));
671+
target.path = Some(PathValue(paths::normalize_path(&path).into()));
655672
result.push(target);
656673
}
657674
Ok(result)
@@ -1037,7 +1054,14 @@ pub fn normalize_build(build: Option<&StringOrBool>, package_root: &Path) -> Opt
10371054
}
10381055
}
10391056
// Explicitly no build script.
1040-
Some(StringOrBool::Bool(false)) | Some(StringOrBool::String(_)) => build.cloned(),
1057+
Some(StringOrBool::Bool(false)) => build.cloned(),
1058+
Some(StringOrBool::String(build_file)) => {
1059+
let build_file = paths::normalize_path(Path::new(build_file));
1060+
let build = build_file.into_os_string().into_string().expect(
1061+
"`build_file` started as a String and `normalize_path` shouldn't have changed that",
1062+
);
1063+
Some(StringOrBool::String(build))
1064+
}
10411065
Some(StringOrBool::Bool(true)) => Some(StringOrBool::String(BUILD_RS.to_owned())),
10421066
}
10431067
}

tests/testsuite/binary_name.rs

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use cargo_test_support::install::assert_has_installed_exe;
44
use cargo_test_support::install::assert_has_not_installed_exe;
5+
use cargo_test_support::is_nightly;
56
use cargo_test_support::paths;
67
use cargo_test_support::prelude::*;
78
use cargo_test_support::project;
@@ -340,3 +341,187 @@ fn check_msg_format_json() {
340341
)
341342
.run();
342343
}
344+
345+
#[cargo_test]
346+
fn targets_with_relative_path_in_workspace_members() {
347+
let p = project()
348+
.file(
349+
"Cargo.toml",
350+
r#"
351+
[workspace]
352+
members = ["relative-bar"]
353+
resolver = "2"
354+
"#,
355+
)
356+
.file(
357+
"relative-bar/Cargo.toml",
358+
r#"
359+
[package]
360+
name = "relative-bar"
361+
version = "0.1.0"
362+
edition = "2021"
363+
364+
build = "./build.rs"
365+
366+
[[bin]]
367+
name = "bar"
368+
path = "./src/main.rs"
369+
370+
[lib]
371+
name = "lib"
372+
path = "./src/lib.rs"
373+
374+
[[example]]
375+
name = "example"
376+
path = "./example.rs"
377+
378+
[[test]]
379+
name = "test"
380+
path = "./test.rs"
381+
382+
[[bench]]
383+
name = "bench"
384+
path = "./bench.rs"
385+
"#,
386+
)
387+
.file("relative-bar/build.rs", "fn main() { let a = 1; }")
388+
.file("relative-bar/src/main.rs", "fn main() { let a = 1; }")
389+
.file("relative-bar/src/lib.rs", "fn a() {}")
390+
.file("relative-bar/example.rs", "fn main() { let a = 1; }")
391+
.file(
392+
"relative-bar/test.rs",
393+
r#"
394+
fn main() {}
395+
396+
#[test]
397+
fn test_a() { let a = 1; }
398+
"#,
399+
)
400+
.file(
401+
"relative-bar/bench.rs",
402+
r#"
403+
#![feature(test)]
404+
#[cfg(test)]
405+
extern crate test;
406+
407+
#[bench]
408+
fn bench_a(_b: &mut test::Bencher) { let a = 1; }
409+
"#,
410+
)
411+
.build();
412+
413+
p.cargo("check")
414+
.with_stderr_data(str![[r#"
415+
...
416+
--> relative-bar/build.rs:1:17
417+
...
418+
--> relative-bar/src/lib.rs:1:4
419+
...
420+
--> relative-bar/src/main.rs:1:17
421+
...
422+
"#]])
423+
.run();
424+
425+
p.cargo("check --example example")
426+
.with_stderr_data(str![[r#"
427+
...
428+
--> relative-bar/example.rs:1:17
429+
...
430+
"#]])
431+
.run();
432+
433+
p.cargo("check --test test")
434+
.with_stderr_data(str![[r#"
435+
...
436+
--> relative-bar/test.rs:5:35
437+
...
438+
"#]])
439+
.run();
440+
441+
if is_nightly() {
442+
p.cargo("check --bench bench")
443+
.with_stderr_data(str![[r#"
444+
...
445+
--> relative-bar/bench.rs:7:58
446+
...
447+
"#]])
448+
.run();
449+
}
450+
451+
// Disable Cargo target auto-discovery.
452+
p.change_file(
453+
"relative-bar/Cargo.toml",
454+
r#"
455+
[package]
456+
name = "relative-bar"
457+
version = "0.1.0"
458+
edition = "2021"
459+
460+
autolib = false
461+
autobins = false
462+
autoexamples = false
463+
autotests = false
464+
autobenches = false
465+
466+
build = "./build.rs"
467+
468+
[[bin]]
469+
name = "bar"
470+
path = "./src/main.rs"
471+
472+
[lib]
473+
name = "lib"
474+
path = "./src/lib.rs"
475+
476+
[[example]]
477+
name = "example"
478+
path = "./example.rs"
479+
480+
[[test]]
481+
name = "test"
482+
path = "./test.rs"
483+
484+
[[bench]]
485+
name = "bench"
486+
path = "./bench.rs"
487+
"#,
488+
);
489+
490+
p.cargo("check")
491+
.with_stderr_data(str![[r#"
492+
...
493+
--> relative-bar/build.rs:1:17
494+
...
495+
--> relative-bar/src/lib.rs:1:4
496+
...
497+
--> relative-bar/src/main.rs:1:17
498+
...
499+
"#]])
500+
.run();
501+
502+
p.cargo("check --example example")
503+
.with_stderr_data(str![[r#"
504+
...
505+
--> relative-bar/example.rs:1:17
506+
...
507+
"#]])
508+
.run();
509+
510+
p.cargo("check --test test")
511+
.with_stderr_data(str![[r#"
512+
...
513+
--> relative-bar/test.rs:5:35
514+
...
515+
"#]])
516+
.run();
517+
518+
if is_nightly() {
519+
p.cargo("check --bench bench")
520+
.with_stderr_data(str![[r#"
521+
...
522+
--> relative-bar/bench.rs:7:58
523+
...
524+
"#]])
525+
.run();
526+
}
527+
}

0 commit comments

Comments
 (0)