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

Compilation Error with Passing Cargo args when build program when IDL Generation is enabled in Anchor 0.30 #3054

Closed
biryukovmaxim opened this issue Jun 26, 2024 · 3 comments · Fixed by #3059
Labels
cli enhancement New feature or request idl related to the IDL, either program or client side

Comments

@biryukovmaxim
Copy link
Contributor

Hello! I'm from Debridge team. We faced an issue while updating our contract from anchor 0.28 to 0.30

Description:

After updating from Anchor 0.28 to 0.30, our project encounters a compilation error when conditional compilation features are used along with IDL generation. The error does not occur when compiling with the --no-idl flag.

MRE:

use anchor_lang::prelude::*;

declare_id!("8bBe1LzQ9cBVzk4bMqcYsRqM942C4DvYvwKy9bZqUVyg");

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

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        cfg_if!(
           if #[cfg(feature = "one")] {
                const FOO: usize = 1;
           } else if #[cfg(feature = "two")] {
                const FOO: usize = 2;
           } else {
                compile_error!("You should use one of feature: `one`, `two`");
           }
        );

        msg!("Greetings from: {:?}", ctx.program_id);
        Ok(())
    }
}

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

features "one" and "two" are added to Cargo.toml

Expected behavior:

Running anchor build -- --features one should produce a valid library and an IDL.

Actual result:

error: You should use one of feature: `one`, `two`
  --> programs/mre-idl-bug/src/lib.rs:17:17
   |
17 |                 compile_error!("You should use one of feature: `one`, `two`");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `mre-idl-bug` (lib test) due to 1 previous error
Error: Building IDL failed

Analysis:

The issue likely stems from the IDL builder function not accepting and passing cargo_args, based on the examination of the source code here.

It works fine when I add flag --no-idl.
It works fine with 0.28.0

@acheroncrypto acheroncrypto added enhancement New feature or request cli idl related to the IDL, either program or client side labels Jun 26, 2024
@acheroncrypto
Copy link
Collaborator

The issue likely stems from the IDL builder function not accepting and passing cargo_args, based on the examination of the source code here.

Thanks, your analysis is correct; cargo arguments you pass don't apply to the IDL build command currently, as you've figured out.

Using the same cargo arguments both for the regular program build and the IDL build should be fine at first glance. If there is a problem, we can also introduce an option that takes in arguments to only pass to the IDL build command.

@biryukovmaxim
Copy link
Contributor Author

biryukovmaxim commented Jun 26, 2024

I saw labels. From my perspective the issue is a bug(or at least breaking change) since it worked before

@acheroncrypto
Copy link
Collaborator

The reason I didn't classify this issue as a bug is because IDL generation process between v0.28 and v0.30 are vastly different (the old one did not use compilation), and you can fix this by including the feature you want to enable to the idl-build feature array in your Cargo.toml:

[features]
idl-build = ["anchor-lang/idl-build", "one"]

It's definitely a breaking change though, hence the versioning.

biryukovmaxim added a commit to debridge-finance/anchor that referenced this issue Jun 27, 2024
passes cargo args to idl generation
biryukovmaxim added a commit to debridge-finance/anchor that referenced this issue Jun 27, 2024
passes cargo args to idl generation
biryukovmaxim added a commit to debridge-finance/anchor that referenced this issue Jun 27, 2024
passes cargo args to idl generation
biryukovmaxim added a commit to debridge-finance/anchor that referenced this issue Jun 28, 2024
passes cargo args to idl generation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli enhancement New feature or request idl related to the IDL, either program or client side
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants