diff --git a/build/configure/src/rust.rs b/build/configure/src/rust.rs index e137b00c5..92f79cc64 100644 --- a/build/configure/src/rust.rs +++ b/build/configure/src/rust.rs @@ -113,6 +113,7 @@ pub fn check_rust(build: &mut Build) -> Result<()> { CargoFormat { inputs: inputs.clone(), check_only: true, + working_dir: Some("cargo/format"), }, )?; build.add( @@ -120,6 +121,7 @@ pub fn check_rust(build: &mut Build) -> Result<()> { CargoFormat { inputs: inputs.clone(), check_only: false, + working_dir: Some("cargo/format"), }, )?; diff --git a/build/ninja_gen/src/build.rs b/build/ninja_gen/src/build.rs index 60c69dadb..dc88c1e6f 100644 --- a/build/ninja_gen/src/build.rs +++ b/build/ninja_gen/src/build.rs @@ -214,6 +214,7 @@ struct BuildStatement<'a> { rule_variables: Vec<(String, String)>, output_stamp: bool, env_vars: Vec, + working_dir: Option, release: bool, bypass_runner: bool, } @@ -237,6 +238,7 @@ impl BuildStatement<'_> { output_subsets: Default::default(), output_stamp: false, env_vars: Default::default(), + working_dir: None, release, bypass_runner: action.bypass_runner(), }; @@ -296,6 +298,9 @@ impl BuildStatement<'_> { write!(&mut buf, "--env={var} ").unwrap(); } } + if let Some(working_dir) = &self.working_dir { + write!(&mut buf, "--cwd={working_dir} ").unwrap(); + } buf.push_str(&command); buf } @@ -360,6 +365,11 @@ pub trait FilesHandle { /// for each command, `constant_value` should reference a `$variable` you /// have defined. fn add_env_var(&mut self, key: &str, constant_value: &str); + /// Set the current working dir for the provided command(s). + /// Note this is defined once for the rule, so if the value should change + /// for each command, `constant_value` should reference a `$variable` you + /// have defined. + fn set_working_dir(&mut self, constant_value: &str); fn release_build(&self) -> bool; } @@ -448,6 +458,10 @@ impl FilesHandle for BuildStatement<'_> { fn add_env_var(&mut self, key: &str, constant_value: &str) { self.env_vars.push(format!("{key}={constant_value}")); } + + fn set_working_dir(&mut self, constant_value: &str) { + self.working_dir = Some(constant_value.to_owned()); + } } fn to_ninja_target_string(explicit: &[String], implicit: &[String]) -> String { diff --git a/build/ninja_gen/src/cargo.rs b/build/ninja_gen/src/cargo.rs index 111923e13..c5620292e 100644 --- a/build/ninja_gen/src/cargo.rs +++ b/build/ninja_gen/src/cargo.rs @@ -173,17 +173,21 @@ impl BuildAction for CargoClippy { pub struct CargoFormat { pub inputs: BuildInput, pub check_only: bool, + pub working_dir: Option<&'static str>, } impl BuildAction for CargoFormat { fn command(&self) -> &str { - // the empty config file prevents warnings about nightly features - "cargo fmt $mode -- --config-path=.rustfmt-empty.toml --color always" + "cargo fmt $mode --all" } fn files(&mut self, build: &mut impl FilesHandle) { build.add_inputs("", &self.inputs); build.add_variable("mode", if self.check_only { "--check" } else { "" }); + if let Some(working_dir) = self.working_dir { + build.set_working_dir("$working_dir"); + build.add_variable("working_dir", working_dir); + } build.add_output_stamp(format!( "tests/cargo_format.{}", if self.check_only { "check" } else { "fmt" } diff --git a/build/runner/src/build.rs b/build/runner/src/build.rs index 036d03b56..9a226fa49 100644 --- a/build/runner/src/build.rs +++ b/build/runner/src/build.rs @@ -67,7 +67,7 @@ pub fn run_build(args: BuildArgs) { // commands will not show colors by default, as we do not provide a tty .env("FORCE_COLOR", "1") .env("MYPY_FORCE_COLOR", "1") - .env("TERM", "1") + .env("TERM", std::env::var("TERM").unwrap_or_default()) // Prevents 'Warn: You must provide the URL of lib/mappings.wasm'. // Updating svelte-check or its deps will likely remove the need for it. .env("NODE_OPTIONS", "--no-experimental-fetch"); diff --git a/build/runner/src/run.rs b/build/runner/src/run.rs index a2e67391e..e9fcc3f31 100644 --- a/build/runner/src/run.rs +++ b/build/runner/src/run.rs @@ -13,6 +13,8 @@ pub struct RunArgs { stamp: Option, #[arg(long, value_parser = split_env)] env: Vec<(String, String)>, + #[arg(long)] + cwd: Option, #[arg(trailing_var_arg = true)] args: Vec, } @@ -22,7 +24,7 @@ pub struct RunArgs { pub fn run_commands(args: RunArgs) { let commands = split_args(args.args); for command in commands { - run_silent(&mut build_command(command, &args.env)); + run_silent(&mut build_command(command, &args.env, &args.cwd)); } if let Some(stamp_file) = args.stamp { std::fs::write(stamp_file, b"").expect("unable to write stamp file"); @@ -37,12 +39,19 @@ fn split_env(s: &str) -> Result<(String, String), std::io::Error> { } } -fn build_command(command_and_args: Vec, env: &[(String, String)]) -> Command { +fn build_command( + command_and_args: Vec, + env: &[(String, String)], + cwd: &Option, +) -> Command { let mut command = Command::new(&command_and_args[0]); command.args(&command_and_args[1..]); for (k, v) in env { command.env(k, v); } + if let Some(cwd) = cwd { + command.current_dir(cwd); + } command } diff --git a/cargo/README.md b/cargo/README.md index 1cc581fad..6c8d75bab 100644 --- a/cargo/README.md +++ b/cargo/README.md @@ -1,3 +1,5 @@ This folder used to contain Bazel-specific Rust integration, but now only -contains our license-checking script, which should be run when using -cargo update. +contains: + +- our license-checking script, which should be run when using cargo update. +- a nightly toolchain definition for formatting diff --git a/cargo/format/rust-toolchain.toml b/cargo/format/rust-toolchain.toml new file mode 100644 index 000000000..84816df87 --- /dev/null +++ b/cargo/format/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "nightly-2023-01-24" +profile = "minimal" +components = ["rustfmt"] diff --git a/rslib/src/sync/http_client/io_monitor.rs b/rslib/src/sync/http_client/io_monitor.rs index 2cf0d080d..6361d5c36 100644 --- a/rslib/src/sync/http_client/io_monitor.rs +++ b/rslib/src/sync/http_client/io_monitor.rs @@ -201,9 +201,9 @@ mod test { use super::*; use crate::sync::error::HttpError; - /// The delays in the tests are aggressively short, and false positives slip through - /// on a loaded system - especially on Windows. Fix by applying a universal - /// multiplier. + /// The delays in the tests are aggressively short, and false positives slip + /// through on a loaded system - especially on Windows. Fix by applying + /// a universal multiplier. fn millis(millis: u64) -> Duration { Duration::from_millis(millis * if cfg!(windows) { 10 } else { 5 }) }