Skip to content

Commit 64c538f

Browse files
committed
Auto merge of #18009 - Veykril:reformat-no-rustup, r=Veykril
fix: do not assume rustup is installed in xtask codegen take 2 7d9e4fc broke this on rustup toolchains, the `cmd` command is trying to be too smart here
2 parents 1cb4266 + 3ad54a7 commit 64c538f

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

xtask/src/codegen.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
145129
fn 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

Comments
 (0)