Skip to content

Commit b1defdb

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

File tree

2 files changed

+16
-48
lines changed

2 files changed

+16
-48
lines changed

src/bin/cargo/cli.rs

Lines changed: 6 additions & 44 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::{self, drop_print, drop_println, CargoResult, CliResult, Config};
54
use clap::{builder::UnknownArgumentValueParser, Arg, ArgMatches};
@@ -15,7 +14,11 @@ use crate::command_prelude::*;
1514
use crate::util::is_rustup;
1615
use cargo::util::style;
1716

18-
pub fn main(config: &mut LazyConfig) -> CliResult {
17+
pub fn main(config: &mut Config) -> 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(config: &mut LazyConfig) -> CliResult {
3841
.into());
3942
}
4043
std::env::set_current_dir(&new_cwd).context("could not change to requested directory")?;
41-
config.get_mut().reload_cwd();
44+
config.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 config = config.get_mut();
48-
4947
let (expanded_args, global_args) = expand_aliases(config, args, vec![])?;
5048

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

648-
/// Delay loading [`Config`] until access.
649-
///
650-
/// In the common path, the [`Config`] 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 LazyConfig {
655-
config: Option<Config>,
656-
}
657-
658-
impl LazyConfig {
659-
pub fn new() -> Self {
660-
Self { config: 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) -> &Config {
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 Config {
674-
self.config.get_or_insert_with(|| match Config::default() {
675-
Ok(cfg) => cfg,
676-
Err(e) => {
677-
let mut shell = Shell::new();
678-
cargo::exit_with_error(e.into(), &mut shell)
679-
}
680-
})
681-
}
682-
}
683-
684646
#[test]
685647
fn verify_cli() {
686648
cli().debug_assert();

src/bin/cargo/main.rs

Lines changed: 10 additions & 4 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::CliError;
@@ -20,18 +21,23 @@ use crate::command_prelude::*;
2021
fn main() {
2122
setup_logger();
2223

23-
let mut config = cli::LazyConfig::new();
24-
config.get();
24+
let mut config = match Config::default() {
25+
Ok(cfg) => cfg,
26+
Err(e) => {
27+
let mut shell = Shell::new();
28+
cargo::exit_with_error(e.into(), &mut shell)
29+
}
30+
};
2531

2632
let result = if let Some(lock_addr) = cargo::ops::fix_get_proxy_lock_addr() {
27-
cargo::ops::fix_exec_rustc(config.get(), &lock_addr).map_err(|e| CliError::from(e))
33+
cargo::ops::fix_exec_rustc(&config, &lock_addr).map_err(|e| CliError::from(e))
2834
} else {
2935
let _token = cargo::util::job::setup();
3036
cli::main(&mut config)
3137
};
3238

3339
match result {
34-
Err(e) => cargo::exit_with_error(e, &mut config.get_mut().shell()),
40+
Err(e) => cargo::exit_with_error(e, &mut *config.shell()),
3541
Ok(()) => {}
3642
}
3743
}

0 commit comments

Comments
 (0)