I ran into a few odd errors when compiling a Solana program:
$ npm run build > hello-world@1.0.0 build > cargo build-bpf --manifest-path=./src/program/Cargo.toml --bpf-out-dir=./dist/program BPF SDK: /Users/matt/.local/share/solana/install/releases/1.8.6/solana-release/bin/sdk/bpf cargo-build-bpf child: rustup toolchain list -v cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release Compiling program v0.0.1 (/Users/matt/code/solana/hello-world/src/program) error[E0433]: failed to resolve: maybe a missing crate `solana_program`? --> src/lib.rs:3:5 | 3 | use solana_program::{ | ^^^^^^^^^^^^^^ maybe a missing crate `solana_program`? error[E0432]: unresolved import `borsh` --> src/lib.rs:1:5 | 1 | use borsh::{BorshDeserialize, BorshSerialize}; | ^^^^^ maybe a missing crate `borsh`? error[E0432]: unresolved import `solana_program` --> src/lib.rs:3:5 | 3 | use solana_program::{ | ^^^^^^^^^^^^^^ maybe a missing crate `solana_program`?
Took a while to figure out, but in the end the reason was that I was missing an edition
value in Cargo.toml
.
I had:
[package] name = "program" version = "0.0.1"
But needed:
[package] name = "program" version = "0.0.1" edition = "2021"
Note that the name
and package
can be whatever you want here.
The edition is automatically set when you use cargo new
, but at some point I had deleted it when cleaning up the manifest. Without it set, Rust assumes the edition is 2015 for backwards compatibility, but this causes problems during the Solana program compilation.