@@ -8,7 +8,7 @@ use std::str::{self, FromStr};
88use crate :: AlreadyPrintedError ;
99use anyhow:: { anyhow, bail, Context as _} ;
1010use cargo_platform:: Platform ;
11- use cargo_util:: paths;
11+ use cargo_util:: paths:: { self , normalize_path } ;
1212use cargo_util_schemas:: manifest:: { self , TomlManifest } ;
1313use cargo_util_schemas:: manifest:: { RustVersion , StringOrBool } ;
1414use 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+ }
0 commit comments