Skip to content

Commit e5fd597

Browse files
committed
Revert "refactor(cli): Lazily do first-pass config loading"
This reverts commit eda1652.
1 parent 8434854 commit e5fd597

File tree

2 files changed

+17
-50
lines changed

2 files changed

+17
-50
lines changed

src/bin/cargo/cli.rs

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use anyhow::{anyhow, Context as _};
2-
use cargo::core::shell::Shell;
32
use cargo::core::{features, CliUnstable};
43
use cargo::{drop_print, drop_println, CargoResult};
54
use clap::builder::UnknownArgumentValueParser;
@@ -15,7 +14,11 @@ use crate::command_prelude::*;
1514
use crate::util::is_rustup;
1615
use cargo::util::style;
1716

18-
pub fn main(lazy_gctx: &mut LazyContext) -> CliResult {
17+
pub fn main(gctx: &mut GlobalContext) -> CliResult {
18+
// CAUTION: Be careful with using `config` until it is configured below.
19+
// In general, try to avoid loading config values unless necessary (like
20+
// the [alias] table).
21+
1922
let args = cli().try_get_matches()?;
2023

2124
// Update the process-level notion of cwd
@@ -38,14 +41,9 @@ pub fn main(lazy_gctx: &mut LazyContext) -> CliResult {
3841
.into());
3942
}
4043
std::env::set_current_dir(&new_cwd).context("could not change to requested directory")?;
41-
lazy_gctx.get_mut().reload_cwd()?;
44+
gctx.reload_cwd()?;
4245
}
4346

44-
// CAUTION: Be careful with using `config` until it is configured below.
45-
// In general, try to avoid loading config values unless necessary (like
46-
// the [alias] table).
47-
let gctx = lazy_gctx.get_mut();
48-
4947
let (expanded_args, global_args) = expand_aliases(gctx, args, vec![])?;
5048

5149
if expanded_args
@@ -645,43 +643,6 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
645643
.subcommands(commands::builtin())
646644
}
647645

648-
/// Delay loading [`GlobalContext`] until access.
649-
///
650-
/// In the common path, the [`GlobalContext`] is dependent on CLI parsing and shouldn't be loaded until
651-
/// after that is done but some other paths (like fix or earlier errors) might need access to it,
652-
/// so this provides a way to share the instance and the implementation across these different
653-
/// accesses.
654-
pub struct LazyContext {
655-
gctx: Option<GlobalContext>,
656-
}
657-
658-
impl LazyContext {
659-
pub fn new() -> Self {
660-
Self { gctx: None }
661-
}
662-
663-
/// Get the config, loading it if needed
664-
///
665-
/// On error, the process is terminated
666-
pub fn get(&mut self) -> &GlobalContext {
667-
self.get_mut()
668-
}
669-
670-
/// Get the config, loading it if needed
671-
///
672-
/// On error, the process is terminated
673-
pub fn get_mut(&mut self) -> &mut GlobalContext {
674-
self.gctx
675-
.get_or_insert_with(|| match GlobalContext::default() {
676-
Ok(cfg) => cfg,
677-
Err(e) => {
678-
let mut shell = Shell::new();
679-
cargo::exit_with_error(e.into(), &mut shell)
680-
}
681-
})
682-
}
683-
}
684-
685646
#[test]
686647
fn verify_cli() {
687648
cli().debug_assert();

src/bin/cargo/main.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(clippy::self_named_module_files)] // false positive in `commands/build.rs`
22

3+
use cargo::core::shell::Shell;
34
use cargo::util::network::http::http_handle;
45
use cargo::util::network::http::needs_custom_http_transport;
56
use cargo::util::{self, closest_msg, command_prelude, CargoResult};
@@ -19,18 +20,23 @@ use crate::command_prelude::*;
1920
fn main() {
2021
setup_logger();
2122

22-
let mut lazy_gctx = cli::LazyContext::new();
23-
lazy_gctx.get();
23+
let mut gctx = match GlobalContext::default() {
24+
Ok(gctx) => gctx,
25+
Err(e) => {
26+
let mut shell = Shell::new();
27+
cargo::exit_with_error(e.into(), &mut shell)
28+
}
29+
};
2430

2531
let result = if let Some(lock_addr) = cargo::ops::fix_get_proxy_lock_addr() {
26-
cargo::ops::fix_exec_rustc(lazy_gctx.get(), &lock_addr).map_err(|e| CliError::from(e))
32+
cargo::ops::fix_exec_rustc(&gctx, &lock_addr).map_err(|e| CliError::from(e))
2733
} else {
2834
let _token = cargo::util::job::setup();
29-
cli::main(&mut lazy_gctx)
35+
cli::main(&mut gctx)
3036
};
3137

3238
match result {
33-
Err(e) => cargo::exit_with_error(e, &mut lazy_gctx.get_mut().shell()),
39+
Err(e) => cargo::exit_with_error(e, &mut *gctx.shell()),
3440
Ok(()) => {}
3541
}
3642
}

0 commit comments

Comments
 (0)