Report valid file name when we can't find a build target for name = "foo.rs" (#15707)

fixes #15703
This commit is contained in:
Ed Page 2025-07-01 20:49:34 +00:00 committed by GitHub
commit 714157308b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 541 additions and 469 deletions

View File

@ -21,12 +21,11 @@ use cargo_util_schemas::manifest::{
TomlManifest, TomlPackageBuild, TomlTarget, TomlTestTarget,
};
use crate::core::compiler::rustdoc::RustdocScrapeExamples;
use crate::core::compiler::CrateType;
use crate::core::compiler::{rustdoc::RustdocScrapeExamples, CrateType};
use crate::core::{Edition, Feature, Features, Target};
use crate::util::errors::CargoResult;
use crate::util::restricted_names;
use crate::util::toml::deprecated_underscore;
use crate::util::{
closest_msg, errors::CargoResult, restricted_names, toml::deprecated_underscore,
};
const DEFAULT_TEST_DIR_NAME: &'static str = "tests";
const DEFAULT_BENCH_DIR_NAME: &'static str = "benches";
@ -952,73 +951,59 @@ fn target_path_not_found_error_message(
package_root: &Path,
target: &TomlTarget,
target_kind: &str,
inferred: &[(String, PathBuf)],
) -> String {
fn possible_target_paths(name: &str, kind: &str, commonly_wrong: bool) -> [PathBuf; 2] {
let mut target_path = PathBuf::new();
match (kind, commonly_wrong) {
// commonly wrong paths
("test" | "bench" | "example", true) => target_path.push(kind),
("bin", true) => {
target_path.push("src");
target_path.push("bins");
}
("bin", true) => target_path.extend(["src", "bins"]),
// default inferred paths
("test", false) => target_path.push(DEFAULT_TEST_DIR_NAME),
("bench", false) => target_path.push(DEFAULT_BENCH_DIR_NAME),
("example", false) => target_path.push(DEFAULT_EXAMPLE_DIR_NAME),
("bin", false) => {
target_path.push("src");
target_path.push("bin");
}
("bin", false) => target_path.extend(["src", "bin"]),
_ => unreachable!("invalid target kind: {}", kind),
}
target_path.push(name);
let target_path_file = {
let mut path = target_path.clone();
path.set_extension("rs");
path.push(format!("{name}.rs"));
path
};
let target_path_subdir = {
target_path.push("main.rs");
target_path.extend([name, "main.rs"]);
target_path
};
return [target_path_file, target_path_subdir];
}
let target_name = name_or_panic(target);
let commonly_wrong_paths = possible_target_paths(&target_name, target_kind, true);
let possible_paths = possible_target_paths(&target_name, target_kind, false);
let existing_wrong_path_index = match (
package_root.join(&commonly_wrong_paths[0]).exists(),
package_root.join(&commonly_wrong_paths[1]).exists(),
) {
(true, _) => Some(0),
(_, true) => Some(1),
_ => None,
};
if let Some(i) = existing_wrong_path_index {
return format!(
"\
can't find `{name}` {kind} at default paths, but found a file at `{wrong_path}`.
Perhaps rename the file to `{possible_path}` for target auto-discovery, \
or specify {kind}.path if you want to use a non-default path.",
name = target_name,
kind = target_kind,
wrong_path = commonly_wrong_paths[i].display(),
possible_path = possible_paths[i].display(),
);
let msg = closest_msg(target_name, inferred.iter(), |(n, _p)| n, target_kind);
if let Some((wrong_path, possible_path)) = commonly_wrong_paths
.iter()
.zip(possible_paths.iter())
.filter(|(wp, _)| package_root.join(wp).exists())
.next()
{
let [wrong_path, possible_path] = [wrong_path, possible_path].map(|p| p.display());
format!(
"can't find `{target_name}` {target_kind} at default paths, but found a file at `{wrong_path}`.\n\
Perhaps rename the file to `{possible_path}` for target auto-discovery, \
or specify {target_kind}.path if you want to use a non-default path.{msg}",
)
} else {
let [path_file, path_dir] = possible_paths.each_ref().map(|p| p.display());
format!(
"can't find `{target_name}` {target_kind} at `{path_file}` or `{path_dir}`. \
Please specify {target_kind}.path if you want to use a non-default path.{msg}"
)
}
format!(
"can't find `{name}` {kind} at `{path_file}` or `{path_dir}`. \
Please specify {kind}.path if you want to use a non-default path.",
name = target_name,
kind = target_kind,
path_file = possible_paths[0].display(),
path_dir = possible_paths[1].display(),
)
}
fn target_path(
@ -1054,6 +1039,7 @@ fn target_path(
package_root,
target,
target_kind,
inferred,
))
}
(Some(p0), Some(p1)) => {

View File

@ -3,8 +3,7 @@
use crate::prelude::*;
use cargo_test_support::git::cargo_uses_gitoxide;
use cargo_test_support::registry::{self, Package};
use cargo_test_support::str;
use cargo_test_support::{basic_manifest, project, rustc_host};
use cargo_test_support::{basic_bin_manifest, basic_manifest, project, rustc_host, str, Project};
#[cargo_test]
fn bad1() {
@ -419,16 +418,13 @@ fn bad_crate_type() {
.file("src/lib.rs", "")
.build();
p.cargo("build -v")
p.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to run `rustc` to learn about crate-type bad_type information
Caused by:
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --crate-type bad_type` ([EXIT_STATUS]: 1)
--- stderr
[ERROR] unknown crate type: `bad_type`[..]
[CHECKING] foo v0.0.0 ([ROOT]/foo)
[ERROR] unknown crate type: `bad_type`, expected one of: `lib`, `rlib`, `staticlib`, `dylib`, `cdylib`, `bin`, `proc-macro`
[ERROR] could not compile `foo` (lib) due to 1 previous error
"#]])
.run();
@ -3043,3 +3039,509 @@ fn bad_trim_paths() {
"#]])
.run();
}
fn bad_target_name_project(target: &str, path: &str, name: &str) -> Project {
project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "bad-{target}-name"
edition = "2024"
[[{target}]]
name = "{name}"
"#
),
)
.file("src/lib.rs", "")
.file(format!("{path}/{name}"), "")
.build()
}
#[cargo_test]
fn bad_bin_name() {
bad_target_name_project("bin", "src/bin", "bin.rs")
.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
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.
[HELP] a bin with a similar name exists: `bin`
"#]])
.run();
}
#[cargo_test]
fn bad_example_name() {
bad_target_name_project("example", "examples", "example.rs")
.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
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.
[HELP] a example with a similar name exists: `example`
"#]])
.run();
}
#[cargo_test]
fn bad_test_name() {
bad_target_name_project("test", "tests", "test.rs")
.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
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.
[HELP] a test with a similar name exists: `test`
"#]])
.run();
}
#[cargo_test]
fn bad_bench_name() {
bad_target_name_project("bench", "benches", "bench.rs")
.cargo("check")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
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.
[HELP] a bench with a similar name exists: `bench`
"#]])
.run();
}
#[cargo_test]
fn non_existing_test() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[test]]
name = "hello"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check --tests -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `hello` test at `tests/hello.rs` or `tests/hello/main.rs`. Please specify test.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn non_existing_example() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[example]]
name = "hello"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check --examples -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `hello` example at `examples/hello.rs` or `examples/hello/main.rs`. Please specify example.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn non_existing_benchmark() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[bench]]
name = "hello"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("check --benches -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `hello` bench at `benches/hello.rs` or `benches/hello/main.rs`. Please specify bench.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn non_existing_binary() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/lib.rs", "")
.file("src/bin/ehlo.rs", "")
.build();
p.cargo("check -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
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.
[HELP] a bin with a similar name exists: `ehlo`
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_of_test() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[test]]
name = "foo"
"#,
)
.file("src/lib.rs", "")
.file("test/foo.rs", "")
.build();
p.cargo("check --tests -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` test at default paths, but found a file at `test/foo.rs`.
Perhaps rename the file to `tests/foo.rs` for target auto-discovery, or specify test.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_of_example() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[example]]
name = "foo"
"#,
)
.file("src/lib.rs", "")
.file("example/foo.rs", "")
.build();
p.cargo("check --examples -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` example at default paths, but found a file at `example/foo.rs`.
Perhaps rename the file to `examples/foo.rs` for target auto-discovery, or specify example.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_of_benchmark() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[bench]]
name = "foo"
"#,
)
.file("src/lib.rs", "")
.file("bench/foo.rs", "")
.build();
p.cargo("check --benches -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` bench at default paths, but found a file at `bench/foo.rs`.
Perhaps rename the file to `benches/foo.rs` for target auto-discovery, or specify bench.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_binary() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/lib.rs", "")
.file("src/bins/foo.rs", "")
.build();
p.cargo("check -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` bin at default paths, but found a file at `src/bins/foo.rs`.
Perhaps rename the file to `src/bin/foo.rs` for target auto-discovery, or specify bin.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_subdir_binary() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/lib.rs", "")
.file("src/bins/foo/main.rs", "")
.build();
p.cargo("check -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` bin at default paths, but found a file at `src/bins/foo/main.rs`.
Perhaps rename the file to `src/bin/foo/main.rs` for target auto-discovery, or specify bin.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn found_multiple_target_files() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/lib.rs", "")
.file("src/bin/foo.rs", "")
.file("src/bin/foo/main.rs", "")
.build();
p.cargo("check -v")
.with_status(101)
// Don't assert the inferred paths since the order is non-deterministic.
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
cannot infer path for `foo` bin
Cargo doesn't know which to use because multiple target files found at `src/bin/foo[..]rs` and `src/bin/foo[..].rs`.
"#]])
.run();
}
#[cargo_test]
fn legacy_binary_paths_warnings() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
authors = []
[[bin]]
name = "bar"
"#,
)
.file("src/lib.rs", "")
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("check -v")
.with_stderr_data(str![[r#"
[WARNING] An explicit [[bin]] section is specified in Cargo.toml which currently
disables Cargo from automatically inferring other binary targets.
This inference behavior will change in the Rust 2018 edition and the following
files will be included as a binary target:
* src/main.rs
This is likely to break cargo build or cargo test as these files may not be
ready to be compiled as a binary target today. You can future-proof yourself
and disable this warning by adding `autobins = false` to your [package]
section. You may also move the files to a location where Cargo would not
automatically infer them to be a target, such as in subfolders.
For more information on this warning you can consult
https://github.com/rust-lang/cargo/issues/5330
[WARNING] path `src/main.rs` was erroneously implicitly accepted for binary `bar`,
please set bin.path in Cargo.toml
[CHECKING] foo v1.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]`
[RUNNING] `rustc [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
authors = []
[[bin]]
name = "bar"
"#,
)
.file("src/lib.rs", "")
.file("src/bin/main.rs", "fn main() {}")
.build();
p.cargo("check -v")
.with_stderr_data(str![[r#"
[WARNING] An explicit [[bin]] section is specified in Cargo.toml which currently
disables Cargo from automatically inferring other binary targets.
This inference behavior will change in the Rust 2018 edition and the following
files will be included as a binary target:
* src/bin/main.rs
This is likely to break cargo build or cargo test as these files may not be
ready to be compiled as a binary target today. You can future-proof yourself
and disable this warning by adding `autobins = false` to your [package]
section. You may also move the files to a location where Cargo would not
automatically infer them to be a target, such as in subfolders.
For more information on this warning you can consult
https://github.com/rust-lang/cargo/issues/5330
[WARNING] path `src/bin/main.rs` was erroneously implicitly accepted for binary `bar`,
please set bin.path in Cargo.toml
[CHECKING] foo v1.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]`
[RUNNING] `rustc [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
authors = []
[[bin]]
name = "bar"
"#,
)
.file("src/bar.rs", "fn main() {}")
.build();
p.cargo("check -v")
.with_stderr_data(str![[r#"
[WARNING] path `src/bar.rs` was erroneously implicitly accepted for binary `bar`,
please set bin.path in Cargo.toml
[CHECKING] foo v1.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}

View File

@ -2251,422 +2251,6 @@ Goodbye, World!
.run();
}
#[cargo_test]
fn non_existing_test() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[test]]
name = "hello"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("build --tests -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `hello` test at `tests/hello.rs` or `tests/hello/main.rs`. Please specify test.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn non_existing_example() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[example]]
name = "hello"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("build --examples -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `hello` example at `examples/hello.rs` or `examples/hello/main.rs`. Please specify example.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn non_existing_benchmark() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[bench]]
name = "hello"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("build --benches -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `hello` bench at `benches/hello.rs` or `benches/hello/main.rs`. Please specify bench.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn non_existing_binary() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/lib.rs", "")
.file("src/bin/ehlo.rs", "")
.build();
p.cargo("build -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
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.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_of_test() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[test]]
name = "foo"
"#,
)
.file("src/lib.rs", "")
.file("test/foo.rs", "")
.build();
p.cargo("build --tests -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` test at default paths, but found a file at `test/foo.rs`.
Perhaps rename the file to `tests/foo.rs` for target auto-discovery, or specify test.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_of_example() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[example]]
name = "foo"
"#,
)
.file("src/lib.rs", "")
.file("example/foo.rs", "")
.build();
p.cargo("build --examples -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` example at default paths, but found a file at `example/foo.rs`.
Perhaps rename the file to `examples/foo.rs` for target auto-discovery, or specify example.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_of_benchmark() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
[lib]
name = "foo"
path = "src/lib.rs"
[[bench]]
name = "foo"
"#,
)
.file("src/lib.rs", "")
.file("bench/foo.rs", "")
.build();
p.cargo("build --benches -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` bench at default paths, but found a file at `bench/foo.rs`.
Perhaps rename the file to `benches/foo.rs` for target auto-discovery, or specify bench.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_binary() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/lib.rs", "")
.file("src/bins/foo.rs", "")
.build();
p.cargo("build -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` bin at default paths, but found a file at `src/bins/foo.rs`.
Perhaps rename the file to `src/bin/foo.rs` for target auto-discovery, or specify bin.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn commonly_wrong_path_subdir_binary() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/lib.rs", "")
.file("src/bins/foo/main.rs", "")
.build();
p.cargo("build -v")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
can't find `foo` bin at default paths, but found a file at `src/bins/foo/main.rs`.
Perhaps rename the file to `src/bin/foo/main.rs` for target auto-discovery, or specify bin.path if you want to use a non-default path.
"#]])
.run();
}
#[cargo_test]
fn found_multiple_target_files() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file("src/lib.rs", "")
.file("src/bin/foo.rs", "")
.file("src/bin/foo/main.rs", "")
.build();
p.cargo("build -v")
.with_status(101)
// Don't assert the inferred paths since the order is non-deterministic.
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
cannot infer path for `foo` bin
Cargo doesn't know which to use because multiple target files found at `src/bin/foo[..]rs` and `src/bin/foo[..].rs`.
"#]])
.run();
}
#[cargo_test]
fn legacy_binary_paths_warnings() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
authors = []
[[bin]]
name = "bar"
"#,
)
.file("src/lib.rs", "")
.file("src/main.rs", "fn main() {}")
.build();
p.cargo("build -v")
.with_stderr_data(str![[r#"
[WARNING] An explicit [[bin]] section is specified in Cargo.toml which currently
disables Cargo from automatically inferring other binary targets.
This inference behavior will change in the Rust 2018 edition and the following
files will be included as a binary target:
* src/main.rs
This is likely to break cargo build or cargo test as these files may not be
ready to be compiled as a binary target today. You can future-proof yourself
and disable this warning by adding `autobins = false` to your [package]
section. You may also move the files to a location where Cargo would not
automatically infer them to be a target, such as in subfolders.
For more information on this warning you can consult
https://github.com/rust-lang/cargo/issues/5330
[WARNING] path `src/main.rs` was erroneously implicitly accepted for binary `bar`,
please set bin.path in Cargo.toml
[COMPILING] foo v1.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]`
[RUNNING] `rustc [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
authors = []
[[bin]]
name = "bar"
"#,
)
.file("src/lib.rs", "")
.file("src/bin/main.rs", "fn main() {}")
.build();
p.cargo("build -v")
.with_stderr_data(str![[r#"
[WARNING] An explicit [[bin]] section is specified in Cargo.toml which currently
disables Cargo from automatically inferring other binary targets.
This inference behavior will change in the Rust 2018 edition and the following
files will be included as a binary target:
* src/bin/main.rs
This is likely to break cargo build or cargo test as these files may not be
ready to be compiled as a binary target today. You can future-proof yourself
and disable this warning by adding `autobins = false` to your [package]
section. You may also move the files to a location where Cargo would not
automatically infer them to be a target, such as in subfolders.
For more information on this warning you can consult
https://github.com/rust-lang/cargo/issues/5330
[WARNING] path `src/bin/main.rs` was erroneously implicitly accepted for binary `bar`,
please set bin.path in Cargo.toml
[COMPILING] foo v1.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]`
[RUNNING] `rustc [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "1.0.0"
edition = "2015"
authors = []
[[bin]]
name = "bar"
"#,
)
.file("src/bar.rs", "fn main() {}")
.build();
p.cargo("build -v")
.with_stderr_data(str![[r#"
[WARNING] path `src/bar.rs` was erroneously implicitly accepted for binary `bar`,
please set bin.path in Cargo.toml
[COMPILING] foo v1.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
"#]])
.run();
}
#[cargo_test]
fn implicit_examples() {
let p = project()