@@ -8,7 +8,7 @@ use std::str::{self, FromStr};
8
8
use crate :: AlreadyPrintedError ;
9
9
use anyhow:: { anyhow, bail, Context as _} ;
10
10
use cargo_platform:: Platform ;
11
- use cargo_util:: paths;
11
+ use cargo_util:: paths:: { self , normalize_path } ;
12
12
use cargo_util_schemas:: manifest:: { self , TomlManifest } ;
13
13
use cargo_util_schemas:: manifest:: { RustVersion , StringOrBool } ;
14
14
use itertools:: Itertools ;
@@ -2336,6 +2336,14 @@ fn prepare_toml_for_publish(
2336
2336
2337
2337
let mut package = me. package ( ) . unwrap ( ) . clone ( ) ;
2338
2338
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
+ }
2339
2347
let current_resolver = package
2340
2348
. resolver
2341
2349
. as_ref ( )
@@ -2362,7 +2370,14 @@ fn prepare_toml_for_publish(
2362
2370
. context ( "license file should have been resolved before `prepare_for_publish()`" ) ?;
2363
2371
let license_path = Path :: new ( & license_file) ;
2364
2372
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 {
2366
2381
// This path points outside of the package root. `cargo package`
2367
2382
// will copy it into the root, so adjust the path to this location.
2368
2383
package. license_file = Some ( manifest:: InheritableField :: Value (
@@ -2384,7 +2399,14 @@ fn prepare_toml_for_publish(
2384
2399
manifest:: StringOrBool :: String ( readme) => {
2385
2400
let readme_path = Path :: new ( & readme) ;
2386
2401
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 {
2388
2410
// This path points outside of the package root. `cargo package`
2389
2411
// will copy it into the root, so adjust the path to this location.
2390
2412
package. readme = Some ( manifest:: InheritableField :: Value (
@@ -2402,16 +2424,30 @@ fn prepare_toml_for_publish(
2402
2424
manifest:: StringOrBool :: Bool ( _) => { }
2403
2425
}
2404
2426
}
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
+
2405
2441
let all = |_d : & manifest:: TomlDependency | true ;
2406
2442
let mut manifest = manifest:: TomlManifest {
2407
2443
package : Some ( package) ,
2408
2444
project : None ,
2409
2445
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,
2415
2451
dependencies : map_deps ( gctx, me. dependencies . as_ref ( ) , all) ?,
2416
2452
dev_dependencies : map_deps (
2417
2453
gctx,
@@ -2555,3 +2591,20 @@ fn prepare_toml_for_publish(
2555
2591
. map ( manifest:: InheritableDependency :: Value )
2556
2592
}
2557
2593
}
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