Skip to content

Commit a456c22

Browse files
committed
fix: Apply path::normalize_path to targets in toml::normalize_toml phase
1 parent f8df39b commit a456c22

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::core::summary::MissingDependencyError;
99
use crate::AlreadyPrintedError;
1010
use anyhow::{anyhow, bail, Context as _};
1111
use cargo_platform::Platform;
12-
use cargo_util::paths::{self, normalize_path};
12+
use cargo_util::paths;
1313
use cargo_util_schemas::manifest::{
1414
self, PackageName, PathBaseName, TomlDependency, TomlDetailedDependency, TomlManifest,
1515
};
@@ -2712,7 +2712,7 @@ fn prepare_toml_for_publish(
27122712
let mut package = me.package().unwrap().clone();
27132713
package.workspace = None;
27142714
if let Some(StringOrBool::String(path)) = &package.build {
2715-
let path = paths::normalize_path(Path::new(path));
2715+
let path = Path::new(path).to_path_buf();
27162716
let included = packaged_files.map(|i| i.contains(&path)).unwrap_or(true);
27172717
let build = if included {
27182718
let path = path
@@ -3017,7 +3017,7 @@ pub fn prepare_target_for_publish(
30173017
gctx: &GlobalContext,
30183018
) -> CargoResult<Option<manifest::TomlTarget>> {
30193019
let path = target.path.as_ref().expect("previously normalized");
3020-
let path = normalize_path(&path.0);
3020+
let path = &path.0;
30213021
if let Some(packaged_files) = packaged_files {
30223022
if !packaged_files.contains(&path) {
30233023
let name = target.name.as_ref().expect("previously normalized");
@@ -3030,7 +3030,7 @@ pub fn prepare_target_for_publish(
30303030
}
30313031

30323032
let mut target = target.clone();
3033-
let path = normalize_path_sep(path, context)?;
3033+
let path = normalize_path_sep(path.to_path_buf(), context)?;
30343034
target.path = Some(manifest::PathValue(path.into()));
30353035

30363036
Ok(Some(target))

src/cargo/util/toml/targets.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::fs::{self, DirEntry};
1515
use std::path::{Path, PathBuf};
1616

1717
use anyhow::Context as _;
18+
use cargo_util::paths;
1819
use cargo_util_schemas::manifest::{
1920
PathValue, StringOrBool, StringOrVec, TomlBenchTarget, TomlBinTarget, TomlExampleTarget,
2021
TomlLibTarget, TomlManifest, TomlTarget, TomlTestTarget,
@@ -133,7 +134,7 @@ pub fn normalize_lib(
133134
warnings: &mut Vec<String>,
134135
) -> CargoResult<Option<TomlLibTarget>> {
135136
if is_normalized(original_lib, autodiscover) {
136-
let Some(lib) = original_lib.cloned() else {
137+
let Some(mut lib) = original_lib.cloned() else {
137138
return Ok(None);
138139
};
139140

@@ -143,6 +144,10 @@ pub fn normalize_lib(
143144
validate_proc_macro(&lib, "library", edition, warnings)?;
144145
validate_crate_types(&lib, "library", edition, warnings)?;
145146

147+
if let Some(PathValue(path)) = &lib.path {
148+
lib.path = Some(PathValue(paths::normalize_path(path).into()));
149+
}
150+
146151
Ok(Some(lib))
147152
} else {
148153
let inferred = inferred_lib(package_root);
@@ -184,6 +189,10 @@ pub fn normalize_lib(
184189
}
185190
}
186191

192+
if let Some(PathValue(path)) = lib.path.as_ref() {
193+
lib.path = Some(PathValue(paths::normalize_path(&path).into()));
194+
}
195+
187196
Ok(Some(lib))
188197
}
189198
}
@@ -255,11 +264,15 @@ pub fn normalize_bins(
255264
has_lib: bool,
256265
) -> CargoResult<Vec<TomlBinTarget>> {
257266
if are_normalized(toml_bins, autodiscover) {
258-
let toml_bins = toml_bins.cloned().unwrap_or_default();
259-
for bin in &toml_bins {
267+
let mut toml_bins = toml_bins.cloned().unwrap_or_default();
268+
for bin in toml_bins.iter_mut() {
260269
validate_bin_name(bin, warnings)?;
261270
validate_bin_crate_types(bin, edition, warnings, errors)?;
262271
validate_bin_proc_macro(bin, edition, warnings, errors)?;
272+
273+
if let Some(PathValue(path)) = &bin.path {
274+
bin.path = Some(PathValue(paths::normalize_path(path).into()));
275+
}
263276
}
264277
Ok(toml_bins)
265278
} else {
@@ -300,7 +313,7 @@ pub fn normalize_bins(
300313
}
301314
});
302315
let path = match path {
303-
Ok(path) => path,
316+
Ok(path) => paths::normalize_path(&path).into(),
304317
Err(e) => anyhow::bail!("{}", e),
305318
};
306319
bin.path = Some(PathValue(path));
@@ -603,13 +616,17 @@ fn normalize_targets_with_legacy_path(
603616
autodiscover_flag_name: &str,
604617
) -> CargoResult<Vec<TomlTarget>> {
605618
if are_normalized(toml_targets, autodiscover) {
606-
let toml_targets = toml_targets.cloned().unwrap_or_default();
607-
for target in &toml_targets {
619+
let mut toml_targets = toml_targets.cloned().unwrap_or_default();
620+
for target in toml_targets.iter_mut() {
608621
// Check early to improve error messages
609622
validate_target_name(target, target_kind_human, target_kind, warnings)?;
610623

611624
validate_proc_macro(target, target_kind_human, edition, warnings)?;
612625
validate_crate_types(target, target_kind_human, edition, warnings)?;
626+
627+
if let Some(PathValue(path)) = &target.path {
628+
target.path = Some(PathValue(paths::normalize_path(path).into()));
629+
}
613630
}
614631
Ok(toml_targets)
615632
} else {
@@ -651,7 +668,7 @@ fn normalize_targets_with_legacy_path(
651668
continue;
652669
}
653670
};
654-
target.path = Some(PathValue(path));
671+
target.path = Some(PathValue(paths::normalize_path(&path).into()));
655672
result.push(target);
656673
}
657674
Ok(result)
@@ -1037,7 +1054,14 @@ pub fn normalize_build(build: Option<&StringOrBool>, package_root: &Path) -> Opt
10371054
}
10381055
}
10391056
// Explicitly no build script.
1040-
Some(StringOrBool::Bool(false)) | Some(StringOrBool::String(_)) => build.cloned(),
1057+
Some(StringOrBool::Bool(false)) => build.cloned(),
1058+
Some(StringOrBool::String(build_file)) => {
1059+
let build_file = paths::normalize_path(Path::new(build_file));
1060+
let build = build_file.into_os_string().into_string().expect(
1061+
"`build_file` started as a String and `normalize_path` shouldn't have changed that",
1062+
);
1063+
Some(StringOrBool::String(build))
1064+
}
10411065
Some(StringOrBool::Bool(true)) => Some(StringOrBool::String(BUILD_RS.to_owned())),
10421066
}
10431067
}

tests/testsuite/binary_name.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -413,27 +413,27 @@ fn targets_with_relative_path_in_workspace_members() {
413413
p.cargo("check")
414414
.with_stderr_data(str![[r#"
415415
...
416-
--> relative-bar/./build.rs:1:17
416+
--> relative-bar/build.rs:1:17
417417
...
418-
--> relative-bar/./src/lib.rs:1:4
418+
--> relative-bar/src/lib.rs:1:4
419419
...
420-
--> relative-bar/./src/main.rs:1:17
420+
--> relative-bar/src/main.rs:1:17
421421
...
422422
"#]])
423423
.run();
424424

425425
p.cargo("check --example example")
426426
.with_stderr_data(str![[r#"
427427
...
428-
--> relative-bar/./example.rs:1:17
428+
--> relative-bar/example.rs:1:17
429429
...
430430
"#]])
431431
.run();
432432

433433
p.cargo("check --test test")
434434
.with_stderr_data(str![[r#"
435435
...
436-
--> relative-bar/./test.rs:5:35
436+
--> relative-bar/test.rs:5:35
437437
...
438438
"#]])
439439
.run();
@@ -442,7 +442,7 @@ fn targets_with_relative_path_in_workspace_members() {
442442
p.cargo("check --bench bench")
443443
.with_stderr_data(str![[r#"
444444
...
445-
--> relative-bar/./bench.rs:7:58
445+
--> relative-bar/bench.rs:7:58
446446
...
447447
"#]])
448448
.run();
@@ -490,27 +490,27 @@ fn targets_with_relative_path_in_workspace_members() {
490490
p.cargo("check")
491491
.with_stderr_data(str![[r#"
492492
...
493-
--> relative-bar/./build.rs:1:17
493+
--> relative-bar/build.rs:1:17
494494
...
495-
--> relative-bar/./src/lib.rs:1:4
495+
--> relative-bar/src/lib.rs:1:4
496496
...
497-
--> relative-bar/./src/main.rs:1:17
497+
--> relative-bar/src/main.rs:1:17
498498
...
499499
"#]])
500500
.run();
501501

502502
p.cargo("check --example example")
503503
.with_stderr_data(str![[r#"
504504
...
505-
--> relative-bar/./example.rs:1:17
505+
--> relative-bar/example.rs:1:17
506506
...
507507
"#]])
508508
.run();
509509

510510
p.cargo("check --test test")
511511
.with_stderr_data(str![[r#"
512512
...
513-
--> relative-bar/./test.rs:5:35
513+
--> relative-bar/test.rs:5:35
514514
...
515515
"#]])
516516
.run();
@@ -519,7 +519,7 @@ fn targets_with_relative_path_in_workspace_members() {
519519
p.cargo("check --bench bench")
520520
.with_stderr_data(str![[r#"
521521
...
522-
--> relative-bar/./bench.rs:7:58
522+
--> relative-bar/bench.rs:7:58
523523
...
524524
"#]])
525525
.run();

0 commit comments

Comments
 (0)