error: toolchain 'stable-x86_64-apple-darwin' does not have the binary `rustfmt`

error: toolchain 'stable-x86_64-apple-darwin' does not have the binary `rustfmt`

我已经 运行 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`

出了什么问题,我该如何解决?

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

rustup component add rustfmt

或特定工具链:

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

nightly 工具链中的测试和构建有可能会失败,这意味着那些工具链不太可能总是包含此组件。最新的 stablebeta 工具链通常会根据 No Tool Breakage Week 政策拥有它。

为了让Rustup管理rustfmt,请看以下步骤:

  1. 将 Rustup 更新到最新版本后,您可能会收到消息 warning: tool rustfmt is already installed。按照建议从 Cargo 的二进制文件夹中删除二进制文件。 cargo uninstall rustfmt(或 rustfmt-nightly,如果您安装了它)运行良好。
  2. 运行 rustup update 让它用自己管理的 rustfmtcargo-fmt.
  3. 填充已删除的二进制文件
  4. 确保安装了您希望使用的工具链(例如 stable
  5. 运行 上面的命令也确保为该工具链安装了 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 下。有一些关于该主题的相关问题和 PR (#1305 and #1310)。

错误告诉你你没有 rustfmt-preview 没有安装在实际的 *-apple-darwin 上。

您需要做的是:

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

之后你就可以走了:)

$ 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