错误:工具链“stable-x86_64-apple-darwin”没有二进制文件“rustfmt”。

30

我运行了 rustup update 来更新我的工具链,并看到了两个警告:

warning: tool `rustfmt` is already installed, remove it from `/Users/<username>/.cargo/bin`, then run `rustup update` to have rustup manage this tool.
warning: tool `cargo-fmt` is already installed, remove it from `/Users/<username>/.cargo/bin`, then run `rustup update` to have rustup manage this tool.

我按照警告信息中的说明进行了操作,然后尝试再次运行rustfmt。但是我收到了一个错误。

error: toolchain 'stable-x86_64-apple-darwin' does not have the binary rustfmt`
什么出了问题,我应该如何修复?

我认为你问题的第一部分并不相关 - 现在你只是在干净的rustup安装中遇到了rustfmt的问题。我也是这样。 - theicfire
3个回答

23

确保在您的Rustup工具链中安装了rustfmt组件是拥有rustfmt的最标准和可靠的方式。

rustup component add rustfmt

或者针对特定的工具链:

rustup component add rustfmt --toolchain nightly-2020-06-09

有可能在夜间工具链中测试和构建失败,这意味着这些工具链不太可能始终拥有此组件。根据无工具破坏周政策,最新的稳定版测试版工具链通常会拥有它。

为了让 Rustup 管理 rustfmt,请按照以下步骤进行:

  1. 将 Rustup 更新到最新版本后,您可能会收到消息:warning: tool rustfmt is already installed。按建议从 Cargo 的二进制文件夹中移除二进制文件。cargo uninstall rustfmt(如果您安装了它,则是 rustfmt-nightly)。
  2. 运行 rustup update,让其用其自己管理的 rustfmtcargo-fmt 填充已删除的二进制文件。
  3. 确保已安装要使用的工具链(例如 stable)。
  4. 运行上面的命令,以确保该工具链安装了 rustfmt 组件。

完成后,调用 rustfmt 将按预期工作:

$ rustup run stable rustfmt --version
rustfmt 1.4.12-stable (a828ffea 2020-03-11)

或者通过 Cargo 子命令:

$ cargo fmt --version
rustfmt 1.4.12-stable (a828ffea 2020-03-11)

在早期,由 Rustup 管理的 rustfmt 可能有点令人困惑,因为 Rustup 并不总是拥有 rustfmt,而且它经常作为预览组件出现,必须以 rustfmt-preview 的名称安装。关于这个问题有一些相关的 Issues 和 PRs(#1305#1310)。


1
要在该工具链中添加 rustfmt-preview,您还需要安装工具链:rustup install nightly-2017-12-20-x86_64-apple-darwin - theicfire

18

这个错误告诉你在实际的 *-apple-darwin 上未安装 rustfmt-preview

你需要做的是:

rustup component add rustfmt-preview --toolchain stable-x86_64-apple-darwin

添加完成后就可以了 :)


4
谢谢!这正是我需要的“默认”新手安装。 - xdg

1
$ rustup run stable rustfmt --version
error: `toolchain 'stable-x86_64-pc-windows-msvc' does not have th`e binary `rustfmt.exe`

$ rustup component remove rustfmt-preview --toolchain=stable-x86_64-pc-windows-msvc
info: removing component 'rustfmt-preview'
warning: during uninstall component rustfmt-preview-x86_64-pc-windows-msvc was not found

$ rustup component add rustfmt-preview --toolchain=stable-x86_64-pc-windows-msvc
info: downloading component 'rustfmt-preview'
info: installing component 'rustfmt-preview'

$ rustup run stable rustfmt --version
rustfmt 0.99.1-stable (da17b689 2018-08-04)

https://users.rust-lang.org/t/problem-with-rustfmt-on-stable/15165/7


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