Skip to content

Commit 8b2cc62

Browse files
committed
cargo/artifact: prepare compatibility env-vars
We are about to change the default value for target-names of libraries. They used to match the package-name. In the future, they will use the package-name with dashes converted to underscores. This will affect the artifact env-variables, since they expose target-names. Hence, set the old env-vars, too, to avoid breakage. Note that we do not retain the name of a target before it was converted, and the conversion is lossy, so we cannot reconstruct it. However, we can rely on the fact that the conversion only happens for default values (since user-supplied values never allowed dashes). Furthermore, we now remember whether a target-name was inferred, so we can exactly reconstruct whether a library-target could have contained dashes in older releases, or not.
1 parent 8bcc0fa commit 8b2cc62

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/cargo/core/compiler/artifact.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,39 @@ pub fn get_env(
2929
let path = artifact_path.parent().expect("parent dir for artifacts");
3030
env.insert(var, path.to_owned().into());
3131

32-
let var = format!(
32+
let var_file = format!(
3333
"CARGO_{}_FILE_{}_{}",
3434
artifact_type_upper,
3535
dep_name_upper,
3636
unit_dep.unit.target.name()
3737
);
38-
env.insert(var, artifact_path.to_owned().into());
3938

40-
if unit_dep.unit.target.name() == dep_name.as_str() {
39+
// In older releases, lib-targets defaulted to the name of the package. Newer releases
40+
// use the same name as default, but with dashes replaced. Hence, if the name of the
41+
// target was inferred by Cargo, we also set the env-var with the unconverted name for
42+
// backwards compatibility.
43+
let need_compat = unit_dep.unit.target.is_lib() && unit_dep.unit.target.name_inferred();
44+
if need_compat {
45+
let var_compat = format!(
46+
"CARGO_{}_FILE_{}_{}",
47+
artifact_type_upper,
48+
dep_name_upper,
49+
unit_dep.unit.pkg.name(),
50+
);
51+
if var_compat != var_file {
52+
env.insert(var_compat, artifact_path.to_owned().into());
53+
}
54+
}
55+
56+
env.insert(var_file, artifact_path.to_owned().into());
57+
58+
// If the name of the target matches the name of the dependency, we strip the
59+
// repetition and provide the simpler env-var as well.
60+
// For backwards-compatibility of inferred names, we compare against the name of the
61+
// package as well, since that used to be the default for library targets.
62+
if unit_dep.unit.target.name() == dep_name.as_str()
63+
|| (need_compat && unit_dep.unit.pkg.name() == dep_name.as_str())
64+
{
4165
let var = format!("CARGO_{}_FILE_{}", artifact_type_upper, dep_name_upper,);
4266
env.insert(var, artifact_path.to_owned().into());
4367
}

0 commit comments

Comments
 (0)