构建失败并出现错误:即使在每晚成功设置 rustup 覆盖后,Pear 也需要 rustc 的“开发”或“每晚”版本

问题描述

  • Windows 10
  • rustup 1.23.1 (3df2264a9 2020-11-30)
  • 认 rustc 1.50.0 (cb75ad5db 2021-02-10)
  • 项目 rustc 1.52.0-nightly (4a8b6f708 2021-03-11)
  • 火箭 = "0.4.4"

我正在尝试用火箭构建一个 Rust 项目,但在编译时我总是遇到这个错误,即使在成功覆盖项目的工具链之后:

D:\GitHub\Learning-Rust\poke_api> rustup override set nightly
info: using existing install for 'nightly-x86_64-pc-windows-msvc'
info: override toolchain for 'D:\GitHub\Learning-Rust\poke_api' set to 'nightly-x86_64-pc-windows-msvc'

  nightly-x86_64-pc-windows-msvc unchanged - rustc 1.52.0-nightly (4a8b6f708 2021-03-11)

PS D:\GitHub\Learning-Rust\poke_api> cargo build
   Compiling winapi v0.3.9
   Compiling serde_derive v1.0.124
   Compiling rocket v0.4.7
   Compiling pear_codegen v0.1.4
   Compiling rocket_codegen v0.4.7
   Compiling proc-macro2 v1.0.24
   Compiling pq-sys v0.4.6
   Compiling aho-corasick v0.6.10
   Compiling serde_json v1.0.64
error: Failed to run custom build command for `pear_codegen v0.1.4`

Caused by:
  process didn't exit successfully: `D:\GitHub\Learning-Rust\poke_api\target\debug\build\pear_codegen-e182711746033ac9\build-script-build` (exit code: 101)
  --- stderr
  Error: Pear requires a 'dev' or 'nightly' version of rustc.
  Installed version: 1.48.0 (2020-11-16)
  Minimum required:  1.31.0-nightly (2018-10-05)
  thread 'main' panicked at 'Aborting compilation due to incompatible compiler.',C:\Users\gabre\.cargo\registry\src\github.com-1ecc6299db9ec823\pear_codegen-0.1.4\build.rs:24:13
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build Failed,waiting for other jobs to finish...
error: build Failed

解决方法

我在使用火箭时遇到了类似的问题。同样的错误信息,Error: Pear requires a 'dev' or 'nightly' version of rustc.

如果您访问火箭框架网站上的 get-started 页面。它说,“Rocket 大量使用了 Rust 的语法扩展和其他高级的、不稳定的功能。因此,我们需要使用 Rust 的夜间版本。”

我的问题是我没有使用 Rust 的夜间版本。在我的项目目录中的终端上运行它对我有用。

rustup override set nightly

如果您之后检查该目录的货物版本,

cargo version

你会确认它已经切换了夜间版本

,

即使使用 nightly 也无法编译

看起来您有一些过时的依赖项(pear-codegen 可能是导致问题的依赖项),更新这些依赖项可能会解决编译问题。

关于如何覆盖工具链的一般说明

使用 rustups override 可以正常工作,但它绑定到您本地的 rustup 配置并且未在项目中指定。

为了实现这一点,从而使项目更具可移植性并允许其他人始终使用正确的工具链,我建议使用 toolchain file。它看起来像这样(示例取自链接页面),并且只会为包含的项目准确指定所需的工具链。

# rust-toolchain.toml
[toolchain]
channel = "nightly-2020-07-10"
components = [ "rustfmt","rustc-dev" ]
targets = [ "wasm32-unknown-unknown","thumbv2-none-eabi" ]
profile = "minimal"

就您的目的而言,像这样的简单配置很可能就是您所需要的,尽管添加您想要使用的组件会有所帮助。

[toolchain]
channel = "nightly"