coral-xyz / anchor

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

idl: Remove `anchor-syn` dependency #3030

Closed acheroncrypto closed 2 weeks ago

acheroncrypto commented 2 weeks ago

Problem

The new IDL crate depends on anchor-syn (behind build feature flag), only to check the safety comments. Ideally, the IDL crate shouldn't depend on any of the other Anchor crates and be a stand-alone crate (as it's versioning is also different).

Summary of changes

Handle the safety comment checks inside the #[program] macro's idl-build implementation. This should work fine since we pass the program path as an env variable (https://github.com/coral-xyz/anchor/pull/2946), and we can use that to run the logic with the idl-build feature. However, there is one downside: we lose the span information:

Before:

Error: 
        /program/src/lib.rs:87:8
        Struct field "new_account" is unsafe, but is not documented.
        Please add a `/// CHECK:` doc comment explaining why no checks through types are necessary.
        See https://www.anchor-lang.com/docs/the-accounts-struct#safety-checks for more information.

After:

error: custom attribute panicked
  --> program/src/lib.rs:11:1
   |
11 | #[program]
   | ^^^^^^^^^^
   |
   = help: message: Safety checks failed: 
                   /program/src/lib.rs:0:0
                   Struct field "new_account" is unsafe, but is not documented.
                   Please add a `/// CHECK:` doc comment explaining why no checks through types are necessary.
                   See https://www.anchor-lang.com/docs/the-accounts-struct#safety-checks for more information.

error: could not compile `program` (lib test) due to 1 previous error
Error: Building IDL failed

The line and column info is set to 0 when we parse the crate inside a proc macro. Not exactly sure why this happens, but I don't think it's that big of a deal since the file name and the account name is still correct. Not planning to change the span info in this PR as it's harmless, and maybe we can make it work in the future?

vercel[bot] commented 2 weeks ago

@acheroncrypto is attempting to deploy a commit to the coral-xyz Team on Vercel.

A member of the Team first needs to authorize it.