| author | bors <bors@rust-lang.org> 2025-05-08 11:53:39 UTC |
| committer | bors <bors@rust-lang.org> 2025-05-08 11:53:39 UTC |
| log | e964ccafedcf7a505f90f31370d568e649286176 |
| tree | 5ca00a5d4473fd474c335614bc993e940f6aaf60 |
| parent | 19738720130a82959acd4fc45259166262f3ffbe |
| parent | e85d014244da0c208bfbf6bfc60b89b1ecc831b0 |
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 #1407234 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; |
| 9 | 9 | use build_helper::git::get_git_modified_files; |
| 10 | 10 | use ignore::WalkBuilder; |
| 11 | 11 | |
| 12 | use crate::core::builder::Builder; | |
| 12 | use crate::core::builder::{Builder, Kind}; | |
| 13 | 13 | use crate::utils::build_stamp::BuildStamp; |
| 14 | 14 | use crate::utils::exec::command; |
| 15 | 15 | use crate::utils::helpers::{self, t}; |
| ... | ... | @@ -122,6 +122,12 @@ fn print_paths(verb: &str, adjective: Option<&str>, paths: &[String]) { |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | 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 | ||
| 125 | 131 | if !paths.is_empty() { |
| 126 | 132 | eprintln!( |
| 127 | 133 | "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 { |
| 420 | 420 | cmd.run(builder); |
| 421 | 421 | } |
| 422 | 422 | } |
| 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| ... | ... | @@ -1116,6 +1116,7 @@ impl<'a> Builder<'a> { |
| 1116 | 1116 | run::FeaturesStatusDump, |
| 1117 | 1117 | run::CyclicStep, |
| 1118 | 1118 | run::CoverageDump, |
| 1119 | run::Rustfmt, | |
| 1119 | 1120 | ), |
| 1120 | 1121 | Kind::Setup => { |
| 1121 | 1122 | 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] = &[ |
| 406 | 406 | severity: ChangeSeverity::Info, |
| 407 | 407 | summary: "Added a new option `rust.debug-assertions-tools` to control debug asssertions for tools.", |
| 408 | 408 | }, |
| 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 | }, | |
| 409 | 414 | ]; |