@@ -81,7 +81,13 @@ pub(super) fn to_targets(
81
81
warnings,
82
82
has_lib,
83
83
) ?;
84
- targets. extend ( to_bin_targets ( features, & bins, edition, errors) ?) ;
84
+ targets. extend ( to_bin_targets (
85
+ features,
86
+ & bins,
87
+ package_root,
88
+ edition,
89
+ errors,
90
+ ) ?) ;
85
91
86
92
let toml_examples = resolve_examples (
87
93
resolved_toml. example . as_ref ( ) ,
@@ -91,7 +97,12 @@ pub(super) fn to_targets(
91
97
warnings,
92
98
errors,
93
99
) ?;
94
- targets. extend ( to_example_targets ( & toml_examples, edition, warnings) ?) ;
100
+ targets. extend ( to_example_targets (
101
+ & toml_examples,
102
+ package_root,
103
+ edition,
104
+ warnings,
105
+ ) ?) ;
95
106
96
107
let toml_tests = resolve_tests (
97
108
resolved_toml. test . as_ref ( ) ,
@@ -101,7 +112,7 @@ pub(super) fn to_targets(
101
112
warnings,
102
113
errors,
103
114
) ?;
104
- targets. extend ( to_test_targets ( & toml_tests, edition) ?) ;
115
+ targets. extend ( to_test_targets ( & toml_tests, package_root , edition) ?) ;
105
116
106
117
let toml_benches = resolve_benches (
107
118
resolved_toml. bench . as_ref ( ) ,
@@ -111,7 +122,7 @@ pub(super) fn to_targets(
111
122
warnings,
112
123
errors,
113
124
) ?;
114
- targets. extend ( to_bench_targets ( & toml_benches, edition) ?) ;
125
+ targets. extend ( to_bench_targets ( & toml_benches, package_root , edition) ?) ;
115
126
116
127
// processing the custom build script
117
128
if let Some ( custom_build) = maybe_custom_build ( custom_build, package_root) {
@@ -327,6 +338,7 @@ fn resolve_bins(
327
338
fn to_bin_targets (
328
339
features : & Features ,
329
340
bins : & [ TomlBinTarget ] ,
341
+ package_root : & Path ,
330
342
edition : Edition ,
331
343
errors : & mut Vec < String > ,
332
344
) -> CargoResult < Vec < Target > > {
@@ -364,7 +376,7 @@ fn to_bin_targets(
364
376
365
377
let mut result = Vec :: new ( ) ;
366
378
for bin in bins {
367
- let path = bin. path . clone ( ) . expect ( "previously resolved" ) . 0 ;
379
+ let path = package_root . join ( & bin. path . as_ref ( ) . expect ( "previously resolved" ) . 0 ) ;
368
380
let mut target = Target :: bin_target (
369
381
name_or_panic ( bin) ,
370
382
bin. filename . clone ( ) ,
@@ -381,19 +393,21 @@ fn to_bin_targets(
381
393
382
394
fn legacy_bin_path ( package_root : & Path , name : & str , has_lib : bool ) -> Option < PathBuf > {
383
395
if !has_lib {
384
- let path = package_root . join ( "src" ) . join ( format ! ( "{}.rs" , name) ) ;
385
- if path . exists ( ) {
386
- return Some ( path ) ;
396
+ let rel_path = Path :: new ( "src" ) . join ( format ! ( "{}.rs" , name) ) ;
397
+ if package_root . join ( & rel_path ) . exists ( ) {
398
+ return Some ( rel_path ) ;
387
399
}
388
400
}
389
- let path = package_root. join ( "src" ) . join ( "main.rs" ) ;
390
- if path. exists ( ) {
391
- return Some ( path) ;
401
+
402
+ let rel_path = Path :: new ( "src" ) . join ( "main.rs" ) ;
403
+ if package_root. join ( & rel_path) . exists ( ) {
404
+ return Some ( rel_path) ;
392
405
}
393
406
394
- let path = package_root. join ( "src" ) . join ( "bin" ) . join ( "main.rs" ) ;
395
- if path. exists ( ) {
396
- return Some ( path) ;
407
+ let default_bin_dir_name = Path :: new ( "src" ) . join ( "bin" ) ;
408
+ let rel_path = default_bin_dir_name. join ( "main.rs" ) ;
409
+ if package_root. join ( & rel_path) . exists ( ) {
410
+ return Some ( rel_path) ;
397
411
}
398
412
None
399
413
}
@@ -426,14 +440,15 @@ fn resolve_examples(
426
440
427
441
fn to_example_targets (
428
442
targets : & [ TomlExampleTarget ] ,
443
+ package_root : & Path ,
429
444
edition : Edition ,
430
445
warnings : & mut Vec < String > ,
431
446
) -> CargoResult < Vec < Target > > {
432
447
validate_unique_names ( & targets, "example" ) ?;
433
448
434
449
let mut result = Vec :: new ( ) ;
435
450
for toml in targets {
436
- let path = toml. path . clone ( ) . expect ( "previously resolved" ) . 0 ;
451
+ let path = package_root . join ( & toml. path . as_ref ( ) . expect ( "previously resolved" ) . 0 ) ;
437
452
validate_crate_types ( & toml, "example" , warnings) ;
438
453
let crate_types = match toml. crate_types ( ) {
439
454
Some ( kinds) => kinds. iter ( ) . map ( |s| s. into ( ) ) . collect ( ) ,
@@ -480,12 +495,16 @@ fn resolve_tests(
480
495
Ok ( targets)
481
496
}
482
497
483
- fn to_test_targets ( targets : & [ TomlTestTarget ] , edition : Edition ) -> CargoResult < Vec < Target > > {
498
+ fn to_test_targets (
499
+ targets : & [ TomlTestTarget ] ,
500
+ package_root : & Path ,
501
+ edition : Edition ,
502
+ ) -> CargoResult < Vec < Target > > {
484
503
validate_unique_names ( & targets, "test" ) ?;
485
504
486
505
let mut result = Vec :: new ( ) ;
487
506
for toml in targets {
488
- let path = toml. path . clone ( ) . expect ( "previously resolved" ) . 0 ;
507
+ let path = package_root . join ( & toml. path . as_ref ( ) . expect ( "previously resolved" ) . 0 ) ;
489
508
let mut target = Target :: test_target (
490
509
name_or_panic ( & toml) ,
491
510
path,
@@ -508,8 +527,8 @@ fn resolve_benches(
508
527
) -> CargoResult < Vec < TomlBenchTarget > > {
509
528
let mut legacy_warnings = vec ! [ ] ;
510
529
let mut legacy_bench_path = |bench : & TomlTarget | {
511
- let legacy_path = package_root . join ( "src" ) . join ( "bench.rs" ) ;
512
- if !( name_or_panic ( bench) == "bench" && legacy_path. exists ( ) ) {
530
+ let legacy_path = Path :: new ( "src" ) . join ( "bench.rs" ) ;
531
+ if !( name_or_panic ( bench) == "bench" && package_root . join ( & legacy_path) . exists ( ) ) {
513
532
return None ;
514
533
}
515
534
legacy_warnings. push ( format ! (
@@ -541,12 +560,16 @@ fn resolve_benches(
541
560
Ok ( targets)
542
561
}
543
562
544
- fn to_bench_targets ( targets : & [ TomlBenchTarget ] , edition : Edition ) -> CargoResult < Vec < Target > > {
563
+ fn to_bench_targets (
564
+ targets : & [ TomlBenchTarget ] ,
565
+ package_root : & Path ,
566
+ edition : Edition ,
567
+ ) -> CargoResult < Vec < Target > > {
545
568
validate_unique_names ( & targets, "bench" ) ?;
546
569
547
570
let mut result = Vec :: new ( ) ;
548
571
for toml in targets {
549
- let path = toml. path . clone ( ) . expect ( "previously resolved" ) . 0 ;
572
+ let path = package_root . join ( & toml. path . as_ref ( ) . expect ( "previously resolved" ) . 0 ) ;
550
573
let mut target = Target :: bench_target (
551
574
name_or_panic ( & toml) ,
552
575
path,
@@ -640,22 +663,23 @@ fn resolve_targets_with_legacy_path(
640
663
}
641
664
642
665
fn inferred_lib ( package_root : & Path ) -> Option < PathBuf > {
643
- let lib = package_root . join ( "src" ) . join ( "lib.rs" ) ;
644
- if lib. exists ( ) {
666
+ let lib = Path :: new ( "src" ) . join ( "lib.rs" ) ;
667
+ if package_root . join ( & lib) . exists ( ) {
645
668
Some ( lib)
646
669
} else {
647
670
None
648
671
}
649
672
}
650
673
651
674
fn inferred_bins ( package_root : & Path , package_name : & str ) -> Vec < ( String , PathBuf ) > {
652
- let main = package_root . join ( "src" ) . join ( " main.rs") ;
675
+ let main = "src/ main.rs" ;
653
676
let mut result = Vec :: new ( ) ;
654
- if main. exists ( ) {
677
+ if package_root. join ( main) . exists ( ) {
678
+ let main = PathBuf :: from ( main) ;
655
679
result. push ( ( package_name. to_string ( ) , main) ) ;
656
680
}
657
681
let default_bin_dir_name = Path :: new ( "src" ) . join ( "bin" ) ;
658
- result. extend ( infer_from_directory ( & package_root, & default_bin_dir_name) ) ;
682
+ result. extend ( infer_from_directory ( package_root, & default_bin_dir_name) ) ;
659
683
660
684
result
661
685
}
@@ -670,31 +694,39 @@ fn infer_from_directory(package_root: &Path, relpath: &Path) -> Vec<(String, Pat
670
694
entries
671
695
. filter_map ( |e| e. ok ( ) )
672
696
. filter ( is_not_dotfile)
673
- . filter_map ( |d| infer_any ( & d) )
697
+ . filter_map ( |d| infer_any ( package_root , & d) )
674
698
. collect ( )
675
699
}
676
700
677
- fn infer_any ( entry : & DirEntry ) -> Option < ( String , PathBuf ) > {
701
+ fn infer_any ( package_root : & Path , entry : & DirEntry ) -> Option < ( String , PathBuf ) > {
678
702
if entry. file_type ( ) . map_or ( false , |t| t. is_dir ( ) ) {
679
- infer_subdirectory ( entry)
703
+ infer_subdirectory ( package_root , entry)
680
704
} else if entry. path ( ) . extension ( ) . and_then ( |p| p. to_str ( ) ) == Some ( "rs" ) {
681
- infer_file ( entry)
705
+ infer_file ( package_root , entry)
682
706
} else {
683
707
None
684
708
}
685
709
}
686
710
687
- fn infer_file ( entry : & DirEntry ) -> Option < ( String , PathBuf ) > {
711
+ fn infer_file ( package_root : & Path , entry : & DirEntry ) -> Option < ( String , PathBuf ) > {
688
712
let path = entry. path ( ) ;
689
713
let stem = path. file_stem ( ) ?. to_str ( ) ?. to_owned ( ) ;
714
+ let path = path
715
+ . strip_prefix ( package_root)
716
+ . map ( |p| p. to_owned ( ) )
717
+ . unwrap_or ( path) ;
690
718
Some ( ( stem, path) )
691
719
}
692
720
693
- fn infer_subdirectory ( entry : & DirEntry ) -> Option < ( String , PathBuf ) > {
721
+ fn infer_subdirectory ( package_root : & Path , entry : & DirEntry ) -> Option < ( String , PathBuf ) > {
694
722
let path = entry. path ( ) ;
695
723
let main = path. join ( "main.rs" ) ;
696
724
let name = path. file_name ( ) ?. to_str ( ) ?. to_owned ( ) ;
697
725
if main. exists ( ) {
726
+ let main = main
727
+ . strip_prefix ( package_root)
728
+ . map ( |p| p. to_owned ( ) )
729
+ . unwrap_or ( main) ;
698
730
Some ( ( name, main) )
699
731
} else {
700
732
None
@@ -997,7 +1029,7 @@ fn target_path(
997
1029
) -> Result < PathBuf , String > {
998
1030
if let Some ( ref path) = target. path {
999
1031
// Should we verify that this path exists here?
1000
- return Ok ( package_root . join ( & path. 0 ) ) ;
1032
+ return Ok ( path. 0 . clone ( ) ) ;
1001
1033
}
1002
1034
let name = name_or_panic ( target) . to_owned ( ) ;
1003
1035
0 commit comments