Skip to content

Commit

Permalink
refactor(cli): Delay fix's access to config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
epage committed Sep 1, 2022
1 parent d674c22 commit ddc21ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
12 changes: 5 additions & 7 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 13 additions & 10 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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<bool> {
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);

Expand Down Expand Up @@ -392,7 +395,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
// 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
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit ddc21ea

Please sign in to comment.