Skip to content

Commit ff41868

Browse files
committed
refactor(toml): Use relative paths in resolved targets
1 parent f1f7bbf commit ff41868

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ pub(super) fn to_targets(
8181
warnings,
8282
has_lib,
8383
)?;
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+
)?);
8591

8692
let toml_examples = resolve_examples(
8793
resolved_toml.example.as_ref(),
@@ -91,7 +97,12 @@ pub(super) fn to_targets(
9197
warnings,
9298
errors,
9399
)?;
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+
)?);
95106

96107
let toml_tests = resolve_tests(
97108
resolved_toml.test.as_ref(),
@@ -101,7 +112,7 @@ pub(super) fn to_targets(
101112
warnings,
102113
errors,
103114
)?;
104-
targets.extend(to_test_targets(&toml_tests, edition)?);
115+
targets.extend(to_test_targets(&toml_tests, package_root, edition)?);
105116

106117
let toml_benches = resolve_benches(
107118
resolved_toml.bench.as_ref(),
@@ -111,7 +122,7 @@ pub(super) fn to_targets(
111122
warnings,
112123
errors,
113124
)?;
114-
targets.extend(to_bench_targets(&toml_benches, edition)?);
125+
targets.extend(to_bench_targets(&toml_benches, package_root, edition)?);
115126

116127
// processing the custom build script
117128
if let Some(custom_build) = maybe_custom_build(custom_build, package_root) {
@@ -327,6 +338,7 @@ fn resolve_bins(
327338
fn to_bin_targets(
328339
features: &Features,
329340
bins: &[TomlBinTarget],
341+
package_root: &Path,
330342
edition: Edition,
331343
errors: &mut Vec<String>,
332344
) -> CargoResult<Vec<Target>> {
@@ -364,7 +376,7 @@ fn to_bin_targets(
364376

365377
let mut result = Vec::new();
366378
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);
368380
let mut target = Target::bin_target(
369381
name_or_panic(bin),
370382
bin.filename.clone(),
@@ -381,19 +393,21 @@ fn to_bin_targets(
381393

382394
fn legacy_bin_path(package_root: &Path, name: &str, has_lib: bool) -> Option<PathBuf> {
383395
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);
387399
}
388400
}
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);
392405
}
393406

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);
397411
}
398412
None
399413
}
@@ -426,14 +440,15 @@ fn resolve_examples(
426440

427441
fn to_example_targets(
428442
targets: &[TomlExampleTarget],
443+
package_root: &Path,
429444
edition: Edition,
430445
warnings: &mut Vec<String>,
431446
) -> CargoResult<Vec<Target>> {
432447
validate_unique_names(&targets, "example")?;
433448

434449
let mut result = Vec::new();
435450
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);
437452
validate_crate_types(&toml, "example", warnings);
438453
let crate_types = match toml.crate_types() {
439454
Some(kinds) => kinds.iter().map(|s| s.into()).collect(),
@@ -480,12 +495,16 @@ fn resolve_tests(
480495
Ok(targets)
481496
}
482497

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>> {
484503
validate_unique_names(&targets, "test")?;
485504

486505
let mut result = Vec::new();
487506
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);
489508
let mut target = Target::test_target(
490509
name_or_panic(&toml),
491510
path,
@@ -508,8 +527,8 @@ fn resolve_benches(
508527
) -> CargoResult<Vec<TomlBenchTarget>> {
509528
let mut legacy_warnings = vec![];
510529
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()) {
513532
return None;
514533
}
515534
legacy_warnings.push(format!(
@@ -541,12 +560,16 @@ fn resolve_benches(
541560
Ok(targets)
542561
}
543562

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>> {
545568
validate_unique_names(&targets, "bench")?;
546569

547570
let mut result = Vec::new();
548571
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);
550573
let mut target = Target::bench_target(
551574
name_or_panic(&toml),
552575
path,
@@ -640,22 +663,23 @@ fn resolve_targets_with_legacy_path(
640663
}
641664

642665
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() {
645668
Some(lib)
646669
} else {
647670
None
648671
}
649672
}
650673

651674
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";
653676
let mut result = Vec::new();
654-
if main.exists() {
677+
if package_root.join(main).exists() {
678+
let main = PathBuf::from(main);
655679
result.push((package_name.to_string(), main));
656680
}
657681
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));
659683

660684
result
661685
}
@@ -670,31 +694,39 @@ fn infer_from_directory(package_root: &Path, relpath: &Path) -> Vec<(String, Pat
670694
entries
671695
.filter_map(|e| e.ok())
672696
.filter(is_not_dotfile)
673-
.filter_map(|d| infer_any(&d))
697+
.filter_map(|d| infer_any(package_root, &d))
674698
.collect()
675699
}
676700

677-
fn infer_any(entry: &DirEntry) -> Option<(String, PathBuf)> {
701+
fn infer_any(package_root: &Path, entry: &DirEntry) -> Option<(String, PathBuf)> {
678702
if entry.file_type().map_or(false, |t| t.is_dir()) {
679-
infer_subdirectory(entry)
703+
infer_subdirectory(package_root, entry)
680704
} else if entry.path().extension().and_then(|p| p.to_str()) == Some("rs") {
681-
infer_file(entry)
705+
infer_file(package_root, entry)
682706
} else {
683707
None
684708
}
685709
}
686710

687-
fn infer_file(entry: &DirEntry) -> Option<(String, PathBuf)> {
711+
fn infer_file(package_root: &Path, entry: &DirEntry) -> Option<(String, PathBuf)> {
688712
let path = entry.path();
689713
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);
690718
Some((stem, path))
691719
}
692720

693-
fn infer_subdirectory(entry: &DirEntry) -> Option<(String, PathBuf)> {
721+
fn infer_subdirectory(package_root: &Path, entry: &DirEntry) -> Option<(String, PathBuf)> {
694722
let path = entry.path();
695723
let main = path.join("main.rs");
696724
let name = path.file_name()?.to_str()?.to_owned();
697725
if main.exists() {
726+
let main = main
727+
.strip_prefix(package_root)
728+
.map(|p| p.to_owned())
729+
.unwrap_or(main);
698730
Some((name, main))
699731
} else {
700732
None
@@ -997,7 +1029,7 @@ fn target_path(
9971029
) -> Result<PathBuf, String> {
9981030
if let Some(ref path) = target.path {
9991031
// Should we verify that this path exists here?
1000-
return Ok(package_root.join(&path.0));
1032+
return Ok(path.0.clone());
10011033
}
10021034
let name = name_or_panic(target).to_owned();
10031035

0 commit comments

Comments
 (0)