Skip to content

Issue with check-cfg and bin targets #131376

@Urgau

Description

@Urgau

Yeah, I'm using Rust 1.81.0, within my ./.cargo/config.toml, I have:

[build]
# MUST put `rustflags` NOT `RUSTFLAGS`
rustflags=["--cfg", "MAINNET"]

Then I have a very simple ./src/main.rs:

fn main() {
    #[cfg(MAINNET)]
    println!(
        "Hello, world! from `bins` and from `MAINNET`",
    );

    #[cfg(TESTNET)]
    println!(
        "Hello, world! from `bins` and from `TESTNET`",
    );
}

These are the warnings I get from cargo check:

warning: unexpected `cfg` condition name: `MAINNET`
 --> crates/bins/simulate_txn/src/main.rs:2:11
  |
2 |     #[cfg(MAINNET)]
  |           ^^^^^^^
  |
  = help: expected names are: `clippy`, `debug_assertions`, `doc`, `docsrs`, `doctest`, `feature`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows`
  = help: consider using a Cargo feature instead
  = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
           [lints.rust]
           unexpected_cfgs = { level = "warn", check-cfg = ['cfg(MAINNET)'] }
  = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(MAINNET)");` to the top of the `build.rs`
  = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
  = note: `#[warn(unexpected_cfgs)]` on by default

warning: unexpected `cfg` condition name: `TESTNET`
 --> crates/bins/simulate_txn/src/main.rs:7:11
  |
7 |     #[cfg(TESTNET)]
  |           ^^^^^^^
  |
  = help: consider using a Cargo feature instead
  = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
           [lints.rust]
           unexpected_cfgs = { level = "warn", check-cfg = ['cfg(TESTNET)'] }
  = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(TESTNET)");` to the top of the `build.rs`
  = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration

I tried adding the suggested fixes to ./build.rs:

fn main() {
    println!("cargo::rustc-check-cfg=cfg(TESTNET)");
    println!("cargo::rustc-check-cfg=cfg(MAINNET)");
}

It does not work at all.

Originally posted by @jymchng in #125368

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.F-check-cfg--check-cfgS-needs-reproStatus: This issue has no reproduction and needs a reproduction to make progress.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions