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

The most standard and reliable way to have rustfmt in your system is to ensure that the rustfmt component is installed in your Rustup toolchain.

rustup component add rustfmt

Or for a specific toolchain:

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

There is a possibility that the tests and builds in nightly toolchains fail, which means that those are not as likely to always have this component. The latest stable and beta toolchains will usually have it in accordance to the No Tool Breakage Week policy.

In order to let Rustup manage rustfmt, see the following steps:

  1. Once you update Rustup to the latest version, you may receive the message warning: tool rustfmt is already installed. Remove the binaries from Cargo's binary folder, as suggested. cargo uninstall rustfmt (or rustfmt-nightly if you installed that) works well.
  2. Run rustup update to let it fill in the deleted binaries with its own, managed rustfmt and cargo-fmt.
  3. Ensure that the toolchain that you wish to work with is installed (e.g. stable)
  4. Run the command above too ensure that the rustfmt component is installed for that toolchain.

With that done, calling rustfmt will work as intended:

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

Or via the Cargo subcommand:

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

In the early days, rustfmt managed by Rustup could have been a bit confusing, because Rustup did not always have rustfmt, and would still too often emerge as a preview component which had to be installed under the name rustfmt-preview. There are a few relevant issues and PRs on the subject (#1305 and #1310).


The Error tells you that you don't have the rustfmt-preview is not installed on the actual *-apple-darwin.

what you need to do is:

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

after you'll be good to go :)

Tags:

Rust

Rustup