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()); }