anchorで「[dependencies] section, which is not allowed」エラー

現象

anchor testを実行すると以下のエラー。

% anchor test
[2024-02-18T15:05:09.634883000Z ERROR cargo_build_sbf] Failed to obtain package metadata: `cargo metadata` exited with an error: error: failed to parse manifest at `/user/Documents/Programming/Blockchain/solana-anchor-react-minimal-example/anchor/transfer-sol/Cargo.toml`

    Caused by:
      this virtual manifest specifies a [dependencies] section, which is not allowed

/Cargo.toml

[workspace]
members = [
    "programs/*"
]

[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1

[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1

[dependencies]
anchor-lang = "0.29.0"
anchor-spl = "0.29.0"

原因

dependencies を記載する Cargo.toml の場所が間違っていた。

Cargo.tomlは2つファイルがあり、

  • /Cargo.toml
  • /programs/パッケージ名/Cargo.toml

誤って /Cargo.toml に dependencies を記載していた。
/programs/パッケージ名/Cargo.toml に記載する必要がある。

対応

/Cargo.toml
誤って記載した dependencires を削除。

[workspace]
members = [
    "programs/*"
]

[profile.release]
overflow-checks = true
lto = "fat"
codegen-units = 1

[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1

/programs/パッケージ名/Cargo.toml
dependencies を記載。

[package]
name = "transfer-sol"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

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

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.29.0"
anchor-spl = "0.29.0"