Skip to content

Anchor #[program] macro appears to ignore cargo feature flags  #2257

@kathodler

Description

@kathodler

I expect to be able to use Cargo feature flags when developing a rust application. They are useful when following continuous integration allowing partially developed features to be integrated within a team to improve a teams workflow.

Take the following example:

use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod cfg_issue {
    use super::*;

    pub fn initialize(_ctx: Context<Initialize>) -> Result<()> {
        Ok(())
    }

    #[cfg(feature = "my-feature")]
    pub fn only_my_feature(_ctx: Context<Initialize>) -> Result<()> {
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize {}

With a configured my-feature feature flag in ./programs/cfg_issue/Cargo.toml

# ...

[features]
my-feature = []

# ...

Expected Behaviour

The program should compile as if the only_my_feature function was not in the source code.

Actual Behaviour

The following will fail:

$ anchor build 
BPF SDK: /home/vscode/.local/share/solana/install/releases/1.10.39/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
   Compiling cfg-issue v0.1.0 (/workspaces/test-bug-anchor/cfg-issue/programs/cfg-issue)
error[E0425]: cannot find function `only_my_feature` in module `cfg_issue`
  --> programs/cfg-issue/src/lib.rs:14:12
   |
14 |     pub fn only_my_feature(_ctx: Context<Initialize>) -> Result<()> {
   |            ^^^^^^^^^^^^^^^ not found in `cfg_issue`
   |
   = note: consider importing this function:
           crate::__private::__global::only_my_feature

For more information about this error, try `rustc --explain E0425`.
error: could not compile `cfg-issue` due to previous error

Whilst including the feature makes the macro succeed as expected:

$ anchor build -- --features my-feature
BPF SDK: /home/vscode/.local/share/solana/install/releases/1.10.39/solana-release/bin/sdk/bpf
Features: my-feature
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release --features my-feature
   Compiling cfg-issue v0.1.0 (/workspaces/test-bug-anchor/cfg-issue/programs/cfg-issue)
    Finished release [optimized] target(s) in 0.64s
cargo-build-bpf child: /home/vscode/.local/share/solana/install/releases/1.10.39/solana-release/bin/sdk/bpf/scripts/strip.sh /workspaces/test-bug-anchor/cfg-issue/target/bpfel-unknown-unknown/release/cfg_issue.so /workspaces/test-bug-anchor/cfg-issue/target/deploy/cfg_issue.so
cargo-build-bpf child: /home/vscode/.local/share/solana/install/releases/1.10.39/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /workspaces/test-bug-anchor/cfg-issue/target/deploy/cfg_issue.so

To deploy this program:
  $ solana program deploy /workspaces/test-bug-anchor/cfg-issue/target/deploy/cfg_issue.so
The program address will default to this keypair (override with --program-id):
  /workspaces/test-bug-anchor/cfg-issue/target/deploy/cfg_issue-keypair.json

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions