From 38c1d6e3373ea07365926b9201d310181d601c6f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 30 Aug 2024 16:15:01 +0200 Subject: [PATCH 1/2] Revert "fix: do not assume rustup is installed in xtask codegen" This reverts commit 7d9e4fcc07e5de94e37b73436147cdbbaa35dbdc. --- xtask/src/codegen.rs | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 09bfed19b0e9..aeb0c00ae6aa 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -126,31 +126,27 @@ 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'. \ +fn ensure_rustfmt(sh: &Shell) { + let version = cmd!(sh, "rustup run stable 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.", - ); + ); + } } fn reformat(text: String) -> String { let sh = Shell::new().unwrap(); - let rustfmt_exe = rustfmt_executable(&sh); + ensure_rustfmt(&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 mut stdout = 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'); } From 3ad54a74abaa654503812ae779feed8869df130c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 30 Aug 2024 16:17:45 +0200 Subject: [PATCH 2/2] Allow xtask::reformat to work without rustup --- xtask/src/codegen.rs | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index aeb0c00ae6aa..4c7b07c5e02c 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -126,27 +126,35 @@ impl fmt::Display for Location { } } -fn ensure_rustfmt(sh: &Shell) { - let version = cmd!(sh, "rustup run stable 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.", - ); - } -} - fn reformat(text: String) -> String { let sh = Shell::new().unwrap(); - ensure_rustfmt(&sh); let rustfmt_toml = project_root().join("rustfmt.toml"); - let mut stdout = cmd!( - sh, - "rustup run stable rustfmt --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'); }