Skip to content

Commit 2fba2ad

Browse files
committed
fix: Nomarlize the relative path for all targets
1 parent abefaea commit 2fba2ad

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/cargo/util/toml/mod.rs

+21-5
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

+12-12
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ fn targets_with_relative_path_in_workspace_members() {
413413
.with_stderr_data(str![[r#"
414414
[COMPILING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
415415
[WARNING] unused variable: `a`
416-
--> relative-bar/./build.rs:1:17
416+
--> relative-bar/build.rs:1:17
417417
|
418418
1 | fn main() { let a = 1; }
419419
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -422,7 +422,7 @@ fn targets_with_relative_path_in_workspace_members() {
422422
423423
[WARNING] `relative-bar` (build script) generated 1 warning
424424
[WARNING] function `a` is never used
425-
--> relative-bar/./src/lib.rs:1:4
425+
--> relative-bar/src/lib.rs:1:4
426426
|
427427
1 | fn a() {}
428428
| ^
@@ -431,7 +431,7 @@ fn targets_with_relative_path_in_workspace_members() {
431431
432432
[WARNING] `relative-bar` (lib) generated 1 warning
433433
[WARNING] unused variable: `a`
434-
--> relative-bar/./src/main.rs:1:17
434+
--> relative-bar/src/main.rs:1:17
435435
|
436436
1 | fn main() { let a = 1; }
437437
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -447,7 +447,7 @@ fn targets_with_relative_path_in_workspace_members() {
447447
p.cargo("check --example example")
448448
.with_stderr_data(str![[r#"
449449
[WARNING] unused variable: `a`
450-
--> relative-bar/./build.rs:1:17
450+
--> relative-bar/build.rs:1:17
451451
|
452452
1 | fn main() { let a = 1; }
453453
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -456,7 +456,7 @@ fn targets_with_relative_path_in_workspace_members() {
456456
457457
[WARNING] `relative-bar` (build script) generated 1 warning
458458
[WARNING] function `a` is never used
459-
--> relative-bar/./src/lib.rs:1:4
459+
--> relative-bar/src/lib.rs:1:4
460460
|
461461
1 | fn a() {}
462462
| ^
@@ -466,7 +466,7 @@ fn targets_with_relative_path_in_workspace_members() {
466466
[WARNING] `relative-bar` (lib) generated 1 warning
467467
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
468468
[WARNING] unused variable: `a`
469-
--> relative-bar/./example.rs:1:17
469+
--> relative-bar/example.rs:1:17
470470
|
471471
1 | fn main() { let a = 1; }
472472
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -481,7 +481,7 @@ fn targets_with_relative_path_in_workspace_members() {
481481

482482
p.cargo("check --test test").with_stderr_data(str![[r#"
483483
[WARNING] unused variable: `a`
484-
--> relative-bar/./build.rs:1:17
484+
--> relative-bar/build.rs:1:17
485485
|
486486
1 | fn main() { let a = 1; }
487487
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -490,7 +490,7 @@ fn targets_with_relative_path_in_workspace_members() {
490490
491491
[WARNING] `relative-bar` (build script) generated 1 warning
492492
[WARNING] function `a` is never used
493-
--> relative-bar/./src/lib.rs:1:4
493+
--> relative-bar/src/lib.rs:1:4
494494
|
495495
1 | fn a() {}
496496
| ^
@@ -500,7 +500,7 @@ fn targets_with_relative_path_in_workspace_members() {
500500
[WARNING] `relative-bar` (lib) generated 1 warning
501501
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
502502
[WARNING] unused variable: `a`
503-
--> relative-bar/./test.rs:5:35
503+
--> relative-bar/test.rs:5:35
504504
|
505505
5 | fn test_a() { let a = 1; }
506506
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -514,7 +514,7 @@ fn targets_with_relative_path_in_workspace_members() {
514514

515515
p.cargo("check --bench bench").with_stderr_data(str![[r#"
516516
[WARNING] unused variable: `a`
517-
--> relative-bar/./build.rs:1:17
517+
--> relative-bar/build.rs:1:17
518518
|
519519
1 | fn main() { let a = 1; }
520520
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -523,7 +523,7 @@ fn targets_with_relative_path_in_workspace_members() {
523523
524524
[WARNING] `relative-bar` (build script) generated 1 warning
525525
[WARNING] function `a` is never used
526-
--> relative-bar/./src/lib.rs:1:4
526+
--> relative-bar/src/lib.rs:1:4
527527
|
528528
1 | fn a() {}
529529
| ^
@@ -533,7 +533,7 @@ fn targets_with_relative_path_in_workspace_members() {
533533
[WARNING] `relative-bar` (lib) generated 1 warning
534534
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
535535
[WARNING] unused variable: `a`
536-
--> relative-bar/./bench.rs:7:58
536+
--> relative-bar/bench.rs:7:58
537537
|
538538
7 | fn bench_a(_b: &mut test::Bencher) { let a = 1; }
539539
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`

tests/testsuite/metadata.rs

+4-1
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)

0 commit comments

Comments
 (0)