Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 1 addition & 21 deletions src/cargo/core/compiler/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,13 @@ pub fn get_env(
dep_name_upper,
unit_dep.unit.target.name()
);

// In older releases, lib-targets defaulted to the name of the package. Newer releases
// use the same name as default, but with dashes replaced. Hence, if the name of the
// target was inferred by Cargo, we also set the env-var with the unconverted name for
// backwards compatibility.
let need_compat = unit_dep.unit.target.is_lib() && unit_dep.unit.target.name_inferred();
if need_compat {
let var_compat = format!(
"CARGO_{}_FILE_{}_{}",
artifact_type_upper,
dep_name_upper,
unit_dep.unit.pkg.name(),
);
if var_compat != var_file {
env.insert(var_compat, artifact_path.to_owned().into());
}
}

Comment on lines -70 to -86

@weihanglo weihanglo May 20, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this, we forgot to update documentation when we added this compat layer

Note that `<NAME>` is not modified in any way from the `name` specified in the crate supplying the artifact, or the crate name if not specified; for instance, it may be in lowercase, or contain dashes.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was taking that crate to mean "build target" and not .crate file

env.insert(var_file, artifact_path.to_owned().into());

// If the name of the target matches the name of the dependency, we strip the
// repetition and provide the simpler env-var as well.
// For backwards-compatibility of inferred names, we compare against the name of the
// package as well, since that used to be the default for library targets.
if unit_dep.unit.target.name() == dep_name.as_str()
|| (need_compat && unit_dep.unit.pkg.name() == dep_name.as_str())
{
if unit_dep.unit.target.name() == dep_name.as_str() {
let var = format!("CARGO_{}_FILE_{}", artifact_type_upper, dep_name_upper,);
env.insert(var, artifact_path.to_owned().into());
}
Expand Down
4 changes: 0 additions & 4 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,11 +858,7 @@ fn lib_with_selected_dashed_bin_artifact_and_lib_true() {

env!("CARGO_BIN_DIR_BAR_BAZ");
let _b = include_bytes!(env!("CARGO_BIN_FILE_BAR_BAZ_baz-suffix"));
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ"));
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ_bar-baz"));
let _b = include_bytes!(env!("CARGO_STATICLIB_FILE_BAR_BAZ_bar_baz"));
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ"));
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ_bar-baz"));
let _b = include_bytes!(env!("CARGO_CDYLIB_FILE_BAR_BAZ_bar_baz"));
}
"#,
Expand Down
Loading