Cargo test本地通过但GitHub Actions运行失败问题求助
问题描述
单元测试执行时抛出如下错误:
/home/runner/.cargo/bin/cargo unit-test --locked Updating crates.io index Updating git repository `https://github.com/enigmampc/SecretNetwork` Updating git repository `https://github.com/Haseeb30000/secret-toolkit` Downloading crates ... Downloaded rand_chacha v0.2.2 ..... ..... Downloaded rust_decimal v1.25.0 error: failed to parse manifest at `/home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/rust_decimal-1.25.0/Cargo.toml` Error: failed to parse manifest at `/home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/rust_decimal-1.25.0/Cargo.toml` Caused by: failed to parse the `edition` key Caused by: this version of Cargo is older than the `2021` edition, and only supports `2015` and `2018` editions. Error: The process '/home/runner/.cargo/bin/cargo' failed with exit code 101
- 本地环境为M1芯片MacBook Pro,执行
cargo build、cargo unit-test、cargo test均正常通过 - GitHub Actions流水线执行时稳定复现上述错误,多次调整
rust_decimal依赖版本(从1.17.0升级到1.25.0)均无法解决 - 项目Cargo.toml配置如下:
[package] name = "pool_party" version = "0.1.0" edition = "2018" exclude = [ "contract.wasm", "hash.txt", ] [lib] crate-type = ["cdylib", "rlib"] [profile.release] opt-level = 3 debug = false rpath = false lto = true debug-assertions = false codegen-units = 1 panic = 'abort' incremental = false overflow-checks = true [features] backtraces = ["cosmwasm-std/backtraces"] [dependencies] cosmwasm-std = { git = "https://github.com/enigmampc/SecretNetwork", tag = "v1.0.0", features = ["staking"] } cosmwasm-storage = { git = "https://github.com/enigmampc/SecretNetwork", tag = "v1.0.0" } secret-toolkit = { git = "https://github.com/Haseeb30000/secret-toolkit", branch = "master" } schemars = "0.7" serde = { version = "1.0.103", default-features = false, features = ["derive"] } serde_json = "1.0.1" snafu = { version = "0.6.3" } bincode2 = "2.0.1" subtle = { version = "2.2.3", default-features = false } base64 = "0.12.3" hex = "0.4.2" rust_decimal = { version = "1.25.0", default-features = false} rand_chacha = { version = "0.2.2", default-features = false } rand = { version = "0.7.3" } sha2 = { version = "0.9.1", default-features = false } [dev-dependencies] cosmwasm-schema = { version = "0.10.0" }
- CI流水线yaml配置如下:
on: [push, pull_request] name: Basic jobs: test: name: Test Suite runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v2 - name: Install stable toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: 1.46.0 target: wasm32-unknown-unknown override: true - name: Run unit tests uses: actions-rs/cargo@v1 with: command: unit-test args: --locked env: RUST_BACKTRACE: 1 - name: Compile WASM contract uses: actions-rs/cargo@v1 with: command: wasm args: --locked env: RUSTFLAGS: "-C link-arg=-s" - name: Run integration tests uses: actions-rs/cargo@v1 with: command: integration-test args: --locked lints: name: Lints runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v2 - name: Install stable toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: 1.46.0 override: true components: rustfmt, clippy - name: Run cargo fmt uses: actions-rs/cargo@v1 with: command: fmt args: --all -- --check - name: Run cargo clippy uses: actions-rs/cargo@v1 with: command: clippy args: -- -D warnings - name: Generate Schema uses: actions-rs/cargo@v1 with: command: schema args: --locked
根因分析
错误日志已经明确给出原因:当前CI环境安装的Cargo版本过旧,仅支持2015、2018 edition,无法解析使用2021 edition的依赖包。
Rust从1.56.0版本开始正式支持2021 edition,CI配置中硬编码使用1.46.0版本的工具链,远低于支持2021 edition的最低版本要求;本地环境Rust版本高于1.56.0,因此可以正常解析依赖、完成编译测试。rust_decimal 1.24.0及以上版本默认使用2021 edition,因此无论将该依赖调整到1.25.0还是更高的新兼容版本,在1.46.0的工具链下都会触发相同的解析错误,和项目本身配置的edition = "2018"没有关系。
解决方案
二选一即可,优先选择第一种:
- 方案1(推荐):升级CI环境的Rust工具链版本
将CI配置中所有硬编码的toolchain: 1.46.0替换为≥1.56.0的版本,考虑到SecretNetwork合约的兼容性要求,选择1.60.0稳定版即可,test、lints两个job的工具链配置都要同步修改,修改示例:
改完后Cargo可以正常识别2021 edition的依赖配置,错误直接消失。- name: Install stable toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: 1.60.0 target: wasm32-unknown-unknown override: true - 方案2:若有特殊要求必须锁死1.46.0版本工具链
将rust_decimal依赖降级到最后一个兼容Rust 1.46、使用2018 edition的版本(1.23.0),修改Cargo.toml中的依赖配置:
降级后需要本地跑全量测试,确认业务逻辑没有版本兼容问题。rust_decimal = { version = "=1.23.0", default-features = false}
内容的提问来源于stack exchange,提问作者Haseeb Saeed
相关产品推荐
相关产品推荐

