build-support/rust/build-rust-crate/configure-crate: fix using features that aren't valid Bash variable names (#502814)

This commit is contained in:
Bernardo Meurer
2026-03-24 13:58:34 +00:00
committed by GitHub
2 changed files with 14 additions and 6 deletions
@@ -49,7 +49,7 @@ let
completeDepsDir = lib.concatStringsSep " " completeDeps;
completeBuildDepsDir = lib.concatStringsSep " " completeBuildDeps;
envFeatures = lib.concatStringsSep " " (
map (f: lib.replaceStrings [ "-" ] [ "_" ] (lib.toUpper f)) crateFeatures
map (f: "CARGO_FEATURE_${lib.replaceStrings [ "-" ] [ "_" ] (lib.toUpper f)}=1") crateFeatures
);
in
''
@@ -206,11 +206,10 @@ in
(
# Features should be set as environment variable for build scripts:
# https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
for feature in ${envFeatures}; do
export CARGO_FEATURE_$feature=1
done
target/build/${crateName}/build_script_build | tee target/build/${crateName}.opt
#
# We use `env` instead of `export` because it's possible for a Cargo feature name to contain a character that Bash does
# not support for variables.
env ${envFeatures} target/build/${crateName}/build_script_build | tee target/build/${crateName}.opt
)
set +e
@@ -477,6 +477,7 @@ rec {
crateName = "build-script-feature-env";
features = [
"some-feature"
"some-c++17-thing"
"crate/another_feature"
];
src = symlinkJoin {
@@ -488,6 +489,10 @@ rec {
fn feature_not_visible() {
assert!(std::env::var("CARGO_FEATURE_SOME_FEATURE").is_err());
assert!(option_env!("CARGO_FEATURE_SOME_FEATURE").is_none());
assert!(std::env::var("CARGO_FEATURE_SOME_C++17_THING").is_err());
assert!(option_env!("CARGO_FEATURE_SOME_C++17_THING").is_none());
assert!(std::env::var("CARGO_FEATURE_ANOTHER_FEATURE").is_err());
assert!(option_env!("CARGO_FEATURE_ANOTHER_FEATURE").is_none());
}
fn main() {}
'')
@@ -495,6 +500,10 @@ rec {
fn main() {
assert!(std::env::var("CARGO_FEATURE_SOME_FEATURE").is_ok());
assert!(option_env!("CARGO_FEATURE_SOME_FEATURE").is_none());
assert!(std::env::var("CARGO_FEATURE_SOME_C++17_THING").is_ok());
assert!(option_env!("CARGO_FEATURE_SOME_C++17_THING").is_none());
assert!(std::env::var("CARGO_FEATURE_ANOTHER_FEATURE").is_err());
assert!(option_env!("CARGO_FEATURE_ANOTHER_FEATURE").is_none());
}
'')
];