coral-xyz / anchor

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

`#[program]` macro error in rust-analyzer when LSP run with `cargo.features = "all"` #3042

Closed ifiokjr closed 5 days ago

ifiokjr commented 1 week ago

I've just upgraded to the latest version of anchor 0.30.1. Everything is fine and anchor build runs well. However, there's a small issue when rust-analyzer runs with cargo.features = "all". This means that idl-build feature is switched on in the LSP.

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []
idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]

As a result I'm seeing this error on the #[program] macro.

custom attribute panicked
message: Safety checks failed: Failed to get program path

The error originates from this variable declaration: https://github.com/coral-xyz/anchor/blob/e6d7dafe12da661a36ad1b4f3b5970e8986e5321/lang/syn/src/idl/external.rs#L20-L21

As a temporary fix I've hardcoded the env variable ANCHOR_IDL_BUILD_PROGRAM_PATH to point to the program path but that will stop working with multiple programs in the workspace.

acheroncrypto commented 1 week ago

We can remove the panicking behavior but then the error would be silenced even when it's necessary.

As a temporary fix I've hardcoded the env variable ANCHOR_IDL_BUILD_PROGRAM_PATH to point to the program path but that will stop working with multiple programs in the workspace.

Why would it stop working when there are multiple programs? It should continue to work as long as the env variable ANCHOR_IDL_BUILD_PROGRAM_PATH is only overridden in Rust Analyzer.

Thanks for the report!

tarunjais28 commented 1 week ago

Yes I am also facing the same issue, what could be necessary fix for it? how and where to disable cargo.features = "all" ??

robertohuertasm commented 1 week ago

@tarunjais28 provided you're using VS Code, you can add this to your .vscode/settings.json file:

{
  "rust-analyzer.cargo.allFeatures": false
}