Skip to content

Commit f40216a

Browse files
committed
Apply path::normalize_path to targets in toml::normalize_toml phase
1 parent 0341a64 commit f40216a

File tree

4 files changed

+99
-39
lines changed

4 files changed

+99
-39
lines changed

src/cargo/ops/vendor.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ fn prepare_for_vendor(
413413
let contents = me.manifest().contents();
414414
let document = me.manifest().document();
415415
let original_toml = prepare_toml_for_vendor(
416+
me.manifest_path().parent().unwrap(),
416417
me.manifest().normalized_toml().clone(),
417418
packaged_files,
418419
gctx,
@@ -441,6 +442,7 @@ fn prepare_for_vendor(
441442
}
442443

443444
fn prepare_toml_for_vendor(
445+
package_root: &Path,
444446
mut me: cargo_util_schemas::manifest::TomlManifest,
445447
packaged_files: &[PathBuf],
446448
gctx: &GlobalContext,
@@ -472,6 +474,7 @@ fn prepare_toml_for_vendor(
472474
let lib = if let Some(target) = &me.lib {
473475
crate::util::toml::prepare_target_for_publish(
474476
target,
477+
package_root,
475478
Some(packaged_files),
476479
"library",
477480
gctx,
@@ -481,24 +484,28 @@ fn prepare_toml_for_vendor(
481484
};
482485
let bin = crate::util::toml::prepare_targets_for_publish(
483486
me.bin.as_ref(),
487+
package_root,
484488
Some(packaged_files),
485489
"binary",
486490
gctx,
487491
)?;
488492
let example = crate::util::toml::prepare_targets_for_publish(
489493
me.example.as_ref(),
494+
package_root,
490495
Some(packaged_files),
491496
"example",
492497
gctx,
493498
)?;
494499
let test = crate::util::toml::prepare_targets_for_publish(
495500
me.test.as_ref(),
501+
package_root,
496502
Some(packaged_files),
497503
"test",
498504
gctx,
499505
)?;
500506
let bench = crate::util::toml::prepare_targets_for_publish(
501507
me.bench.as_ref(),
508+
package_root,
502509
Some(packaged_files),
503510
"benchmark",
504511
gctx,

src/cargo/util/toml/mod.rs

Lines changed: 64 additions & 24 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
};
@@ -2813,16 +2813,38 @@ fn prepare_toml_for_publish(
28132813
}
28142814

28152815
let lib = if let Some(target) = &me.lib {
2816-
prepare_target_for_publish(target, packaged_files, "library", ws.gctx())?
2816+
prepare_target_for_publish(target, package_root, packaged_files, "library", ws.gctx())?
28172817
} else {
28182818
None
28192819
};
2820-
let bin = prepare_targets_for_publish(me.bin.as_ref(), packaged_files, "binary", ws.gctx())?;
2821-
let example =
2822-
prepare_targets_for_publish(me.example.as_ref(), packaged_files, "example", ws.gctx())?;
2823-
let test = prepare_targets_for_publish(me.test.as_ref(), packaged_files, "test", ws.gctx())?;
2824-
let bench =
2825-
prepare_targets_for_publish(me.bench.as_ref(), packaged_files, "benchmark", ws.gctx())?;
2820+
let bin = prepare_targets_for_publish(
2821+
me.bin.as_ref(),
2822+
package_root,
2823+
packaged_files,
2824+
"binary",
2825+
ws.gctx(),
2826+
)?;
2827+
let example = prepare_targets_for_publish(
2828+
me.example.as_ref(),
2829+
package_root,
2830+
packaged_files,
2831+
"example",
2832+
ws.gctx(),
2833+
)?;
2834+
let test = prepare_targets_for_publish(
2835+
me.test.as_ref(),
2836+
package_root,
2837+
packaged_files,
2838+
"test",
2839+
ws.gctx(),
2840+
)?;
2841+
let bench = prepare_targets_for_publish(
2842+
me.bench.as_ref(),
2843+
package_root,
2844+
packaged_files,
2845+
"benchmark",
2846+
ws.gctx(),
2847+
)?;
28262848

28272849
let all = |_d: &manifest::TomlDependency| true;
28282850
let mut manifest = manifest::TomlManifest {
@@ -2981,6 +3003,7 @@ fn prepare_toml_for_publish(
29813003

29823004
pub fn prepare_targets_for_publish(
29833005
targets: Option<&Vec<manifest::TomlTarget>>,
3006+
package_root: &Path,
29843007
packaged_files: Option<&[PathBuf]>,
29853008
context: &str,
29863009
gctx: &GlobalContext,
@@ -2991,7 +3014,8 @@ pub fn prepare_targets_for_publish(
29913014

29923015
let mut prepared = Vec::with_capacity(targets.len());
29933016
for target in targets {
2994-
let Some(target) = prepare_target_for_publish(target, packaged_files, context, gctx)?
3017+
let Some(target) =
3018+
prepare_target_for_publish(target, package_root, packaged_files, context, gctx)?
29953019
else {
29963020
continue;
29973021
};
@@ -3007,28 +3031,44 @@ pub fn prepare_targets_for_publish(
30073031

30083032
pub fn prepare_target_for_publish(
30093033
target: &manifest::TomlTarget,
3034+
package_root: &Path,
30103035
packaged_files: Option<&[PathBuf]>,
30113036
context: &str,
30123037
gctx: &GlobalContext,
30133038
) -> CargoResult<Option<manifest::TomlTarget>> {
3014-
let path = target.path.as_ref().expect("previously normalized");
3015-
let path = normalize_path(&path.0);
3016-
if let Some(packaged_files) = packaged_files {
3017-
if !packaged_files.contains(&path) {
3018-
let name = target.name.as_ref().expect("previously normalized");
3019-
gctx.shell().warn(format!(
3020-
"ignoring {context} `{name}` as `{}` is not included in the published package",
3021-
path.display()
3022-
))?;
3023-
return Ok(None);
3039+
let path = target
3040+
.path
3041+
.as_ref()
3042+
.expect("previously normalized")
3043+
.0
3044+
.clone();
3045+
3046+
if path.starts_with(package_root) {
3047+
let path: PathBuf = path.strip_prefix(package_root).unwrap().to_path_buf();
3048+
if let Some(packaged_files) = packaged_files {
3049+
if !packaged_files.contains(&path) {
3050+
let name = target.name.as_ref().expect("previously normalized");
3051+
gctx.shell().warn(format!(
3052+
"ignoring {context} `{name}` as `{}` is not included in the published package",
3053+
path.display()
3054+
))?;
3055+
return Ok(None);
3056+
}
30243057
}
3025-
}
30263058

3027-
let mut target = target.clone();
3028-
let path = normalize_path_sep(path, context)?;
3029-
target.path = Some(manifest::PathValue(path.into()));
3059+
let mut target = target.clone();
3060+
let path = normalize_path_sep(path, context)?;
3061+
target.path = Some(manifest::PathValue(path.into()));
30303062

3031-
Ok(Some(target))
3063+
Ok(Some(target))
3064+
} else {
3065+
let name = target.name.as_ref().expect("previously normalized");
3066+
gctx.shell().warn(format!(
3067+
"ignoring {context} `{name}` as `{}` is not included in the published package",
3068+
path.display()
3069+
))?;
3070+
return Ok(None);
3071+
}
30323072
}
30333073

30343074
fn normalize_path_sep(path: PathBuf, context: &str) -> CargoResult<PathBuf> {

src/cargo/util/toml/targets.rs

Lines changed: 16 additions & 3 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,
@@ -98,7 +99,7 @@ pub(super) fn to_targets(
9899
);
99100
targets.push(Target::custom_build_target(
100101
&name,
101-
package_root.join(custom_build),
102+
paths::normalize_path(package_root.join(custom_build).as_path()),
102103
edition,
103104
));
104105
}
@@ -184,6 +185,12 @@ pub fn normalize_lib(
184185
}
185186
}
186187

188+
if let Some(PathValue(path)) = lib.path.as_ref() {
189+
let path = package_root.join(path);
190+
let path = paths::normalize_path(&path);
191+
lib.path = Some(PathValue(path.into()));
192+
}
193+
187194
Ok(Some(lib))
188195
}
189196
}
@@ -300,7 +307,11 @@ pub fn normalize_bins(
300307
}
301308
});
302309
let path = match path {
303-
Ok(path) => path,
310+
Ok(path) => {
311+
let path = package_root.join(path);
312+
let path = paths::normalize_path(&path);
313+
path.into()
314+
}
304315
Err(e) => anyhow::bail!("{}", e),
305316
};
306317
bin.path = Some(PathValue(path));
@@ -651,7 +662,9 @@ fn normalize_targets_with_legacy_path(
651662
continue;
652663
}
653664
};
654-
target.path = Some(PathValue(path));
665+
let path = package_root.join(path);
666+
let path = paths::normalize_path(&path);
667+
target.path = Some(PathValue(path.into()));
655668
result.push(target);
656669
}
657670
Ok(result)

tests/testsuite/binary_name.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ fn targets_with_relative_path_in_workspace_members() {
414414
.with_stderr_data(str![[r#"
415415
[COMPILING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
416416
[WARNING] unused variable: `a`
417-
--> relative-bar/./build.rs:1:17
417+
--> relative-bar/build.rs:1:17
418418
|
419419
1 | fn main() { let a = 1; }
420420
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -423,7 +423,7 @@ fn targets_with_relative_path_in_workspace_members() {
423423
424424
[WARNING] `relative-bar` (build script) generated 1 warning
425425
[WARNING] function `a` is never used
426-
--> relative-bar/./src/lib.rs:1:4
426+
--> relative-bar/src/lib.rs:1:4
427427
|
428428
1 | fn a() {}
429429
| ^
@@ -432,7 +432,7 @@ fn targets_with_relative_path_in_workspace_members() {
432432
433433
[WARNING] `relative-bar` (lib) generated 1 warning
434434
[WARNING] unused variable: `a`
435-
--> relative-bar/./src/main.rs:1:17
435+
--> relative-bar/src/main.rs:1:17
436436
|
437437
1 | fn main() { let a = 1; }
438438
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -448,7 +448,7 @@ fn targets_with_relative_path_in_workspace_members() {
448448
p.cargo("check --example example")
449449
.with_stderr_data(str![[r#"
450450
[WARNING] unused variable: `a`
451-
--> relative-bar/./build.rs:1:17
451+
--> relative-bar/build.rs:1:17
452452
|
453453
1 | fn main() { let a = 1; }
454454
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -457,7 +457,7 @@ fn targets_with_relative_path_in_workspace_members() {
457457
458458
[WARNING] `relative-bar` (build script) generated 1 warning
459459
[WARNING] function `a` is never used
460-
--> relative-bar/./src/lib.rs:1:4
460+
--> relative-bar/src/lib.rs:1:4
461461
|
462462
1 | fn a() {}
463463
| ^
@@ -467,7 +467,7 @@ fn targets_with_relative_path_in_workspace_members() {
467467
[WARNING] `relative-bar` (lib) generated 1 warning
468468
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
469469
[WARNING] unused variable: `a`
470-
--> relative-bar/./example.rs:1:17
470+
--> relative-bar/example.rs:1:17
471471
|
472472
1 | fn main() { let a = 1; }
473473
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -482,7 +482,7 @@ fn targets_with_relative_path_in_workspace_members() {
482482

483483
p.cargo("check --test test").with_stderr_data(str![[r#"
484484
[WARNING] unused variable: `a`
485-
--> relative-bar/./build.rs:1:17
485+
--> relative-bar/build.rs:1:17
486486
|
487487
1 | fn main() { let a = 1; }
488488
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -491,7 +491,7 @@ fn targets_with_relative_path_in_workspace_members() {
491491
492492
[WARNING] `relative-bar` (build script) generated 1 warning
493493
[WARNING] function `a` is never used
494-
--> relative-bar/./src/lib.rs:1:4
494+
--> relative-bar/src/lib.rs:1:4
495495
|
496496
1 | fn a() {}
497497
| ^
@@ -501,7 +501,7 @@ fn targets_with_relative_path_in_workspace_members() {
501501
[WARNING] `relative-bar` (lib) generated 1 warning
502502
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
503503
[WARNING] unused variable: `a`
504-
--> relative-bar/./test.rs:5:35
504+
--> relative-bar/test.rs:5:35
505505
|
506506
5 | fn test_a() { let a = 1; }
507507
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -516,7 +516,7 @@ fn targets_with_relative_path_in_workspace_members() {
516516
if is_nightly() {
517517
p.cargo("check --bench bench").with_stderr_data(str![[r#"
518518
[WARNING] unused variable: `a`
519-
--> relative-bar/./build.rs:1:17
519+
--> relative-bar/build.rs:1:17
520520
|
521521
1 | fn main() { let a = 1; }
522522
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`
@@ -525,7 +525,7 @@ fn targets_with_relative_path_in_workspace_members() {
525525
526526
[WARNING] `relative-bar` (build script) generated 1 warning
527527
[WARNING] function `a` is never used
528-
--> relative-bar/./src/lib.rs:1:4
528+
--> relative-bar/src/lib.rs:1:4
529529
|
530530
1 | fn a() {}
531531
| ^
@@ -535,7 +535,7 @@ fn targets_with_relative_path_in_workspace_members() {
535535
[WARNING] `relative-bar` (lib) generated 1 warning
536536
[CHECKING] relative-bar v0.1.0 ([ROOT]/foo/relative-bar)
537537
[WARNING] unused variable: `a`
538-
--> relative-bar/./bench.rs:7:58
538+
--> relative-bar/bench.rs:7:58
539539
|
540540
7 | fn bench_a(_b: &mut test::Bencher) { let a = 1; }
541541
| ^ [HELP] if this is intentional, prefix it with an underscore: `_a`

0 commit comments

Comments
 (0)