I've run into this error with Anchor a few times. It occurs when something in your repository structure doesn't align with what Anchor or Cargo are looking for.
Anchor is pretty particular about the repository structure for your program, so here's the key naming conventions to look out for:
Anchor.toml
- Make sure your program's name under [programs.localnet]
is represented in snake case (ie. program_name
).
programs/<program-name>
- Make sure the folder your program lives in is named with hyphens (ie. programs/program-name
).
programs/<program-name>/Cargo.toml
- There's two spots in here, the top one under [package]
should say name = program-name
in hyphen format, and the second one under [lib]
should say name = program_name
in snake case.
programs/<program-name>/lib.rs
- Verify the name of your module is the snake case name of your program (ie. pub mod program_name
)
- ^ The ones above are enough to get
anchor build
working, but you'll also want to change the IDL in the test TS file (tests/*.ts
) to match your program name (ie. import { ProgramName } from "../target/types/program_name";
)
When you think you've got it all named correctly, delete your target
folder before building.
Hope this helps, it trips me up all the time.
EDIT:
I left the above information in case someone does have an issue with naming conventions.
Windows Issues:
I reproduced your issue on Windows since I also typically use WSL2.
I was seeing the following behavior:
Inside the project and VS Code PowerShell terminal:
anchor build
-> Throws (same as OP):
PS C:\Users\<username>\mysolanaapp> anchor build
Failed to obtain package metadata: `cargo metadata` exited with an error: error: failed to load manifest for workspace member `\\?\C:\Users\<username>\mysolanaapp\programs\*`
Inside the project and VS Code PowerShell terminal:
cargo build-bpf
-> Throws:
Can't get home directory path: environment variable not found
This last one was key. The PowerShell terminal in VS Code can't find the environment variable HOME
.
Solution:
You just need to set this variable in the current VS Code PowerShell terminal:
$Env:HOME = 'C:\Users\<username>'
Or you can choose to set it permanently, by running this in a PowerShell administrator shell:
[Environment]::SetEnvironmentVariable('HOME', 'C:\Users\<username>')
Then close the terminal in VS Code then close VS Code and re-open it (you have to close the terminal first or VS Code will reload it).
After solving this issue I then ran into a privilege error when Cargo tried to download BPF tooling:
A required privilege is not held by the client.
Open Settings > Windows Update > For Developers
and enable Developer Mode.
Alternate Solution:
You can also choose to run Bash shells in VS Code instead of PowerShell, where HOME
will be set automatically for you.
See [2] in Sources
Sources:
Cargo.toml
for the program thats failing.