From ea8f33edc22b59d25882d435c141f2d52e674841 Mon Sep 17 00:00:00 2001 From: Lin Yihai <1161813899@qq.com> Date: Sun, 8 Sep 2024 23:04:07 +0800 Subject: [PATCH] fix: Normalize `target`s path --- src/cargo/util/toml/targets.rs | 16 ++++++++++------ tests/testsuite/binary_name.rs | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/cargo/util/toml/targets.rs b/src/cargo/util/toml/targets.rs index c80b31dba61..5b0cc0fc54c 100644 --- a/src/cargo/util/toml/targets.rs +++ b/src/cargo/util/toml/targets.rs @@ -15,6 +15,7 @@ use std::fs::{self, DirEntry}; use std::path::{Path, PathBuf}; use anyhow::Context as _; +use cargo_util::paths; use cargo_util_schemas::manifest::{ PathValue, StringOrBool, StringOrVec, TomlBenchTarget, TomlBinTarget, TomlExampleTarget, TomlLibTarget, TomlManifest, TomlTarget, TomlTestTarget, @@ -96,11 +97,9 @@ pub(super) fn to_targets( .and_then(|s| s.to_str()) .unwrap_or("") ); - targets.push(Target::custom_build_target( - &name, - package_root.join(custom_build), - edition, - )); + let path = package_root.join(custom_build); + let path = paths::normalize_path(&path); + targets.push(Target::custom_build_target(&name, path, edition)); } if let Some(metabuild) = metabuild { // Verify names match available build deps. @@ -169,6 +168,10 @@ pub fn normalize_lib( } } } + if let Some(PathValue(path)) = lib.path { + let path = paths::normalize_path(&path); + lib.path = Some(PathValue(path)); + } Ok(Some(lib)) } @@ -285,7 +288,7 @@ pub fn normalize_bins( } }); let path = match path { - Ok(path) => path, + Ok(path) => paths::normalize_path(&path), Err(e) => anyhow::bail!("{}", e), }; bin.path = Some(PathValue(path)); @@ -628,6 +631,7 @@ fn normalize_targets_with_legacy_path( continue; } }; + let path = paths::normalize_path(&path); target.path = Some(PathValue(path)); result.push(target); } diff --git a/tests/testsuite/binary_name.rs b/tests/testsuite/binary_name.rs index cda38e23f50..6639840b152 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`