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 #[program] macro appears to ignore cargo feature flags #2257

Closed
kathodler opened this issue Nov 7, 2022 · 1 comment · Fixed by #2339
Closed

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

kathodler opened this issue Nov 7, 2022 · 1 comment · Fixed by #2339

Comments

@kathodler
Copy link

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
@kathodler kathodler changed the title Anchor appears to ignore cargo feature flags Anchor #[program] macro appears to ignore cargo feature flags Nov 7, 2022
@Henry-E
Copy link

Henry-E commented Nov 14, 2022

Hmm, I'll try and look into what the program attribute macro is doing that these cfg flags might be being ignored.

In the meantime if you use the cfg flag functionality literally anywhere else (other than directly under the program module) it should work fine. At least cfg flags have always worked fine for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants