Skip to content

feature to env var mapping is not unique #3702

@durka

Description

@durka

Build scripts receive information about activated features as environment variables CARGO_FEATURE_$name, where $name is the feature name uppercased and with dashes converted to underscores.

However, feature names are allowed to contain underscores, so this transformation can create collisions. Example:

  • Cargo.toml
    [package]
    name = "tester"
    version = "0.1.0"
    
    build = "build.rs"
    
    [features]
    foo-bar = []
    foo_bar = []
  • build.rs
    use std::env;
    
    fn main() {
        for (k, v) in env::vars() {
            if k.starts_with("CARGO_FEATURE") {
                println!("{} => {}", k, v);
            }
        }
        panic!(); // so cargo prints the output
    }
  • cargo build --features "foo_bar foo-bar"
    --- stdout
    CARGO_FEATURE_FOO_BAR => 1
    
    --- stderr
    thread 'main' panicked at 'explicit panic', build.rs:9
    note: Run with `RUST_BACKTRACE=1` for a backtrace.
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-scriptsArea: build.rs scriptsA-featuresArea: features — conditional compilationC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions