@@ -126,31 +126,35 @@ impl fmt::Display for Location {
126126 }
127127}
128128
129- fn rustfmt_executable ( sh : & Shell ) -> & str {
130- // First try explicitly requesting the stable channel via rustup in case nightly is being used by default,
131- // then plain rustfmt in case rustup isn't being used to manage the compiler (e.g. when using Nix).
132- for executable in [ "rustup run stable rustfmt" , "rustfmt" ] {
133- let version = cmd ! ( sh, "{executable} --version" ) . read ( ) . unwrap_or_default ( ) ;
134- if version. contains ( "stable" ) {
135- return executable;
136- }
137- }
138-
139- panic ! (
140- "Failed to run rustfmt from toolchain 'stable'. \
141- Please run `rustup component add rustfmt --toolchain stable` to install it.",
142- ) ;
143- }
144-
145129fn reformat ( text : String ) -> String {
146130 let sh = Shell :: new ( ) . unwrap ( ) ;
147- let rustfmt_exe = rustfmt_executable ( & sh) ;
148131 let rustfmt_toml = project_root ( ) . join ( "rustfmt.toml" ) ;
149- let mut stdout =
150- cmd ! ( sh, "{rustfmt_exe} --config-path {rustfmt_toml} --config fn_single_line=true" )
151- . stdin ( text)
152- . read ( )
153- . 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+ } ;
154158 if !stdout. ends_with ( '\n' ) {
155159 stdout. push ( '\n' ) ;
156160 }
0 commit comments