@@ -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,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+ }
0 commit comments