From 8b2cc62d377c9bef66453f32f4872bb84c498b70 Mon Sep 17 00:00:00 2001 From: David Rheinsberg Date: Fri, 13 Oct 2023 11:26:42 +0200 Subject: [PATCH] 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. --- src/cargo/core/compiler/artifact.rs | 30 ++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/compiler/artifact.rs b/src/cargo/core/compiler/artifact.rs index 1f3b12b5c..ad6c4843b 100644 --- a/src/cargo/core/compiler/artifact.rs +++ b/src/cargo/core/compiler/artifact.rs @@ -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()); }