Skip to content

Commit 0b72c74

Browse files
committed
fix: Nomarlize the relative path for all targets
1 parent 9ef82a1 commit 0b72c74

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ pub fn to_real_manifest(
13021302
// If we have no lib at all, use the inferred lib, if available.
13031303
// If we have a lib with a path, we're done.
13041304
// If we have a lib with no path, use the inferred lib or else the package name.
1305-
let targets = to_targets(
1305+
let mut targets = to_targets(
13061306
&features,
13071307
&original_toml,
13081308
&normalized_toml,
@@ -1312,6 +1312,24 @@ pub fn to_real_manifest(
13121312
warnings,
13131313
)?;
13141314

1315+
// Normalize the targets paths
1316+
for target in targets.iter_mut() {
1317+
if let TargetSourcePath::Path(path) = target.src_path() {
1318+
use crate::core::TargetKind;
1319+
let mut path = normalize_path(path);
1320+
let context = match target.kind() {
1321+
TargetKind::Lib(_) => "library",
1322+
TargetKind::Bin => "binary",
1323+
TargetKind::Test => "test",
1324+
TargetKind::ExampleBin | TargetKind::ExampleLib(_) => "example",
1325+
TargetKind::Bench => "benchmark",
1326+
TargetKind::CustomBuild => "custom-build",
1327+
};
1328+
path = normalize_path_sep(path, context)?;
1329+
target.set_src_path(TargetSourcePath::Path(path));
1330+
}
1331+
}
1332+
13151333
if targets.iter().all(|t| t.is_custom_build()) {
13161334
bail!(
13171335
"no targets specified in the manifest\n\
@@ -3020,10 +3038,9 @@ pub fn prepare_target_for_publish(
30203038
context: &str,
30213039
gctx: &GlobalContext,
30223040
) -> CargoResult<Option<manifest::TomlTarget>> {
3023-
let path = target.path.as_ref().expect("previously normalized");
3024-
let path = normalize_path(&path.0);
3041+
let path = &target.path.as_ref().expect("previously normalized").0;
30253042
if let Some(packaged_files) = packaged_files {
3026-
if !packaged_files.contains(&path) {
3043+
if !packaged_files.contains(path) {
30273044
let name = target.name.as_ref().expect("previously normalized");
30283045
gctx.shell().warn(format!(
30293046
"ignoring {context} `{name}` as `{}` is not included in the published package",
@@ -3034,7 +3051,6 @@ pub fn prepare_target_for_publish(
30343051
}
30353052

30363053
let mut target = target.clone();
3037-
let path = normalize_path_sep(path, context)?;
30383054
target.path = Some(manifest::PathValue(path.into()));
30393055

30403056
Ok(Some(target))

tests/testsuite/binary_name.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ fn targets_with_relative_path_in_workspace_members() {
414414
.with_stderr_data(str![[r#"
415415
[COMPILING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
416416
[WARNING] unused variable: `a`
417-
--> relative-bar/./build.rs:1:17
417+
--> relative-bar/build.rs:1:17
418418
|
419419
1 | fn main() { let a = 1; }
420420
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -423,7 +423,7 @@ fn targets_with_relative_path_in_workspace_members() {
423423
424424
[WARNING] `relative-bar` (build script) generated 1 warning
425425
[WARNING] function `a` is never used
426-
--> relative-bar/./src/lib.rs:1:4
426+
--> relative-bar/src/lib.rs:1:4
427427
|
428428
1 | fn a() {}
429429
| ^
@@ -432,7 +432,7 @@ fn targets_with_relative_path_in_workspace_members() {
432432
433433
[WARNING] `relative-bar` (lib) generated 1 warning
434434
[WARNING] unused variable: `a`
435-
--> relative-bar/./src/main.rs:1:17
435+
--> relative-bar/src/main.rs:1:17
436436
|
437437
1 | fn main() { let a = 1; }
438438
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -448,7 +448,7 @@ fn targets_with_relative_path_in_workspace_members() {
448448
p.cargo("check --example example")
449449
.with_stderr_data(str![[r#"
450450
[WARNING] unused variable: `a`
451-
--> relative-bar/./build.rs:1:17
451+
--> relative-bar/build.rs:1:17
452452
|
453453
1 | fn main() { let a = 1; }
454454
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -457,7 +457,7 @@ fn targets_with_relative_path_in_workspace_members() {
457457
458458
[WARNING] `relative-bar` (build script) generated 1 warning
459459
[WARNING] function `a` is never used
460-
--> relative-bar/./src/lib.rs:1:4
460+
--> relative-bar/src/lib.rs:1:4
461461
|
462462
1 | fn a() {}
463463
| ^
@@ -467,7 +467,7 @@ fn targets_with_relative_path_in_workspace_members() {
467467
[WARNING] `relative-bar` (lib) generated 1 warning
468468
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
469469
[WARNING] unused variable: `a`
470-
--> relative-bar/./example.rs:1:17
470+
--> relative-bar/example.rs:1:17
471471
|
472472
1 | fn main() { let a = 1; }
473473
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -482,7 +482,7 @@ fn targets_with_relative_path_in_workspace_members() {
482482

483483
p.cargo("check --test test").with_stderr_data(str![[r#"
484484
[WARNING] unused variable: `a`
485-
--> relative-bar/./build.rs:1:17
485+
--> relative-bar/build.rs:1:17
486486
|
487487
1 | fn main() { let a = 1; }
488488
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -491,7 +491,7 @@ fn targets_with_relative_path_in_workspace_members() {
491491
492492
[WARNING] `relative-bar` (build script) generated 1 warning
493493
[WARNING] function `a` is never used
494-
--> relative-bar/./src/lib.rs:1:4
494+
--> relative-bar/src/lib.rs:1:4
495495
|
496496
1 | fn a() {}
497497
| ^
@@ -501,7 +501,7 @@ fn targets_with_relative_path_in_workspace_members() {
501501
[WARNING] `relative-bar` (lib) generated 1 warning
502502
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
503503
[WARNING] unused variable: `a`
504-
--> relative-bar/./test.rs:5:35
504+
--> relative-bar/test.rs:5:35
505505
|
506506
5 | fn test_a() { let a = 1; }
507507
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -516,7 +516,7 @@ fn targets_with_relative_path_in_workspace_members() {
516516
if is_nightly() {
517517
p.cargo("check --bench bench").with_stderr_data(str![[r#"
518518
[WARNING] unused variable: `a`
519-
--> relative-bar/./build.rs:1:17
519+
--> relative-bar/build.rs:1:17
520520
|
521521
1 | fn main() { let a = 1; }
522522
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -525,7 +525,7 @@ fn targets_with_relative_path_in_workspace_members() {
525525
526526
[WARNING] `relative-bar` (build script) generated 1 warning
527527
[WARNING] function `a` is never used
528-
--> relative-bar/./src/lib.rs:1:4
528+
--> relative-bar/src/lib.rs:1:4
529529
|
530530
1 | fn a() {}
531531
| ^
@@ -535,7 +535,7 @@ fn targets_with_relative_path_in_workspace_members() {
535535
[WARNING] `relative-bar` (lib) generated 1 warning
536536
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
537537
[WARNING] unused variable: `a`
538-
--> relative-bar/./bench.rs:7:58
538+
--> relative-bar/bench.rs:7:58
539539
|
540540
7 | fn bench_a(_b: &mut test::Bencher) { let a = 1; }
541541
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`

tests/testsuite/metadata.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4295,7 +4295,10 @@ fn cargo_metadata_non_utf8() {
42954295
.arg("--format-version")
42964296
.arg("1")
42974297
.with_stderr_data(str![[r#"
4298-
[ERROR] path contains invalid UTF-8 characters
4298+
[ERROR] failed to parse manifest at `[ROOT]/foo/�/Cargo.toml`
4299+
4300+
Caused by:
4301+
non-UTF8 path for library
42994302
43004303
"#]])
43014304
.with_status(101)

tests/testsuite/package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3506,7 +3506,7 @@ license = "MIT"
35063506
35073507
[lib]
35083508
name = "foo"
3509-
path = "src/lib.rs"
3509+
path = 'src/lib.rs'
35103510
"#,
35113511
cargo::core::manifest::MANIFEST_PREAMBLE
35123512
),

0 commit comments

Comments
 (0)