Skip to content

Commit

Permalink
fix: Nomarlize the relative path for all targets
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Sep 7, 2024
1 parent 9ef82a1 commit 0b72c74
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
26 changes: 21 additions & 5 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1312,6 +1312,24 @@ pub fn to_real_manifest(
warnings,
)?;

// Normalize the targets paths
for target in targets.iter_mut() {
if let TargetSourcePath::Path(path) = target.src_path() {
use crate::core::TargetKind;
let mut path = normalize_path(path);
let context = match target.kind() {
TargetKind::Lib(_) => "library",
TargetKind::Bin => "binary",
TargetKind::Test => "test",
TargetKind::ExampleBin | TargetKind::ExampleLib(_) => "example",
TargetKind::Bench => "benchmark",
TargetKind::CustomBuild => "custom-build",
};
path = normalize_path_sep(path, context)?;
target.set_src_path(TargetSourcePath::Path(path));
}
}

if targets.iter().all(|t| t.is_custom_build()) {
bail!(
"no targets specified in the manifest\n\
Expand Down Expand Up @@ -3020,10 +3038,9 @@ pub fn prepare_target_for_publish(
context: &str,
gctx: &GlobalContext,
) -> CargoResult<Option<manifest::TomlTarget>> {
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;
if let Some(packaged_files) = packaged_files {
if !packaged_files.contains(&path) {
if !packaged_files.contains(path) {
let name = target.name.as_ref().expect("previously normalized");
gctx.shell().warn(format!(
"ignoring {context} `{name}` as `{}` is not included in the published package",
Expand All @@ -3034,7 +3051,6 @@ pub fn prepare_target_for_publish(
}

let mut target = target.clone();
let path = normalize_path_sep(path, context)?;
target.path = Some(manifest::PathValue(path.into()));

Ok(Some(target))
Expand Down
24 changes: 12 additions & 12 deletions tests/testsuite/binary_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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() {}
| ^
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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() {}
| ^
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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() {}
| ^
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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() {}
| ^
Expand All @@ -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`
Expand Down
5 changes: 4 additions & 1 deletion tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3506,7 +3506,7 @@ license = "MIT"
[lib]
name = "foo"
path = "src/lib.rs"
path = 'src/lib.rs'
"#,
cargo::core::manifest::MANIFEST_PREAMBLE
),
Expand Down

0 comments on commit 0b72c74

Please sign in to comment.