Closed
Description
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"]