Skip to content

Commit bfc18fe

Browse files
committed
refactor(cli): Switch '-C' to using 'reload_rooted_at'
1 parent e7ff7a6 commit bfc18fe

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-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(config: &mut LazyConfig) -> 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!(config.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(config: &mut LazyConfig) -> CliResult {
4038
.into());
4139
}
4240
std::env::set_current_dir(&new_cwd).context("could not change to requested directory")?;
41+
config.get_mut().reload_cwd();
4342
}
4443

4544
// CAUTION: Be careful with using `config` until it is configured below.
@@ -661,13 +660,6 @@ impl LazyConfig {
661660
Self { config: 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.config.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
@@ -21,6 +21,7 @@ fn main() {
2121
setup_logger();
2222

2323
let mut config = cli::LazyConfig::new();
24+
config.get();
2425

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

src/cargo/util/config/mod.rs

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

568+
/// Run after calling [`std::env::set_current_dir`]
569+
pub fn reload_cwd(&mut self) -> CargoResult<()> {
570+
let cwd = env::current_dir()
571+
.with_context(|| "couldn't get the current directory of the process")?;
572+
self.cwd = cwd;
573+
self.reload_rooted_at(self.cwd.clone())?;
574+
Ok(())
575+
}
576+
568577
/// Reloads on-disk configuration values, starting at the given path and
569578
/// walking up its ancestors.
570579
pub fn reload_rooted_at<P: AsRef<Path>>(&mut self, path: P) -> CargoResult<()> {

0 commit comments

Comments
 (0)