Skip to content

Commit acd2717

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 dd71cf4 commit acd2717

File tree

2 files changed

+69
-16
lines changed

2 files changed

+69
-16
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 61 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,30 @@ fn prepare_toml_for_publish(
24022424
manifest::StringOrBool::Bool(_) => {}
24032425
}
24042426
}
2427+
2428+
let lib = if let Some(mut target) = me.lib.clone() {
2429+
if let Some(path) = target.path {
2430+
target.path = Some(manifest::PathValue(normalize_path(&path.0)));
2431+
}
2432+
Some(target)
2433+
} else {
2434+
None
2435+
};
2436+
let bin = prepare_targets_for_publish(me.bin.as_ref());
2437+
let example = prepare_targets_for_publish(me.example.as_ref());
2438+
let test = prepare_targets_for_publish(me.test.as_ref());
2439+
let bench = prepare_targets_for_publish(me.bench.as_ref());
2440+
24052441
let all = |_d: &manifest::TomlDependency| true;
24062442
let mut manifest = manifest::TomlManifest {
24072443
package: Some(package),
24082444
project: None,
24092445
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(),
2446+
lib,
2447+
bin,
2448+
example,
2449+
test,
2450+
bench,
24152451
dependencies: map_deps(gctx, me.dependencies.as_ref(), all)?,
24162452
dev_dependencies: map_deps(
24172453
gctx,
@@ -2555,3 +2591,20 @@ fn prepare_toml_for_publish(
25552591
.map(manifest::InheritableDependency::Value)
25562592
}
25572593
}
2594+
2595+
fn prepare_targets_for_publish(
2596+
targets: Option<&Vec<manifest::TomlTarget>>,
2597+
) -> Option<Vec<manifest::TomlTarget>> {
2598+
let targets = targets?;
2599+
2600+
let mut prepared = Vec::with_capacity(targets.len());
2601+
for target in targets {
2602+
let mut target = target.clone();
2603+
if let Some(path) = target.path {
2604+
target.path = Some(manifest::PathValue(normalize_path(&path.0)));
2605+
}
2606+
prepared.push(target);
2607+
}
2608+
2609+
Some(prepared)
2610+
}

tests/testsuite/package.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,30 +3637,30 @@ edition = "2015"
36373637
name = "foo"
36383638
version = "0.0.1"
36393639
authors = []
3640-
build = './src/build.rs'
3640+
build = 'src/build.rs'
36413641
description = "foo"
36423642
documentation = "docs.rs/foo"
3643-
readme = './docs/README.md'
3644-
license-file = './docs/LICENSE'
3643+
readme = 'docs/README.md'
3644+
license-file = 'docs/LICENSE'
36453645
36463646
[lib]
3647-
path = './src/lib.rs'
3647+
path = 'src/lib.rs'
36483648
36493649
[[bin]]
36503650
name = "foo"
3651-
path = './src/bin/foo/main.rs'
3651+
path = 'src/bin/foo/main.rs'
36523652
36533653
[[example]]
36543654
name = "example_foo"
3655-
path = './examples/example_foo.rs'
3655+
path = 'examples/example_foo.rs'
36563656
36573657
[[test]]
36583658
name = "test_foo"
3659-
path = './tests/test_foo.rs'
3659+
path = 'tests/test_foo.rs'
36603660
36613661
[[bench]]
36623662
name = "bench_foo"
3663-
path = './benches/bench_foo.rs'
3663+
path = 'benches/bench_foo.rs'
36643664
"#,
36653665
)],
36663666
);

0 commit comments

Comments
 (0)