From d71aea54f78a0cd45a838527012840c396e105c3 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Thu, 5 Sep 2024 14:51:38 +0800 Subject: [PATCH] fix: Nomarlize the relative path for all targets --- src/cargo/util/toml/mod.rs | 17 ++++++++++++++--- tests/testsuite/binary_name.rs | 24 ++++++++++++------------ tests/testsuite/metadata.rs | 5 ++++- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 44b3b6812c80..f72b9550b287 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1302,7 +1302,7 @@ pub fn to_real_manifest( // If we have no lib at all, use the inferred lib, if available. // If we have a lib with a path, we're done. // If we have a lib with no path, use the inferred lib or else the package name. - let targets = to_targets( + let mut targets = to_targets( &features, &original_toml, &normalized_toml, @@ -1312,6 +1312,13 @@ pub fn to_real_manifest( warnings, )?; + // Normalize the targets paths + for target in targets.iter_mut() { + if let TargetSourcePath::Path(path) = target.src_path() { + target.set_src_path(TargetSourcePath::Path(normalize_path(path))); + } + } + if targets.iter().all(|t| t.is_custom_build()) { bail!( "no targets specified in the manifest\n\ @@ -3020,8 +3027,12 @@ pub fn prepare_target_for_publish( context: &str, gctx: &GlobalContext, ) -> CargoResult> { - let path = target.path.as_ref().expect("previously normalized"); - let path = normalize_path(&path.0); + let path = target + .path + .as_ref() + .expect("previously normalized") + .0 + .clone(); if let Some(packaged_files) = packaged_files { if !packaged_files.contains(&path) { let name = target.name.as_ref().expect("previously normalized"); diff --git a/tests/testsuite/binary_name.rs b/tests/testsuite/binary_name.rs index cda38e23f50e..6639840b1523 100644 --- a/tests/testsuite/binary_name.rs +++ b/tests/testsuite/binary_name.rs @@ -414,7 +414,7 @@ fn targets_with_relative_path_in_workspace_members() { .with_stderr_data(str![[r#" [COMPILING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar) [WARNING] unused variable: `a` - --> relative-bar/./build.rs:1:17 + --> relative-bar/build.rs:1:17 | 1 | fn main() { let a = 1; } | ^ [HELP] if this is intentional, prefix it with an underscore: `_a` @@ -423,7 +423,7 @@ fn targets_with_relative_path_in_workspace_members() { [WARNING] `relative-bar` (build script) generated 1 warning [WARNING] function `a` is never used - --> relative-bar/./src/lib.rs:1:4 + --> relative-bar/src/lib.rs:1:4 | 1 | fn a() {} | ^ @@ -432,7 +432,7 @@ fn targets_with_relative_path_in_workspace_members() { [WARNING] `relative-bar` (lib) generated 1 warning [WARNING] unused variable: `a` - --> relative-bar/./src/main.rs:1:17 + --> relative-bar/src/main.rs:1:17 | 1 | fn main() { let a = 1; } | ^ [HELP] if this is intentional, prefix it with an underscore: `_a` @@ -448,7 +448,7 @@ fn targets_with_relative_path_in_workspace_members() { p.cargo("check --example example") .with_stderr_data(str![[r#" [WARNING] unused variable: `a` - --> relative-bar/./build.rs:1:17 + --> relative-bar/build.rs:1:17 | 1 | fn main() { let a = 1; } | ^ [HELP] if this is intentional, prefix it with an underscore: `_a` @@ -457,7 +457,7 @@ fn targets_with_relative_path_in_workspace_members() { [WARNING] `relative-bar` (build script) generated 1 warning [WARNING] function `a` is never used - --> relative-bar/./src/lib.rs:1:4 + --> relative-bar/src/lib.rs:1:4 | 1 | fn a() {} | ^ @@ -467,7 +467,7 @@ fn targets_with_relative_path_in_workspace_members() { [WARNING] `relative-bar` (lib) generated 1 warning [CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar) [WARNING] unused variable: `a` - --> relative-bar/./example.rs:1:17 + --> relative-bar/example.rs:1:17 | 1 | fn main() { let a = 1; } | ^ [HELP] if this is intentional, prefix it with an underscore: `_a` @@ -482,7 +482,7 @@ fn targets_with_relative_path_in_workspace_members() { p.cargo("check --test test").with_stderr_data(str![[r#" [WARNING] unused variable: `a` - --> relative-bar/./build.rs:1:17 + --> relative-bar/build.rs:1:17 | 1 | fn main() { let a = 1; } | ^ [HELP] if this is intentional, prefix it with an underscore: `_a` @@ -491,7 +491,7 @@ fn targets_with_relative_path_in_workspace_members() { [WARNING] `relative-bar` (build script) generated 1 warning [WARNING] function `a` is never used - --> relative-bar/./src/lib.rs:1:4 + --> relative-bar/src/lib.rs:1:4 | 1 | fn a() {} | ^ @@ -501,7 +501,7 @@ fn targets_with_relative_path_in_workspace_members() { [WARNING] `relative-bar` (lib) generated 1 warning [CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar) [WARNING] unused variable: `a` - --> relative-bar/./test.rs:5:35 + --> relative-bar/test.rs:5:35 | 5 | fn test_a() { let a = 1; } | ^ [HELP] if this is intentional, prefix it with an underscore: `_a` @@ -516,7 +516,7 @@ fn targets_with_relative_path_in_workspace_members() { if is_nightly() { p.cargo("check --bench bench").with_stderr_data(str![[r#" [WARNING] unused variable: `a` - --> relative-bar/./build.rs:1:17 + --> relative-bar/build.rs:1:17 | 1 | fn main() { let a = 1; } | ^ [HELP] if this is intentional, prefix it with an underscore: `_a` @@ -525,7 +525,7 @@ fn targets_with_relative_path_in_workspace_members() { [WARNING] `relative-bar` (build script) generated 1 warning [WARNING] function `a` is never used - --> relative-bar/./src/lib.rs:1:4 + --> relative-bar/src/lib.rs:1:4 | 1 | fn a() {} | ^ @@ -535,7 +535,7 @@ fn targets_with_relative_path_in_workspace_members() { [WARNING] `relative-bar` (lib) generated 1 warning [CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar) [WARNING] unused variable: `a` - --> relative-bar/./bench.rs:7:58 + --> relative-bar/bench.rs:7:58 | 7 | fn bench_a(_b: &mut test::Bencher) { let a = 1; } | ^ [HELP] if this is intentional, prefix it with an underscore: `_a` diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index e76796c0514c..5982b23f88bc 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -4295,7 +4295,10 @@ fn cargo_metadata_non_utf8() { .arg("--format-version") .arg("1") .with_stderr_data(str![[r#" -[ERROR] path contains invalid UTF-8 characters +[ERROR] failed to parse manifest at `[ROOT]/foo/�/Cargo.toml` + +Caused by: + non-UTF8 path for library "#]]) .with_status(101)