Skip to content

Commit e964cca

Browse files
committed
Auto merge of rust-lang#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 rust-lang#140723
2 parents 1973872 + e85d014 commit e964cca

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/bootstrap/src/core/build_steps/format.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::mpsc::SyncSender;
99
use build_helper::git::get_git_modified_files;
1010
use ignore::WalkBuilder;
1111

12-
use crate::core::builder::Builder;
12+
use crate::core::builder::{Builder, Kind};
1313
use crate::utils::build_stamp::BuildStamp;
1414
use crate::utils::exec::command;
1515
use crate::utils::helpers::{self, t};
@@ -122,6 +122,12 @@ fn print_paths(verb: &str, adjective: Option<&str>, paths: &[String]) {
122122
}
123123

124124
pub 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
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,56 @@ impl Step for CoverageDump {
420420
cmd.run(builder);
421421
}
422422
}
423+
424+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
425+
pub struct Rustfmt;
426+
427+
impl 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
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -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
];

0 commit comments

Comments
 (0)