8000 Anchor IDL generation does not work with feature flags · Issue #3465 · solana-foundation/anchor · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Anchor IDL generation does not work with feature flags #3465
Closed
@gwalen

Description

@gwalen

Issue:
When using feature flags in a Rust file to set the program ID, which should be reflected in the IDL file, passing feature flags during compilation does not work as expected. Instead, only the default option specified in Cargo.toml is applied.

Code Example:
Here is an example of code where the program ID depends on feature flags:

cfg_if! {
    if #[cfg(feature = "mainnet")] {
        declare_id!("worm2ZoG2kUd4vFXhvjh93UUH596ayRfgQ2MgjNMTth");
    } else if #[cfg(feature = "devnet")] {
        declare_id!("3u8hJUVTA4jH1wYAyUur7FFZVQ8H635K3tSHHF4ssjQ5");
    }
}

#[derive(Debug, Clone)]
pub struct Wormhole;

impl Id for Wormhole {
    fn id() -> Pubkey {
        ID
    }
}

Build Command:

anchor build --arch sbf -- --features $(NETWORK) --no-default-features

Result:
IDL generation only considers the default settings specified in Cargo.toml, regardless of which feature flag is passed.

Workaround:
To generate an IDL for a specific environment, you must manually change the default feature in Cargo.toml each time before compilation.

Cargo.toml example:

[features]
default = ["mainnet"]
# Uncomment the desired environment before building
# default = ["testnet"]
# default = ["devnet"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliduplicateThis issue or pull request already existsidlrelated to the IDL, either program or client side

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0