Substrate添加Nicks Pallet后执行cargo check编译报错如何解决?
问题核心原因
报错本质是依赖版本不匹配:你使用的substrate node template是2021-09的月度版本,但引入pallet-nicks时没有指定和本地框架一致的版本,导致Cargo拉取了最新版本的pallet,和现有runtime依赖的substrate核心库版本冲突,才会出现重复lang项、trait实现不匹配等大量报错。
解决步骤
- 修正
runtime/Cargo.toml里的pallet-nicks依赖声明
将pallet-nicks的版本、git分支/commit配置改成和项目内其他substrate pallet(比如frame-support、frame-system)完全一致的配置,不要使用默认最新版本。
参考配置示例:
如果你项目内原有pallet依赖是这样写的:
那pallet-nicks也要对应写成:frame-support = { version = "3.0.0-monthly-2021-09", git = "https://github.com/paritytech/substrate.git", branch = "monthly-2021-09", default-features = false }
同时不要漏了在pallet-nicks = { version = "3.0.0-monthly-2021-09", git = "https://github.com/paritytech/substrate.git", branch = "monthly-2021-09", default-features = false }runtime/Cargo.toml的[features]段的std列表里加上pallet-nicks/std。 - 锁定匹配的Rust工具链版本
2021-09的月度版本substrate需要对应时期的nightly工具链,在项目根目录执行以下命令即可锁定:rustup override set nightly-2021-09-30 rustup target add wasm32-unknown-unknown --toolchain nightly-2021-09-30 - 清理缓存重新执行检查
cargo clean rm Cargo.lock cargo check -p node-template-runtime
额外说明
你看到的unused doc comment警告不影响编译,把写在construct_runtime!宏上面的三斜杠文档注释改成普通双斜杠注释即可消除该警告。
内容的提问来源于stack exchange,提问作者Asad Hayat
相关产品推荐
相关产品推荐

