Auto merge of #140732 - onur-ozkan:use-in-tree-rustfmt, r=Kobzol

make it possible to run in-tree rustfmt with `x run rustfmt`

Currently, there is no way to run in-tree `rustfmt` using `x fmt` or `x test tidy` commands. This PR implements `rustfmt` on `x run`, which allows bootstrap to run the in-tree `rustfmt`.

Fixes #140723
This commit is contained in:
bors 2025-05-08 11:53:39 +00:00
commit e964ccafed
4 changed files with 66 additions and 1 deletions

View file

@ -9,7 +9,7 @@ use std::sync::mpsc::SyncSender;
use build_helper::git::get_git_modified_files;
use ignore::WalkBuilder;
use crate::core::builder::Builder;
use crate::core::builder::{Builder, Kind};
use crate::utils::build_stamp::BuildStamp;
use crate::utils::exec::command;
use crate::utils::helpers::{self, t};
@ -122,6 +122,12 @@ fn print_paths(verb: &str, adjective: Option<&str>, paths: &[String]) {
}
pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
if build.kind == Kind::Format && build.top_stage != 0 {
eprintln!("ERROR: `x fmt` only supports stage 0.");
eprintln!("HELP: Use `x run rustfmt` to run in-tree rustfmt.");
crate::exit!(1);
}
if !paths.is_empty() {
eprintln!(
"fmt error: path arguments are no longer accepted; use `--all` to format everything"

View file

@ -420,3 +420,56 @@ impl Step for CoverageDump {
cmd.run(builder);
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Rustfmt;
impl Step for Rustfmt {
type Output = ();
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.path("src/tools/rustfmt")
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Rustfmt);
}
fn run(self, builder: &Builder<'_>) {
let host = builder.build.build;
// `x run` uses stage 0 by default but rustfmt does not work well with stage 0.
// Change the stage to 1 if it's not set explicitly.
let stage = if builder.config.is_explicit_stage() || builder.top_stage >= 1 {
builder.top_stage
} else {
1
};
if stage == 0 {
eprintln!("rustfmt cannot be run at stage 0");
eprintln!("HELP: Use `x fmt` to use stage 0 rustfmt.");
std::process::exit(1);
}
let compiler = builder.compiler(stage, host);
let rustfmt_build = builder.ensure(tool::Rustfmt { compiler, target: host });
let mut rustfmt = tool::prepare_tool_cargo(
builder,
rustfmt_build.build_compiler,
Mode::ToolRustc,
host,
Kind::Run,
"src/tools/rustfmt",
SourceType::InTree,
&[],
);
rustfmt.args(["--bin", "rustfmt", "--"]);
rustfmt.args(builder.config.args());
rustfmt.into_cmd().run(builder);
}
}

View file

@ -1116,6 +1116,7 @@ impl<'a> Builder<'a> {
run::FeaturesStatusDump,
run::CyclicStep,
run::CoverageDump,
run::Rustfmt,
),
Kind::Setup => {
describe!(setup::Profile, setup::Hook, setup::Link, setup::Editor)

View file

@ -406,4 +406,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
severity: ChangeSeverity::Info,
summary: "Added a new option `rust.debug-assertions-tools` to control debug asssertions for tools.",
},
ChangeInfo {
change_id: 140732,
severity: ChangeSeverity::Info,
summary: "`./x run` now supports running in-tree `rustfmt`, e.g., `./x run rustfmt -- --check /path/to/file.rs`.",
},
];