Skip to content

Commit 990eef2

Browse files
committed
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.
1 parent 8ccf90f commit 990eef2

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/bin/cargo/main.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@ fn main() {
3030
}
3131
};
3232

33-
let result = match cargo::ops::fix_maybe_exec_rustc(&config) {
34-
Ok(true) => Ok(()),
35-
Ok(false) => {
36-
let _token = cargo::util::job::setup();
37-
cli::main(&mut config)
38-
}
39-
Err(e) => Err(CliError::from(e)),
33+
let result = if let Some(lock_addr) = cargo::ops::fix_get_proxy_lock_addr() {
34+
cargo::ops::fix_exec_rustc(&config, &lock_addr).map_err(|e| CliError::from(e))
35+
} else {
36+
let _token = cargo::util::job::setup();
37+
cli::main(&mut config)
4038
};
4139

4240
match result {

src/cargo/ops/fix.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,22 @@ to prevent this issue from happening.
333333
Ok(())
334334
}
335335

336-
/// Entry point for `cargo` running as a proxy for `rustc`.
337-
///
338-
/// This is called every time `cargo` is run to check if it is in proxy mode.
336+
/// Provide the lock address when running in proxy mode
339337
///
340-
/// Returns `false` if `fix` is not being run (not in proxy mode). Returns
341-
/// `true` if in `fix` proxy mode, and the fix was complete without any
338+
/// Returns `None` if `fix` is not being run (not in proxy mode). Returns
339+
/// `Some(...)` if in `fix` proxy mode, and the fix was complete without any
342340
/// warnings or errors. If there are warnings or errors, this does not return,
343341
/// and the process exits with the corresponding `rustc` exit code.
344-
pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
345-
let lock_addr = match env::var(FIX_ENV) {
346-
Ok(s) => s,
347-
Err(_) => return Ok(false),
348-
};
342+
pub fn fix_get_proxy_lock_addr() -> Option<String> {
343+
env::var(FIX_ENV).ok()
344+
}
349345

346+
/// Entry point for `cargo` running as a proxy for `rustc`.
347+
///
348+
/// This is called every time `cargo` is run to check if it is in proxy mode.
349+
///
350+
/// See [`fix_proxy_lock_addr`]
351+
pub fn fix_exec_rustc(config: &Config, lock_addr: &str) -> CargoResult<()> {
350352
let args = FixArgs::get()?;
351353
trace!("cargo-fix as rustc got file {:?}", args.file);
352354

@@ -392,7 +394,7 @@ pub fn fix_maybe_exec_rustc(config: &Config) -> CargoResult<bool> {
392394
// any. If stderr is empty then there's no need for the final exec at
393395
// the end, we just bail out here.
394396
if output.status.success() && output.stderr.is_empty() {
395-
return Ok(true);
397+
return Ok(());
396398
}
397399

398400
// Otherwise, if our rustc just failed, then that means that we broke the

src/cargo/ops/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use self::cargo_read_manifest::{read_package, read_packages};
1919
pub use self::cargo_run::run;
2020
pub use self::cargo_test::{run_benches, run_tests, TestOptions};
2121
pub use self::cargo_uninstall::uninstall;
22-
pub use self::fix::{fix, fix_maybe_exec_rustc, FixOptions};
22+
pub use self::fix::{fix, fix_exec_rustc, fix_get_proxy_lock_addr, FixOptions};
2323
pub use self::lockfile::{load_pkg_lockfile, resolve_to_string, write_pkg_lockfile};
2424
pub use self::registry::HttpTimeout;
2525
pub use self::registry::{configure_http_handle, http_handle, http_handle_and_timeout};

0 commit comments

Comments
 (0)