coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

bug: Can't use external types as part of new type with `declare_program!` when compiling with idl-build #2970

Closed cryptopapi997 closed 1 month ago

cryptopapi997 commented 1 month ago

If I have a type in an external program and I try to create a new type with it as part of it, I get

error: proc-macro derive panicked
 --> programs/my-program/src/lib.rs:6:10
  |
6 | #[derive(anchor_lang::AnchorSerialize, anchor_lang::AnchorDeserialize, Clone)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: Failed to get program path: NotPresent

There's an easy way to recreate this:

IDL for external program:

{
  "address": "Externa111111111111111111111111111111111111",
  "metadata": {
    "name": "external",
    "version": "0.1.0",
    "spec": "0.1.0",
    "description": "Created with Anchor"
  },
  "instructions": [],
  "accounts": [],
  "events": [],
  "types": [
    {
      "name": "MyStruct",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "name": "field",
            "type": "u32"
          }
        ]
      }
    }
  ]
}

Code for program importing it:

use anchor_lang::prelude::*;

declare_program!(external);
use external::types::MyStruct;

#[derive(anchor_lang::AnchorSerialize, anchor_lang::AnchorDeserialize)]
pub struct Utils {
    pub authority: MyStruct,
}

Trying to build this throws the above error.

Edit: Upon further investigation, this doesn't happen if we do idl-build = []. Edit: After further investigation I've found the place where it blows up: https://github.com/coral-xyz/anchor/blob/master/lang/syn/src/idl/external.rs#L21

acheroncrypto commented 1 month ago

When using it local or git version, you currently need to also build the CLI using the same way because of https://github.com/coral-xyz/anchor/pull/2946.

Edit: After further investigation I've found the place where it blows up: https://github.com/coral-xyz/anchor/blob/master/lang/syn/src/idl/external.rs#L21

That env variable should always be set using the latest CLI: https://github.com/coral-xyz/anchor/blob/e71a63cb7ae2eaefc4c2f31d04af140434ee0049/idl/src/build.rs#L98

cryptopapi997 commented 1 month ago

Thank you!