Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anchor IDL generation does not work with feature flags #3465

Closed
gwalen opened this issue Jan 3, 2025 · 1 comment
Closed

Anchor IDL generation does not work with feature flags #3465

gwalen opened this issue Jan 3, 2025 · 1 comment
Labels
cli duplicate This issue or pull request already exists idl related to the IDL, either program or client side

Comments

@gwalen
Copy link

gwalen commented Jan 3, 2025

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"]
@acheroncrypto acheroncrypto added duplicate This issue or pull request already exists cli idl related to the IDL, either program or client side labels Jan 3, 2025
@acheroncrypto
Copy link
Collaborator

This has been implemented in #3059 and mentioned in the (unpublished) v0.31.0 (#3259) release notes:

https://github.com/acheroncrypto/anchor/blob/v0.31.0/docs/src/pages/release-notes/0.31.0.md#pass-cargo-args-to-idl-build

Unfortunately, we don't yet have a release that includes this feature, but you can install CLI from any commit starting from ca7fcee to make it work.

You can also make the CLI consistent for all people if you specify it in Anchor.toml:

[toolchain]
anchor_version = "0.30.1-ca7fcee6b8269b732b66536f72ff3fb48cf1b5f9"

Thanks for the report, but since this issue is essentially the same as #3054, I'm closing it as completed, but feel free to reopen it if the problem continues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli duplicate This issue or pull request already exists idl related to the IDL, either program or client side
Projects
None yet
Development

No branches or pull requests

2 participants