Skip to content

Commit 8434854

Browse files
committed
refactor(cli): Switch '-C' to using 'reload_rooted_at'
1 parent 3fcbf73 commit 8434854

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/bin/cargo/cli.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ pub fn main(lazy_gctx: &mut LazyContext) -> CliResult {
1919
let args = cli().try_get_matches()?;
2020

2121
// Update the process-level notion of cwd
22-
// This must be completed before config is initialized
23-
assert_eq!(lazy_gctx.is_init(), false);
2422
if let Some(new_cwd) = args.get_one::<std::path::PathBuf>("directory") {
2523
// This is a temporary hack. This cannot access `Config`, so this is a bit messy.
2624
// This does not properly parse `-Z` flags that appear after the subcommand.
@@ -40,6 +38,7 @@ pub fn main(lazy_gctx: &mut LazyContext) -> CliResult {
4038
.into());
4139
}
4240
std::env::set_current_dir(&new_cwd).context("could not change to requested directory")?;
41+
lazy_gctx.get_mut().reload_cwd()?;
4342
}
4443

4544
// CAUTION: Be careful with using `config` until it is configured below.
@@ -661,13 +660,6 @@ impl LazyContext {
661660
Self { gctx: None }
662661
}
663662

664-
/// Check whether the config is loaded
665-
///
666-
/// This is useful for asserts in case the environment needs to be setup before loading
667-
pub fn is_init(&self) -> bool {
668-
self.gctx.is_some()
669-
}
670-
671663
/// Get the config, loading it if needed
672664
///
673665
/// On error, the process is terminated

src/bin/cargo/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn main() {
2020
setup_logger();
2121

2222
let mut lazy_gctx = cli::LazyContext::new();
23+
lazy_gctx.get();
2324

2425
let result = if let Some(lock_addr) = cargo::ops::fix_get_proxy_lock_addr() {
2526
cargo::ops::fix_exec_rustc(lazy_gctx.get(), &lock_addr).map_err(|e| CliError::from(e))

src/cargo/util/config/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,25 @@ impl GlobalContext {
565565
self.search_stop_path = Some(path);
566566
}
567567

568+
/// Switches the working directory to [`std::env::current_dir`]
569+
///
570+
/// There is not a need to also call [`Self::reload_rooted_at`].
571+
pub fn reload_cwd(&mut self) -> CargoResult<()> {
572+
let cwd = env::current_dir()
573+
.with_context(|| "couldn't get the current directory of the process")?;
574+
let homedir = homedir(&cwd).ok_or_else(|| {
575+
anyhow!(
576+
"Cargo couldn't find your home directory. \
577+
This probably means that $HOME was not set."
578+
)
579+
})?;
580+
581+
self.cwd = cwd;
582+
self.home_path = Filesystem::new(homedir);
583+
self.reload_rooted_at(self.cwd.clone())?;
584+
Ok(())
585+
}
586+
568587
/// Reloads on-disk configuration values, starting at the given path and
569588
/// walking up its ancestors.
570589
pub fn reload_rooted_at<P: AsRef<Path>>(&mut self, path: P) -> CargoResult<()> {

0 commit comments

Comments
 (0)