dtolnay/cargo-expand

cargo expand --ugly fails with "'rustfmt' is not available for the 'nightly-x86_64-apple-darwin' toolchain"

GregOwen opened this issue · 1 comments

I've been trying to use cargo expand to debug a Rust macro in one of my projects, but ran into issues because rustfmt wasn't available for the version of nightly that I had installed. I tried using cargo expand --ugly, but I still got an error about rustfmt not being available:

$ cargo expand --ugly
    Checking serde v1.0.117
   Compiling foo v0.1.0 (/Users/greg/foo)
error: failed to run custom build command for `foo v0.1.0 (/Users/greg/foo)`
Caused by:
  process didn't exit successfully: `/Users/greg/foo/target/debug/build/foo-ed8828f9fdab06c3/build-script-build` (exit status: 1)
  --- stderr
  error: the 'rustfmt' component which provides the command 'rustfmt' is not available for the 'nightly-x86_64-apple-darwin' toolchain
warning: build failed, waiting for other jobs to finish...
error: build failed
$ cargo +nightly version
cargo 1.53.0-nightly (f3e13226d 2021-04-30)
$ cargo expand --version
cargo-expand 1.0.6

I've resolved the issue myself, so posting here to help anybody in the future:

The problem is that my build.rs invoked code that relied on rustfmt (specifically, tonic_build::compile_protos, which relies on rustfmt by default). So even though cargo expand --ugly wasn't pulling in rustfmt, my build.rs was. The clue that put me on this track was that the error complains about build-script-build.

The solution was to downgrade my install of nightly to the most recent version that supports rustfmt:

$ rustup toolchain install nightly --allow-downgrade -c rustfmt

I could also have avoided this by changing my Cargo.toml to disable the rustfmt feature of tonic_build.