@@ -126,27 +126,35 @@ impl fmt::Display for Location {
126
126
}
127
127
}
128
128
129
- fn ensure_rustfmt ( sh : & Shell ) {
130
- let version = cmd ! ( sh, "rustup run stable rustfmt --version" ) . read ( ) . unwrap_or_default ( ) ;
131
- if !version. contains ( "stable" ) {
132
- panic ! (
133
- "Failed to run rustfmt from toolchain 'stable'. \
134
- Please run `rustup component add rustfmt --toolchain stable` to install it.",
135
- ) ;
136
- }
137
- }
138
-
139
129
fn reformat ( text : String ) -> String {
140
130
let sh = Shell :: new ( ) . unwrap ( ) ;
141
- ensure_rustfmt ( & sh) ;
142
131
let rustfmt_toml = project_root ( ) . join ( "rustfmt.toml" ) ;
143
- let mut stdout = cmd ! (
144
- sh,
145
- "rustup run stable rustfmt --config-path {rustfmt_toml} --config fn_single_line=true"
146
- )
147
- . stdin ( text)
148
- . read ( )
149
- . unwrap ( ) ;
132
+ let version = cmd ! ( sh, "rustup run stable rustfmt --version" ) . read ( ) . unwrap_or_default ( ) ;
133
+
134
+ // First try explicitly requesting the stable channel via rustup in case nightly is being used by default,
135
+ // then plain rustfmt in case rustup isn't being used to manage the compiler (e.g. when using Nix).
136
+ let mut stdout = if !version. contains ( "stable" ) {
137
+ let version = cmd ! ( sh, "rustfmt --version" ) . read ( ) . unwrap_or_default ( ) ;
138
+ if !version. contains ( "stable" ) {
139
+ panic ! (
140
+ "Failed to run rustfmt from toolchain 'stable'. \
141
+ Please run `rustup component add rustfmt --toolchain stable` to install it.",
142
+ ) ;
143
+ } else {
144
+ cmd ! ( sh, "rustfmt --config-path {rustfmt_toml} --config fn_single_line=true" )
145
+ . stdin ( text)
146
+ . read ( )
147
+ . unwrap ( )
148
+ }
149
+ } else {
150
+ cmd ! (
151
+ sh,
152
+ "rustup run stable rustfmt --config-path {rustfmt_toml} --config fn_single_line=true"
153
+ )
154
+ . stdin ( text)
155
+ . read ( )
156
+ . unwrap ( )
157
+ } ;
150
158
if !stdout. ends_with ( '\n' ) {
151
159
stdout. push ( '\n' ) ;
152
160
}
0 commit comments