Skip to content

Commit d544952

Browse files
committed
fix(package): Normalize path separators
A windows user could use `\` and no Linux or Mac user could use the package. This normalizes the separator to what works on all platforms.
1 parent 74e7796 commit d544952

File tree

2 files changed

+60
-30
lines changed

2 files changed

+60
-30
lines changed

src/cargo/util/toml/mod.rs

+52-22
Original file line numberDiff line numberDiff line change
@@ -2338,11 +2338,11 @@ fn prepare_toml_for_publish(
23382338
package.workspace = None;
23392339
if let Some(StringOrBool::String(path)) = &package.build {
23402340
let path = paths::normalize_path(Path::new(path));
2341-
package.build = Some(StringOrBool::String(
2341+
package.build = Some(StringOrBool::String(normalize_path_string_sep(
23422342
path.into_os_string()
23432343
.into_string()
23442344
.map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?,
2345-
));
2345+
)));
23462346
}
23472347
let current_resolver = package
23482348
.resolver
@@ -2372,10 +2372,12 @@ fn prepare_toml_for_publish(
23722372
let abs_license_path = paths::normalize_path(&package_root.join(license_path));
23732373
if let Ok(license_file) = abs_license_path.strip_prefix(package_root) {
23742374
package.license_file = Some(manifest::InheritableField::Value(
2375-
license_file
2376-
.to_str()
2377-
.ok_or_else(|| anyhow::format_err!("non-UTF8 `package.license-file`"))?
2378-
.to_owned(),
2375+
normalize_path_string_sep(
2376+
license_file
2377+
.to_str()
2378+
.ok_or_else(|| anyhow::format_err!("non-UTF8 `package.license-file`"))?
2379+
.to_owned(),
2380+
),
23792381
));
23802382
} else {
23812383
// This path points outside of the package root. `cargo package`
@@ -2401,10 +2403,14 @@ fn prepare_toml_for_publish(
24012403
let abs_readme_path = paths::normalize_path(&package_root.join(readme_path));
24022404
if let Ok(readme_path) = abs_readme_path.strip_prefix(package_root) {
24032405
package.readme = Some(manifest::InheritableField::Value(StringOrBool::String(
2404-
readme_path
2405-
.to_str()
2406-
.ok_or_else(|| anyhow::format_err!("non-UTF8 `package.license-file`"))?
2407-
.to_owned(),
2406+
normalize_path_string_sep(
2407+
readme_path
2408+
.to_str()
2409+
.ok_or_else(|| {
2410+
anyhow::format_err!("non-UTF8 `package.license-file`")
2411+
})?
2412+
.to_owned(),
2413+
),
24082414
)));
24092415
} else {
24102416
// This path points outside of the package root. `cargo package`
@@ -2426,14 +2432,14 @@ fn prepare_toml_for_publish(
24262432
}
24272433

24282434
let lib = if let Some(target) = &me.lib {
2429-
Some(prepare_target_for_publish(target))
2435+
Some(prepare_target_for_publish(target, "lib")?)
24302436
} else {
24312437
None
24322438
};
2433-
let bin = prepare_targets_for_publish(me.bin.as_ref());
2434-
let example = prepare_targets_for_publish(me.example.as_ref());
2435-
let test = prepare_targets_for_publish(me.test.as_ref());
2436-
let bench = prepare_targets_for_publish(me.bench.as_ref());
2439+
let bin = prepare_targets_for_publish(me.bin.as_ref(), "bin")?;
2440+
let example = prepare_targets_for_publish(me.example.as_ref(), "example")?;
2441+
let test = prepare_targets_for_publish(me.test.as_ref(), "test")?;
2442+
let bench = prepare_targets_for_publish(me.bench.as_ref(), "bench")?;
24372443

24382444
let all = |_d: &manifest::TomlDependency| true;
24392445
let mut manifest = manifest::TomlManifest {
@@ -2591,22 +2597,46 @@ fn prepare_toml_for_publish(
25912597

25922598
fn prepare_targets_for_publish(
25932599
targets: Option<&Vec<manifest::TomlTarget>>,
2594-
) -> Option<Vec<manifest::TomlTarget>> {
2595-
let targets = targets?;
2600+
context: &str,
2601+
) -> CargoResult<Option<Vec<manifest::TomlTarget>>> {
2602+
let Some(targets) = targets else {
2603+
return Ok(None);
2604+
};
25962605

25972606
let mut prepared = Vec::with_capacity(targets.len());
25982607
for target in targets {
2599-
let target = prepare_target_for_publish(target);
2608+
let target = prepare_target_for_publish(target, context)?;
26002609
prepared.push(target);
26012610
}
26022611

2603-
Some(prepared)
2612+
Ok(Some(prepared))
26042613
}
26052614

2606-
fn prepare_target_for_publish(target: &manifest::TomlTarget) -> manifest::TomlTarget {
2615+
fn prepare_target_for_publish(
2616+
target: &manifest::TomlTarget,
2617+
context: &str,
2618+
) -> CargoResult<manifest::TomlTarget> {
26072619
let mut target = target.clone();
26082620
if let Some(path) = target.path {
2609-
target.path = Some(manifest::PathValue(normalize_path(&path.0)));
2621+
let path = normalize_path(&path.0);
2622+
target.path = Some(manifest::PathValue(normalize_path_sep(path, context)?));
2623+
}
2624+
Ok(target)
2625+
}
2626+
2627+
fn normalize_path_sep(path: PathBuf, context: &str) -> CargoResult<PathBuf> {
2628+
let path = path
2629+
.into_os_string()
2630+
.into_string()
2631+
.map_err(|_err| anyhow::format_err!("non-UTF8 path for {context}"))?;
2632+
let path = normalize_path_string_sep(path);
2633+
Ok(path.into())
2634+
}
2635+
2636+
fn normalize_path_string_sep(path: String) -> String {
2637+
if std::path::MAIN_SEPARATOR != '/' {
2638+
path.replace(std::path::MAIN_SEPARATOR, "/")
2639+
} else {
2640+
path
26102641
}
2611-
target
26122642
}

tests/testsuite/package.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -3638,30 +3638,30 @@ edition = "2015"
36383638
name = "foo"
36393639
version = "0.0.1"
36403640
authors = []
3641-
build = 'src/build.rs'
3641+
build = "src/build.rs"
36423642
description = "foo"
36433643
documentation = "docs.rs/foo"
3644-
readme = 'docs/README.md'
3645-
license-file = 'docs/LICENSE'
3644+
readme = "docs/README.md"
3645+
license-file = "docs/LICENSE"
36463646
36473647
[lib]
3648-
path = 'src/lib.rs'
3648+
path = "src/lib.rs"
36493649
36503650
[[bin]]
36513651
name = "foo"
3652-
path = 'src/bin/foo/main.rs'
3652+
path = "src/bin/foo/main.rs"
36533653
36543654
[[example]]
36553655
name = "example_foo"
3656-
path = 'examples/example_foo.rs'
3656+
path = "examples/example_foo.rs"
36573657
36583658
[[test]]
36593659
name = "test_foo"
3660-
path = 'tests/test_foo.rs'
3660+
path = "tests/test_foo.rs"
36613661
36623662
[[bench]]
36633663
name = "bench_foo"
3664-
path = 'benches/bench_foo.rs'
3664+
path = "benches/bench_foo.rs"
36653665
"#,
36663666
)],
36673667
);

0 commit comments

Comments
 (0)