Skip to content

Commit 87a24aa

Browse files
committed
better msg for bug #15703
1 parent 81c233b commit 87a24aa

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ use cargo_util_schemas::manifest::{
2121
TomlManifest, TomlPackageBuild, TomlTarget, TomlTestTarget,
2222
};
2323

24-
use crate::core::compiler::rustdoc::RustdocScrapeExamples;
25-
use crate::core::compiler::CrateType;
24+
use crate::core::compiler::{rustdoc::RustdocScrapeExamples, CrateType};
2625
use crate::core::{Edition, Feature, Features, Target};
27-
use crate::util::errors::CargoResult;
28-
use crate::util::restricted_names;
29-
use crate::util::toml::deprecated_underscore;
26+
use crate::util::{
27+
closest_msg, errors::CargoResult, restricted_names, toml::deprecated_underscore,
28+
};
3029

3130
const DEFAULT_TEST_DIR_NAME: &'static str = "tests";
3231
const DEFAULT_BENCH_DIR_NAME: &'static str = "benches";
@@ -952,6 +951,7 @@ fn target_path_not_found_error_message(
952951
package_root: &Path,
953952
target: &TomlTarget,
954953
target_kind: &str,
954+
inferred: &[(String, PathBuf)],
955955
) -> String {
956956
fn possible_target_paths(name: &str, kind: &str, commonly_wrong: bool) -> [PathBuf; 2] {
957957
let mut target_path = PathBuf::new();
@@ -980,9 +980,11 @@ fn target_path_not_found_error_message(
980980
}
981981

982982
let target_name = name_or_panic(target);
983+
983984
let commonly_wrong_paths = possible_target_paths(&target_name, target_kind, true);
984985
let possible_paths = possible_target_paths(&target_name, target_kind, false);
985986

987+
let msg = closest_msg(target_name, inferred.iter(), |(n, _p)| n, target_kind);
986988
if let Some((wrong_path, possible_path)) = commonly_wrong_paths
987989
.iter()
988990
.zip(possible_paths.iter())
@@ -993,13 +995,13 @@ fn target_path_not_found_error_message(
993995
format!(
994996
"can't find `{target_name}` {target_kind} at default paths, but found a file at `{wrong_path}`.\n\
995997
Perhaps rename the file to `{possible_path}` for target auto-discovery, \
996-
or specify {target_kind}.path if you want to use a non-default path."
998+
or specify {target_kind}.path if you want to use a non-default path.{msg}",
997999
)
9981000
} else {
9991001
let [path_file, path_dir] = possible_paths.each_ref().map(|p| p.display());
10001002
format!(
10011003
"can't find `{target_name}` {target_kind} at `{path_file}` or `{path_dir}`. \
1002-
Please specify {target_kind}.path if you want to use a non-default path."
1004+
Please specify {target_kind}.path if you want to use a non-default path.{msg}"
10031005
)
10041006
}
10051007
}
@@ -1037,6 +1039,7 @@ fn target_path(
10371039
package_root,
10381040
target,
10391041
target_kind,
1042+
inferred,
10401043
))
10411044
}
10421045
(Some(p0), Some(p1)) => {

tests/testsuite/bad_config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,8 @@ fn bad_bin_name() {
30743074
Caused by:
30753075
can't find `bin.rs` bin at `src/bin/bin.rs.rs` or `src/bin/bin.rs/main.rs`. Please specify bin.path if you want to use a non-default path.
30763076
3077+
[HELP] a bin with a similar name exists: `bin`
3078+
30773079
"#]])
30783080
.run();
30793081
}
@@ -3089,6 +3091,8 @@ fn bad_example_name() {
30893091
Caused by:
30903092
can't find `example.rs` example at `examples/example.rs.rs` or `examples/example.rs/main.rs`. Please specify example.path if you want to use a non-default path.
30913093
3094+
[HELP] a example with a similar name exists: `example`
3095+
30923096
"#]])
30933097
.run();
30943098
}
@@ -3104,6 +3108,8 @@ fn bad_test_name() {
31043108
Caused by:
31053109
can't find `test.rs` test at `tests/test.rs.rs` or `tests/test.rs/main.rs`. Please specify test.path if you want to use a non-default path.
31063110
3111+
[HELP] a test with a similar name exists: `test`
3112+
31073113
"#]])
31083114
.run();
31093115
}
@@ -3119,6 +3125,8 @@ fn bad_bench_name() {
31193125
Caused by:
31203126
can't find `bench.rs` bench at `benches/bench.rs.rs` or `benches/bench.rs/main.rs`. Please specify bench.path if you want to use a non-default path.
31213127
3128+
[HELP] a bench with a similar name exists: `bench`
3129+
31223130
"#]])
31233131
.run();
31243132
}

tests/testsuite/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,6 +2369,8 @@ fn non_existing_binary() {
23692369
Caused by:
23702370
can't find `foo` bin at `src/bin/foo.rs` or `src/bin/foo/main.rs`. Please specify bin.path if you want to use a non-default path.
23712371
2372+
[HELP] a bin with a similar name exists: `ehlo`
2373+
23722374
"#]])
23732375
.run();
23742376
}

0 commit comments

Comments
 (0)