Skip to content

Commit

Permalink
refactor(toml): Centralize target descriptions (#15291)
Browse files Browse the repository at this point in the history
I was going to add another duplicate and was wondering what was the
right value and figured it'd be better if I just did this.

<!--
Thanks for submitting a pull request 🎉! Here are some tips for you:

* If this is your first contribution, read "Cargo Contribution Guide"
first:
  https://doc.crates.io/contrib/
* Run `cargo fmt --all` to format your code changes.
* Small commits and pull requests are always preferable and easy to
review.
* If your idea is large and needs feedback from the community, read how:
  https://doc.crates.io/contrib/process/#working-on-large-features
* Cargo takes care of compatibility. Read our design principles:
  https://doc.crates.io/contrib/design.html
* When changing help text of cargo commands, follow the steps to
generate docs:

https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages
* If your PR is not finished, set it as "draft" PR or add "WIP" in its
title.
* It's ok to use the CI resources to test your PR, but please don't
abuse them.

### What does this PR try to resolve?

Explain the motivation behind this change.
A clear overview along with an in-depth explanation are helpful.

You can use `Fixes #<issue number>` to associate this PR to an existing
issue.

### How should we test and review this PR?

Demonstrate how you test this change and guide reviewers through your
PR.
With a smooth review process, a pull request usually gets reviewed
quicker.

If you don't know how to write and run your tests, please read the
guide:
https://doc.crates.io/contrib/tests

### Additional information

Other information you want to mention in this PR, such as prior arts,
future extensions, an unresolved problem, or a TODO list.
-->
  • Loading branch information
weihanglo authored Mar 11, 2025
2 parents 3897c7c + cb28b36 commit 21f4080
Showing 1 changed file with 53 additions and 34 deletions.
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

0 comments on commit 21f4080

Please sign in to comment.