anchor build 'failed to select a version for solana-program': mpl-core 0.9.1 pins anchor-spl ^0.30, not 0.31
anchor build fails in cargo metadata, before any compilation:
error: failed to select a version for `solana-program`.
... required by package `solana-zk-sdk v2.1.0`
... which satisfies dependency of `spl-token-2022 v6.0.0`
... which satisfies dependency of `anchor-spl v0.31.1`
versions that meet the requirements `=2.1.0` are: 2.1.0
all possible versions conflict with previously selected packages.
previously selected package `solana-program v2.2.1`
... required by `spl-associated-token-account v6.0.0`The message is unreadable at first pass, but it is the whole diagnosis: two paths through your graph demand incompatible solana-program versions, and one of them is pinned exactly (=2.1.0), so cargo has no room to unify. That exact pin is characteristic of the SPL/zk-sdk crates.
The trigger is usually a third-party Anchor-integrated crate lagging behind your Anchor version. Here mpl-core 0.9.1 (with the anchor feature) declares anchor-spl ^0.30, so pairing it with anchor-lang/anchor-spl 0.31.1 drags two generations of the SPL stack into one graph.
Resolution: check the dependency's own crates.io page for which anchor-spl range it declares, and align to that. Delete Cargo.lock, then:
cargo update anchor-spl --precise 0.30.1
cargo update anchor-lang --precise 0.30.1Reading method that generalises: in a failed to select a version error, find the line with = (the exact pin) and the previously selected package line — the two crates named at the tops of those chains are the pair you must reconcile. Downgrading solana-program directly, deleting ~/.cargo/registry, or reinstalling the Solana CLI will not help, because the conflict is declarative, not environmental.
Source: adapted from a Stack Exchange answer by abelmarnk (question by Asaboro Daniel), https://solana.stackexchange.com/questions/21856/failed-to-select-a-version-for-solana-program-error-on-anchor-0-31-1-since-upg — CC BY-SA 4.0
Checked with cargo 1.99.1 'cargo generate-lockfile' (resolution only, nothing compiled) on today's crates.io index. The diagnosis is right in spirit, with two precisions. (1) mpl-core 0.9.1 (and 0.10.0) declares anchor-lang ^0.30.0 (optional, 'anchor' feature), not anchor-spl. That brings in solana-program 1.18.26, whose curve25519-dalek 3.2.1 requires zeroize >=1,<1.4, while solana-zk-sdk 2.1 (via anchor-spl 0.31.1 -> spl-pod 0.5) needs zeroize ^1.7. (2) On the current index the error surfaces as 'failed to select a version for zeroize', not solana-program. The rendered conflict depends on index/lock state, but it's the same root cause: Solana 1.x and 2.x stacks in one graph. Results: anchor-lang+anchor-spl 0.31.1 alone -> rc 0. Adding mpl-core 0.9.1 -> rc 101. All-0.30.1 + mpl-core 0.9.1 -> rc 0 (your fix works). anchor 0.31.1 + mpl-core 0.11.1 (which declares anchor-lang ^0.31.1) -> rc 0. So the forward fix is bumping mpl-core to >= 0.11 rather than downgrading Anchor; mpl-core 0.12.1 moves on to ^0.32.1.