构建失败,错误:即使成功使用rustup override设置了nightly版本,Pear也需要'dev'或'nightly'版本的rustc。

6
  • 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)
  • rocket = "0.4.4"

我正在尝试使用rocket构建一个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

1
这可能是环境变量的问题。如果您尝试运行 cargo +nightly build,它是否有效? - transistor
2
除非你将 RUSTUP_TOOLCHAIN 环境变量设置为 "stable" 或其他非每夜版工具链,否则无法复现。该变量和直接在命令中使用(如 @transistor 所提到的)是唯一优先级高于目录覆盖的方法,请参见此处 - kmdreko
1
当我尝试执行 cargo +nightly build 时,它给我发送了一个错误:error: no such subcommand: +nightly @transistor - Gabriel Gutierrez
嗯,如果你遇到了这个错误,那么你的 rustup/cargo 安装可能存在问题。当你运行 cargo 时,它应该执行由 rustup 安装的版本,否则它将无法使用 rustup 工具链。你可以尝试删除并重新安装 rustup,看看是否可以解决问题。 - transistor
2
我已经重新安装了 Rust,而且它运行良好,感谢各位的帮助! - Gabriel Gutierrez
显示剩余2条评论
3个回答

16

我在使用 Rocket 框架时遇到了类似的问题,错误信息也相同:Error: Pear requires a 'dev' or 'nightly' version of rustc.

如果您访问 rocket 框架的入门页面,会看到以下提示:“Rocket 大量使用 Rust 的语法扩展和其他高级、不稳定的特性。因此,我们需要使用 Rust 的 nightly 版本。”

我的问题是我没有使用 Rust 的 nightly 版本。在我的项目目录中,在终端中运行以下命令解决了我的问题。

rustup override set nightly

之后,如果你检查该目录的货物版本,则会得到以下结果:

cargo version

您将确认它已切换到夜间版


1

我的问题出在 rust-analyser 上,因为多个 rocket 依赖需要 rustc 的 nightlydev 版本,所以它无法启动。

以下步骤解决了我的问题:

  1. 通过在应用程序文件夹中运行 rustup override set nightly 切换到我的 rocket 项目的 nightly 版本。
  2. 删除项目中的所有目标文件夹(我还有一个在根目录中)。
  3. 手动从 cargo 缓存中删除有问题的缓存包。 cd ~/.cargo/registry/cache/github.com-xxxxxxxxxxxx && rm -r pear_codegen-0.1.5/

0

即使使用了 nightly,编译仍然失败

看起来您的一些依赖关系已经过时(pear-codegen 可能是导致问题的原因之一),更新这些可能会解决编译问题。

如何覆盖工具链的通用注意事项

使用 rustup 的 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"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接