Skip to content

Commit 74e7796

Browse files
committed
fix(package): Normalize paths in published Cargo.toml
For now, this is more for visual consistency. However, this blocks #13713 as we need to be able to make these paths comparable to what is included in the package.
1 parent 4eea907 commit 74e7796

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::str::{self, FromStr};
88
use crate::AlreadyPrintedError;
99
use anyhow::{anyhow, bail, Context as _};
1010
use cargo_platform::Platform;
11-
use cargo_util::paths;
11+
use cargo_util::paths::{self, normalize_path};
1212
use cargo_util_schemas::manifest::{self, TomlManifest};
1313
use cargo_util_schemas::manifest::{RustVersion, StringOrBool};
1414
use itertools::Itertools;
@@ -2336,6 +2336,14 @@ fn prepare_toml_for_publish(
23362336

23372337
let mut package = me.package().unwrap().clone();
23382338
package.workspace = None;
2339+
if let Some(StringOrBool::String(path)) = &package.build {
2340+
let path = paths::normalize_path(Path::new(path));
2341+
package.build = Some(StringOrBool::String(
2342+
path.into_os_string()
2343+
.into_string()
2344+
.map_err(|_err| anyhow::format_err!("non-UTF8 `package.build`"))?,
2345+
));
2346+
}
23392347
let current_resolver = package
23402348
.resolver
23412349
.as_ref()
@@ -2362,7 +2370,14 @@ fn prepare_toml_for_publish(
23622370
.context("license file should have been resolved before `prepare_for_publish()`")?;
23632371
let license_path = Path::new(&license_file);
23642372
let abs_license_path = paths::normalize_path(&package_root.join(license_path));
2365-
if abs_license_path.strip_prefix(package_root).is_err() {
2373+
if let Ok(license_file) = abs_license_path.strip_prefix(package_root) {
2374+
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(),
2379+
));
2380+
} else {
23662381
// This path points outside of the package root. `cargo package`
23672382
// will copy it into the root, so adjust the path to this location.
23682383
package.license_file = Some(manifest::InheritableField::Value(
@@ -2384,7 +2399,14 @@ fn prepare_toml_for_publish(
23842399
manifest::StringOrBool::String(readme) => {
23852400
let readme_path = Path::new(&readme);
23862401
let abs_readme_path = paths::normalize_path(&package_root.join(readme_path));
2387-
if abs_readme_path.strip_prefix(package_root).is_err() {
2402+
if let Ok(readme_path) = abs_readme_path.strip_prefix(package_root) {
2403+
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(),
2408+
)));
2409+
} else {
23882410
// This path points outside of the package root. `cargo package`
23892411
// will copy it into the root, so adjust the path to this location.
23902412
package.readme = Some(manifest::InheritableField::Value(
@@ -2402,16 +2424,27 @@ fn prepare_toml_for_publish(
24022424
manifest::StringOrBool::Bool(_) => {}
24032425
}
24042426
}
2427+
2428+
let lib = if let Some(target) = &me.lib {
2429+
Some(prepare_target_for_publish(target))
2430+
} else {
2431+
None
2432+
};
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());
2437+
24052438
let all = |_d: &manifest::TomlDependency| true;
24062439
let mut manifest = manifest::TomlManifest {
24072440
package: Some(package),
24082441
project: None,
24092442
profile: me.profile.clone(),
2410-
lib: me.lib.clone(),
2411-
bin: me.bin.clone(),
2412-
example: me.example.clone(),
2413-
test: me.test.clone(),
2414-
bench: me.bench.clone(),
2443+
lib,
2444+
bin,
2445+
example,
2446+
test,
2447+
bench,
24152448
dependencies: map_deps(gctx, me.dependencies.as_ref(), all)?,
24162449
dev_dependencies: map_deps(
24172450
gctx,
@@ -2555,3 +2588,25 @@ fn prepare_toml_for_publish(
25552588
.map(manifest::InheritableDependency::Value)
25562589
}
25572590
}
2591+
2592+
fn prepare_targets_for_publish(
2593+
targets: Option<&Vec<manifest::TomlTarget>>,
2594+
) -> Option<Vec<manifest::TomlTarget>> {
2595+
let targets = targets?;
2596+
2597+
let mut prepared = Vec::with_capacity(targets.len());
2598+
for target in targets {
2599+
let target = prepare_target_for_publish(target);
2600+
prepared.push(target);
2601+
}
2602+
2603+
Some(prepared)
2604+
}
2605+
2606+
fn prepare_target_for_publish(target: &manifest::TomlTarget) -> manifest::TomlTarget {
2607+
let mut target = target.clone();
2608+
if let Some(path) = target.path {
2609+
target.path = Some(manifest::PathValue(normalize_path(&path.0)));
2610+
}
2611+
target
2612+
}

tests/testsuite/package.rs

Lines changed: 8 additions & 8 deletions
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)