From ddc21ea4cdf01bf784f063f0815c4aaf0367f0be Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 29 Aug 2022 11:46:54 -0500 Subject: [PATCH] refactor(cli): Delay fix's access to config My hope is to make it so we can lazy load the config. This makes it so we only load the config for the fix proxy if needed. I also feel like this better clarifies the intention of the code that we are running in a special mode. --- src/bin/cargo/main.rs | 12 +++++------- src/cargo/ops/fix.rs | 23 +++++++++++++---------- src/cargo/ops/mod.rs | 2 +- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/bin/cargo/main.rs b/src/bin/cargo/main.rs index 1619b487b..bd439f8f1 100644 --- a/src/bin/cargo/main.rs +++ b/src/bin/cargo/main.rs @@ -30,13 +30,11 @@ fn main() { } }; - let result = match cargo::ops::fix_maybe_exec_rustc(&config) { - Ok(true) => Ok(()), - Ok(false) => { - let _token = cargo::util::job::setup(); - cli::main(&mut config) - } - Err(e) => Err(CliError::from(e)), + let result = if let Some(lock_addr) = cargo::ops::fix_get_proxy_lock_addr() { + cargo::ops::fix_exec_rustc(&config, &lock_addr).map_err(|e| CliError::from(e)) + } else { + let _token = cargo::util::job::setup(); + cli::main(&mut config) }; match result { diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 0b40a2942..5fb35b890 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -333,20 +333,23 @@ to prevent this issue from happening. Ok(()) } +/// Provide the lock address when running in proxy mode +/// +/// Returns `None` if `fix` is not being run (not in proxy mode). Returns +/// `Some(...)` if in `fix` proxy mode +pub fn fix_get_proxy_lock_addr() -> Option { + env::var(FIX_ENV).ok() +} + /// Entry point for `cargo` running as a proxy for `rustc`. /// /// This is called every time `cargo` is run to check if it is in proxy mode. /// -/// Returns `false` if `fix` is not being run (not in proxy mode). Returns -/// `true` if in `fix` proxy mode, and the fix was complete without any -/// warnings or errors. If there are warnings or errors, this does not return, +/// If there are warnings or errors, this does not return, /// and the process exits with the corresponding `rustc` exit code. -pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult { - let lock_addr = match env::var(FIX_ENV) { - Ok(s) => s, - Err(_) => return Ok(false), - }; - +/// +/// See [`fix_proxy_lock_addr`] +pub fn fix_exec_rustc(config: &Config, lock_addr: &str) -> CargoResult<()> { let args = FixArgs::get()?; trace!("cargo-fix as rustc got file {:?}", args.file); @@ -392,7 +395,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult { // any. If stderr is empty then there's no need for the final exec at // the end, we just bail out here. if output.status.success() && output.stderr.is_empty() { - return Ok(true); + return Ok(()); } // Otherwise, if our rustc just failed, then that means that we broke the diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index a5715c13e..d9895ea08 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -19,7 +19,7 @@ pub use self::cargo_read_manifest::{read_package, read_packages}; pub use self::cargo_run::run; pub use self::cargo_test::{run_benches, run_tests, TestOptions}; pub use self::cargo_uninstall::uninstall; -pub use self::fix::{fix, fix_maybe_exec_rustc, FixOptions}; +pub use self::fix::{fix, fix_exec_rustc, fix_get_proxy_lock_addr, FixOptions}; pub use self::lockfile::{load_pkg_lockfile, resolve_to_string, write_pkg_lockfile}; pub use self::registry::HttpTimeout; pub use self::registry::{configure_http_handle, http_handle, http_handle_and_timeout}; -- 2.39.5