coral-xyz / anchor

βš“ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.6k stars 1.32k forks source link

Getting started guide doesn't mention npm installation, which results in Error: No such file or directory (os error 2) when running "anchor init" #3281

Open shawazi opened 1 week ago

shawazi commented 1 week ago

Problem:

anchor init anchor-counter --template multiple has an OS error on Arch Linux. However, the project is created despite the error. The error occurs every time the command is run.

The solutions at https://solana.stackexchange.com/questions/1648/error-no-such-file-or-directory-os-error-2-error-from-anchor-test don't seem to solve the underlying issue - upon running the anchor init command for multiple files template, a developer shouldn't see an OS error. If it's occurring due to Anchor being unable to generate an IDL from the template, then the template should possibly include the boilerplate necessary to generate a basic IDL.

 ╭─dev@dev in ~/Code took 4ms
[πŸ”΄] Γ— node --version
v22.9.0

 ╭─dev@dev in ~/Code took 6ms
 ╰─λ yarn --version
1.22.22

 ╭─dev@dev in ~/Code took 173ms
 ╰─λ solana --version
solana-cli 1.18.18 (src:83047136; feat:4215500110, client:SolanaLabs)

 ╭─dev@dev in ~/Code took 3ms
 ╰─λ avm --version
avm 0.30.1

 ╭─dev@dev in ~/Code took 22ms
 ╰─λ anchor --version
anchor-cli 0.30.1

 ╭─dev@dev in ~/Code took 3ms
[πŸ”΄] Γ— anchor init anchor-counter --template multiple

Error: No such file or directory (os error 2)

 ╭─dev@dev in ~/Code took 4ms
[πŸ”΄] Γ— ls
drwxr-xr-x - dev 25 Sep 19:31 ξ—Ώ anchor-counter

 ╭─dev@dev in ~/Code/anchor-counter via  v1.81.0 as πŸ§™ took 8s
[πŸ”] Γ— tree
.
β”œβ”€β”€ Anchor.toml
β”œβ”€β”€ app
β”œβ”€β”€ Cargo.toml
β”œβ”€β”€ migrations
β”œβ”€β”€ programs
β”‚Β Β  └── anchor-counter
β”‚Β Β      β”œβ”€β”€ Cargo.toml
β”‚Β Β      β”œβ”€β”€ src
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ constants.rs
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ error.rs
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ instructions
β”‚Β Β      β”‚Β Β  β”‚Β Β  β”œβ”€β”€ initialize.rs
β”‚Β Β      β”‚Β Β  β”‚Β Β  └── mod.rs
β”‚Β Β      β”‚Β Β  β”œβ”€β”€ lib.rs
β”‚Β Β      β”‚Β Β  └── state
β”‚Β Β      β”‚Β Β      └── mod.rs
β”‚Β Β      └── Xargo.toml
└── target
    └── deploy
        └── anchor_counter-keypair.json

10 directories, 11 files

Solution:

Add the boilerplate code to the dummy files that are causing the OS error on anchor init anchor-counter --template multiple

acheroncrypto commented 1 week ago

I'm not able to reproduce the problem unfortunately. Does it only happen if you use the multiple template?

The solutions at https://solana.stackexchange.com/questions/1648/error-no-such-file-or-directory-os-error-2-error-from-anchor-test don't seem to solve the underlying issue

The Error: No such file or directory (os error 2) error is a generic file system error, so it's difficult to pinpoint where exactly it's coming from. Searching the web for this error is unlikely to yield good results because of this reason.

If it's occurring due to Anchor being unable to generate an IDL from the template, then the template should possibly include the boilerplate necessary to generate a basic IDL.

The post is from 2 years ago when Anchor had a different way of generating the IDLs. It's also running the anchor test command, so this problem is unrelated (other than the error itself).

This would be easy to debug if I could reproduce.

shawazi commented 1 week ago

Hey, thanks for the response! I realized why it occurred. I didn't have npm installed.

https://www.anchor-lang.com/docs/installation I was following this guide on a fresh system (i believe garuda (arch) with fish, but maybe bash).

I installed node directly, I believe, rather than via nvm. I think nvm may provide node and npm, possibly, and perhaps that's why the anchor-lang installation guide doesn't mention it.

It might be beneficial to add a section in the installation guide for "node" and "npm" (or "nvm" if that has both node and npm), alongside the checks for Rust and Yarn.

acheroncrypto commented 1 week ago

It might be beneficial to add a section in the installation guide for "node" and "npm" (or "nvm" if that has both node and npm), alongside the checks for Rust and Yarn.

Possibly, but I'd prefer to handle this error in the CLI itself (provide a good error message) because the installation guide has been getting longer lately, which could be scary for new people.

Since this problem is unrelated to the multiple files template, it would be great if we could either close this issue, or rename it to something that includes npm.

shawazi commented 1 week ago

I see your point, but as a new person following the official documentation, I hit a 30 minute roadblock that hindered additional javascript testing since anchor didn't create the tests directory or package.json, or tell me exactly what went wrong.

It might save a lot of time collectively if the documentation contains the two additional lines to check that the new anchor user has node and npm installed on their system, such as via:

### Node and NPM
Install them both onto your system by following this guide: https://www.freecodecamp.org/news/node-version-manager-nvm-install-guide/

I really think a new person would prefer to have the proper dependencies listed in the getting started guide, rather than playing guessing games with the ambiguous CLI error message. And if the documentation is comprehensive, then there's no mental discrepancy (the documentation for installation doesn't mention node or npm, but the cli error message does? why?)