coral-xyz / anchor

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

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

Closed ifiokjr closed 5 months ago

ifiokjr commented 5 months 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 5 months 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 5 months 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 5 months ago

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

{
  "rust-analyzer.cargo.allFeatures": false
}
tarunjais28 commented 5 months ago

@robertohuertasm I tried that but that is also not working, as issue #3045 is merged so how can it reflect on build as I am getting same error

Abhishek7Tech commented 4 months ago

How to resolve this issue? @tarunjais28 @ifiokjr @acheroncrypto

acheroncrypto commented 4 months ago

If you get this error when building, you resolve it by upgrading anchor-cli to 0.30.1. Otherwise this issue is specifically for Rust Analyzer, it means you have idl-build feature enabled there, and you can resolve it by removing "rust-analyzer.cargo.features": "all" entry.

Alternatively, you can disable this lint in Anchor.toml:

[features]
skip-lint = true
tarunjais28 commented 4 months ago

Hi @acheroncrypto I tried above solutions suggested by you, still getting the same error.

image

Its very critical I am not able to build any of my solana projects

image
Abhishek7Tech commented 4 months ago

@tarunjais28 add this line to your Anchor.toml

[features] skip-lint = true

tarunjais28 commented 4 months ago

hi @Abhishek7Tech that is also there

image
DemidovVladimir commented 3 months ago

Strange that the issue is closed, we have the same issue. Why did you closed it? Same problem, added env variable, added lint skip in Anchor config. With workspace that has multiple programs, all those solutions does not work. Using Anchor 0.30.1 and Solana 1.18.18

real-felix commented 3 months ago

Strange that the issue is closed, we have the same issue. Why did you closed it? Same problem, added env variable, added lint skip in Anchor config. With workspace that has multiple programs, all those solutions does not work. Using Anchor 0.30.1 and Solana 1.18.18

Add "rust-analyzer.cargo.features": [] in .vscode/settings.json.

ranile commented 3 months ago

This is also causing CI to fail when running cargo clippy --all-features. Not running clippy is not an option so this needs a fix

cikacule commented 2 months ago

This issue persists regardless of anchor version (0.30.1) or setting skip-lint=true in Anchor.toml. I also do not have VS Code installed so there is no settings.json to modify, but the error is still here.. Any other suggestions?image

statikdev commented 2 months ago

I have ran into this issue and cannot find a workaround to get my program building anymore.

Cargo.toml

[package]
name = "****"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "****"

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

[dependencies]
anchor-lang = { version = "0.30.1", features = ["init-if-needed"] }
anchor-spl = { version = "0.30.1", features = ["idl-build", "metadata"] }
mpl-token-metadata = { version = "4.1.2" }
solana-program = "1.18.0"

Anchor.toml

[toolchain]

[features]
resolution = true
skip-lint = true

[programs.localnet]
launch = "****"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "Localnet"
wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

Any other suggestions?

ngundotra commented 2 months ago

^+1 also having this issue. I'm able to compile the program fine using declare_program!() but I cannot seem to build an IDL when using declare_program!()

acheroncrypto commented 2 months ago

^+1 also having this issue. I'm able to compile the program fine using declare_program!() but I cannot seem to build an IDL when using declare_program!()

This issue is unrelated to declare_program!. Make sure you're using anchor-cli 0.30.1 (any older version won't work).

ngundotra commented 2 months ago

Here's a repro: https://github.com/ngundotra/spl-account-compression-idl-issue/tree/declare-program-fix

In this example, even with anchor-cli v0.30.1 I still can't get the IDL to build. When I run anchor build I get the following output:

error: custom attribute panicked
  --> programs/spl-account-compression-idl-issue/src/lib.rs:18:1
   |
18 | #[program]
   | ^^^^^^^^^^
   |
   = help: message: Safety checks failed: Failed to get program path
ngundotra commented 2 months ago

Found the issue. Was creating program id via Pubkey::from_str(...).unwrap() that was throwing inside the cargo test __anchor_private_print_idl --features idl-build step, which hides errors (rightfully).

I wonder if it's worth exposing a --verbose flag that allows idl-build errors to be surfaced in the cargo test __anchor_private_print_idl stage

acheroncrypto commented 2 months ago

I wonder if it's worth exposing a --verbose flag that allows idl-build errors to be surfaced in the cargo test __anchor_private_print_idl stage

This is not documented but you can do

ANCHOR_LOG=true anchor build
statikdev commented 2 months ago

I have ran into this issue and cannot find a workaround to get my program building anymore.

Cargo.toml

[package]
....

[dependencies]
anchor-lang = { version = "0.30.1", features = ["init-if-needed"] }
anchor-spl = { version = "0.30.1", features = ["idl-build", "metadata"] }
mpl-token-metadata = { version = "4.1.2" }
solana-program = "1.18.0"

Any other suggestions?

found my issue. I needed to remove "idl-build" from anchor-spl dependency.

acheroncrypto commented 2 months ago

@ngundotra @statikdev FYI, enabling the idl-build feature by default should always be avoided (see https://github.com/jito-foundation/stakenet/pull/48#discussion_r1659971579).

There will be warnings and errors that guide the user to the correct usage of IDL build in the next version (e.g. https://github.com/coral-xyz/anchor/pull/3061, https://github.com/coral-xyz/anchor/pull/3133).

ngundotra commented 2 months ago

@acheroncrypto running ANCHOR_LOG=true anchor build did not actually show me logs of why building the IDL failed. I just see this: Error: Building IDL failed

side note: I love declare_program!() it is fricking awesome.

acheroncrypto commented 2 months ago

@ngundotra Ah, it was returning early (before logging could happen) when the underlying build command failed. Fixed it and improved the build error message in https://github.com/coral-xyz/anchor/pull/3284.

dhl commented 1 month ago

I ran into the same issue with RustRover 2024.2.3. Ran a trace and found the macro expansion failed with the "Failed to get program path" error, resulting in all code completion failing.

What worked for me is explicitly setting ANCHOR_IDL_BUILD_PROGRAM_PATH to where the Anchor program is.

Until the patch from @acheroncrypto (thanks!) gets released, just updating the ANCHOR_IDL_BUILD_PROGRAM_PATH environment variable to the Anchor program being worked on should keep us going.