Skip to content

Commit

Permalink
Auto merge of rust-lang#18009 - Veykril:reformat-no-rustup, r=Veykril
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Aug 30, 2024
2 parents 1cb4266 + 3ad54a7 commit 64c538f
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions xtask/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,31 +126,35 @@ impl fmt::Display for Location {
}
}

fn rustfmt_executable(sh: &Shell) -> &str {
// First try explicitly requesting the stable channel via rustup in case nightly is being used by default,
// then plain rustfmt in case rustup isn't being used to manage the compiler (e.g. when using Nix).
for executable in ["rustup run stable rustfmt", "rustfmt"] {
let version = cmd!(sh, "{executable} --version").read().unwrap_or_default();
if version.contains("stable") {
return executable;
}
}

panic!(
"Failed to run rustfmt from toolchain 'stable'. \
Please run `rustup component add rustfmt --toolchain stable` to install it.",
);
}

fn reformat(text: String) -> String {
let sh = Shell::new().unwrap();
let rustfmt_exe = rustfmt_executable(&sh);
let rustfmt_toml = project_root().join("rustfmt.toml");
let mut stdout =
cmd!(sh, "{rustfmt_exe} --config-path {rustfmt_toml} --config fn_single_line=true")
.stdin(text)
.read()
.unwrap();
let version = cmd!(sh, "rustup run stable rustfmt --version").read().unwrap_or_default();

// First try explicitly requesting the stable channel via rustup in case nightly is being used by default,
// then plain rustfmt in case rustup isn't being used to manage the compiler (e.g. when using Nix).
let mut stdout = if !version.contains("stable") {
let version = cmd!(sh, "rustfmt --version").read().unwrap_or_default();
if !version.contains("stable") {
panic!(
"Failed to run rustfmt from toolchain 'stable'. \
Please run `rustup component add rustfmt --toolchain stable` to install it.",
);
} else {
cmd!(sh, "rustfmt --config-path {rustfmt_toml} --config fn_single_line=true")
.stdin(text)
.read()
.unwrap()
}
} else {
cmd!(
sh,
"rustup run stable rustfmt --config-path {rustfmt_toml} --config fn_single_line=true"
)
.stdin(text)
.read()
.unwrap()
};
if !stdout.ends_with('\n') {
stdout.push('\n');
}
Expand Down

0 comments on commit 64c538f

Please sign in to comment.