Skip to content

refactor(toml): Centralize target descriptions #15291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 53 additions & 34 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ const DEFAULT_TEST_DIR_NAME: &'static str = "tests";
const DEFAULT_BENCH_DIR_NAME: &'static str = "benches";
const DEFAULT_EXAMPLE_DIR_NAME: &'static str = "examples";

const TARGET_KIND_HUMAN_LIB: &str = "library";
const TARGET_KIND_HUMAN_BIN: &str = "binary";
const TARGET_KIND_HUMAN_EXAMPLE: &str = "example";
const TARGET_KIND_HUMAN_TEST: &str = "test";
const TARGET_KIND_HUMAN_BENCH: &str = "benchmark";

const TARGET_KIND_LIB: &str = "lib";
const TARGET_KIND_BIN: &str = "bin";
const TARGET_KIND_EXAMPLE: &str = "example";
const TARGET_KIND_TEST: &str = "test";
const TARGET_KIND_BENCH: &str = "bench";

#[tracing::instrument(skip_all)]
pub(super) fn to_targets(
features: &Features,
Expand Down Expand Up @@ -141,8 +153,8 @@ pub fn normalize_lib(
// Check early to improve error messages
validate_lib_name(&lib, warnings)?;

validate_proc_macro(&lib, "library", edition, warnings)?;
validate_crate_types(&lib, "library", edition, warnings)?;
validate_proc_macro(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?;
validate_crate_types(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?;

if let Some(PathValue(path)) = &lib.path {
lib.path = Some(PathValue(paths::normalize_path(path).into()));
Expand All @@ -164,8 +176,8 @@ pub fn normalize_lib(
// Check early to improve error messages
validate_lib_name(&lib, warnings)?;

validate_proc_macro(&lib, "library", edition, warnings)?;
validate_crate_types(&lib, "library", edition, warnings)?;
validate_proc_macro(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?;
validate_crate_types(&lib, TARGET_KIND_HUMAN_LIB, edition, warnings)?;

if lib.path.is_none() {
if let Some(inferred) = inferred {
Expand Down Expand Up @@ -285,8 +297,8 @@ pub fn normalize_bins(
autodiscover,
edition,
warnings,
"binary",
"bin",
TARGET_KIND_HUMAN_BIN,
TARGET_KIND_BIN,
"autobins",
);

Expand All @@ -297,21 +309,28 @@ pub fn normalize_bins(
validate_bin_crate_types(bin, edition, warnings, errors)?;
validate_bin_proc_macro(bin, edition, warnings, errors)?;

let path = target_path(bin, &inferred, "bin", package_root, edition, &mut |_| {
if let Some(legacy_path) =
legacy_bin_path(package_root, name_or_panic(bin), has_lib)
{
warnings.push(format!(
"path `{}` was erroneously implicitly accepted for binary `{}`,\n\
let path = target_path(
bin,
&inferred,
TARGET_KIND_BIN,
package_root,
edition,
&mut |_| {
if let Some(legacy_path) =
legacy_bin_path(package_root, name_or_panic(bin), has_lib)
{
warnings.push(format!(
"path `{}` was erroneously implicitly accepted for binary `{}`,\n\
please set bin.path in Cargo.toml",
legacy_path.display(),
name_or_panic(bin)
));
Some(legacy_path)
} else {
None
}
});
legacy_path.display(),
name_or_panic(bin)
));
Some(legacy_path)
} else {
None
}
},
);
let path = match path {
Ok(path) => paths::normalize_path(&path).into(),
Err(e) => anyhow::bail!("{}", e),
Expand Down Expand Up @@ -339,7 +358,7 @@ fn to_bin_targets(
}
}

validate_unique_names(&bins, "binary")?;
validate_unique_names(&bins, TARGET_KIND_HUMAN_BIN)?;

let mut result = Vec::new();
for bin in bins {
Expand Down Expand Up @@ -391,8 +410,8 @@ pub fn normalize_examples(
let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_EXAMPLE_DIR_NAME));

let targets = normalize_targets(
"example",
"example",
TARGET_KIND_HUMAN_EXAMPLE,
TARGET_KIND_EXAMPLE,
toml_examples,
&mut inferred,
package_root,
Expand All @@ -412,7 +431,7 @@ fn to_example_targets(
package_root: &Path,
edition: Edition,
) -> CargoResult<Vec<Target>> {
validate_unique_names(&targets, "example")?;
validate_unique_names(&targets, TARGET_KIND_EXAMPLE)?;

let mut result = Vec::new();
for toml in targets {
Expand Down Expand Up @@ -448,8 +467,8 @@ pub fn normalize_tests(
let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_TEST_DIR_NAME));

let targets = normalize_targets(
"test",
"test",
TARGET_KIND_HUMAN_TEST,
TARGET_KIND_TEST,
toml_tests,
&mut inferred,
package_root,
Expand All @@ -469,7 +488,7 @@ fn to_test_targets(
package_root: &Path,
edition: Edition,
) -> CargoResult<Vec<Target>> {
validate_unique_names(&targets, "test")?;
validate_unique_names(&targets, TARGET_KIND_TEST)?;

let mut result = Vec::new();
for toml in targets {
Expand Down Expand Up @@ -513,8 +532,8 @@ pub fn normalize_benches(
let mut inferred = || infer_from_directory(&package_root, Path::new(DEFAULT_BENCH_DIR_NAME));

let targets = normalize_targets_with_legacy_path(
"benchmark",
"bench",
TARGET_KIND_HUMAN_BENCH,
TARGET_KIND_BENCH,
toml_benches,
&mut inferred,
package_root,
Expand All @@ -536,7 +555,7 @@ fn to_bench_targets(
package_root: &Path,
edition: Edition,
) -> CargoResult<Vec<Target>> {
validate_unique_names(&targets, "bench")?;
validate_unique_names(&targets, TARGET_KIND_BENCH)?;

let mut result = Vec::new();
for toml in targets {
Expand Down Expand Up @@ -1074,7 +1093,7 @@ fn name_or_panic(target: &TomlTarget) -> &str {
}

fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec<String>) -> CargoResult<()> {
validate_target_name(target, "library", "lib", warnings)?;
validate_target_name(target, TARGET_KIND_HUMAN_LIB, TARGET_KIND_LIB, warnings)?;
let name = name_or_panic(target);
if name.contains('-') {
anyhow::bail!("library target names cannot contain hyphens: {}", name)
Expand All @@ -1084,7 +1103,7 @@ fn validate_lib_name(target: &TomlTarget, warnings: &mut Vec<String>) -> CargoRe
}

fn validate_bin_name(bin: &TomlTarget, warnings: &mut Vec<String>) -> CargoResult<()> {
validate_target_name(bin, "binary", "bin", warnings)?;
validate_target_name(bin, TARGET_KIND_HUMAN_BIN, TARGET_KIND_BIN, warnings)?;
let name = name_or_panic(bin).to_owned();
if restricted_names::is_conflicting_artifact_name(&name) {
anyhow::bail!(
Expand Down Expand Up @@ -1139,7 +1158,7 @@ fn validate_bin_proc_macro(
name
));
} else {
validate_proc_macro(target, "binary", edition, warnings)?;
validate_proc_macro(target, TARGET_KIND_HUMAN_BIN, edition, warnings)?;
}
Ok(())
}
Expand Down Expand Up @@ -1177,7 +1196,7 @@ fn validate_bin_crate_types(
crate_types.join(", ")
));
} else {
validate_crate_types(target, "binary", edition, warnings)?;
validate_crate_types(target, TARGET_KIND_HUMAN_BIN, edition, warnings)?;
}
}
Ok(())
Expand Down