authorbors <bors@rust-lang.org> 2025-05-08 11:53:39 UTC
committerbors <bors@rust-lang.org> 2025-05-08 11:53:39 UTC
loge964ccafedcf7a505f90f31370d568e649286176
tree5ca00a5d4473fd474c335614bc993e940f6aaf60
parent19738720130a82959acd4fc45259166262f3ffbe
parente85d014244da0c208bfbf6bfc60b89b1ecc831b0

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

4 files changed, 66 insertions(+), 1 deletions(-)

src/bootstrap/src/core/build_steps/format.rs+7-1
......@@ -9,7 +9,7 @@ use std::sync::mpsc::SyncSender;
99use build_helper::git::get_git_modified_files;
1010use ignore::WalkBuilder;
1111
12use crate::core::builder::Builder;
12use crate::core::builder::{Builder, Kind};
1313use crate::utils::build_stamp::BuildStamp;
1414use crate::utils::exec::command;
1515use crate::utils::helpers::{self, t};
......@@ -122,6 +122,12 @@ fn print_paths(verb: &str, adjective: Option<&str>, paths: &[String]) {
122122}
123123
124124pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
125 if build.kind == Kind::Format && build.top_stage != 0 {
126 eprintln!("ERROR: `x fmt` only supports stage 0.");
127 eprintln!("HELP: Use `x run rustfmt` to run in-tree rustfmt.");
128 crate::exit!(1);
129 }
130
125131 if !paths.is_empty() {
126132 eprintln!(
127133 "fmt error: path arguments are no longer accepted; use `--all` to format everything"
src/bootstrap/src/core/build_steps/run.rs+53
......@@ -420,3 +420,56 @@ impl Step for CoverageDump {
420420 cmd.run(builder);
421421 }
422422}
423
424#[derive(Debug, Clone, PartialEq, Eq, Hash)]
425pub struct Rustfmt;
426
427impl Step for Rustfmt {
428 type Output = ();
429 const ONLY_HOSTS: bool = true;
430
431 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
432 run.path("src/tools/rustfmt")
433 }
434
435 fn make_run(run: RunConfig<'_>) {
436 run.builder.ensure(Rustfmt);
437 }
438
439 fn run(self, builder: &Builder<'_>) {
440 let host = builder.build.build;
441
442 // `x run` uses stage 0 by default but rustfmt does not work well with stage 0.
443 // Change the stage to 1 if it's not set explicitly.
444 let stage = if builder.config.is_explicit_stage() || builder.top_stage >= 1 {
445 builder.top_stage
446 } else {
447 1
448 };
449
450 if stage == 0 {
451 eprintln!("rustfmt cannot be run at stage 0");
452 eprintln!("HELP: Use `x fmt` to use stage 0 rustfmt.");
453 std::process::exit(1);
454 }
455
456 let compiler = builder.compiler(stage, host);
457 let rustfmt_build = builder.ensure(tool::Rustfmt { compiler, target: host });
458
459 let mut rustfmt = tool::prepare_tool_cargo(
460 builder,
461 rustfmt_build.build_compiler,
462 Mode::ToolRustc,
463 host,
464 Kind::Run,
465 "src/tools/rustfmt",
466 SourceType::InTree,
467 &[],
468 );
469
470 rustfmt.args(["--bin", "rustfmt", "--"]);
471 rustfmt.args(builder.config.args());
472
473 rustfmt.into_cmd().run(builder);
474 }
475}
src/bootstrap/src/core/builder/mod.rs+1
......@@ -1116,6 +1116,7 @@ impl<'a> Builder<'a> {
11161116 run::FeaturesStatusDump,
11171117 run::CyclicStep,
11181118 run::CoverageDump,
1119 run::Rustfmt,
11191120 ),
11201121 Kind::Setup => {
11211122 describe!(setup::Profile, setup::Hook, setup::Link, setup::Editor)
src/bootstrap/src/utils/change_tracker.rs+5
......@@ -406,4 +406,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[
406406 severity: ChangeSeverity::Info,
407407 summary: "Added a new option `rust.debug-assertions-tools` to control debug asssertions for tools.",
408408 },
409 ChangeInfo {
410 change_id: 140732,
411 severity: ChangeSeverity::Info,
412 summary: "`./x run` now supports running in-tree `rustfmt`, e.g., `./x run rustfmt -- --check /path/to/file.rs`.",
413 },
409414];