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.
This commit is contained in:
David Rheinsberg 2023-10-13 11:26:42 +02:00
parent 8bcc0fa49d
commit 8b2cc62d37

View File

@ -29,15 +29,39 @@ pub fn get_env(
let path = artifact_path.parent().expect("parent dir for artifacts");
env.insert(var, path.to_owned().into());
let var = format!(
let var_file = format!(
"CARGO_{}_FILE_{}_{}",
artifact_type_upper,
dep_name_upper,
unit_dep.unit.target.name()
);
env.insert(var, artifact_path.to_owned().into());
if unit_dep.unit.target.name() == dep_name.as_str() {
// 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());
}
}
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())
{
let var = format!("CARGO_{}_FILE_{}", artifact_type_upper, dep_name_upper,);
env.insert(var, artifact_path.to_owned().into());
}