Migrate to a new PWD API (part 2) (#12749)

Refer to #12603 for part 1.

We need to be careful when migrating to the new API, because the new API
has slightly different semantics (PWD can contain symlinks). This PR
handles the "obviously safe" part of the migrations. Namely, it handles
two specific use cases:

* Passing PWD into `canonicalize_with()`
* Passing PWD into `EngineState::merge_env()`

The first case is safe because symlinks are canonicalized away. The
second case is safe because `EngineState::merge_env()` only uses PWD to
call `std::env::set_current_dir()`, which shouldn't affact Nushell. The
commit message contains detailed stats on the updated files.

Because these migrations touch a lot of files, I want to keep these PRs
small to avoid merge conflicts.
This commit is contained in:
YizhePKU
2024-05-07 23:17:49 +08:00
committed by GitHub
parent b9331d1b08
commit 7a86b98f61
12 changed files with 76 additions and 71 deletions

View File

@ -31,14 +31,19 @@ pub(crate) fn read_config_file(
// Load config startup file
if let Some(file) = config_file {
let working_set = StateWorkingSet::new(engine_state);
#[allow(deprecated)]
let cwd = working_set.get_cwd();
if let Ok(path) = canonicalize_with(&file.item, cwd) {
eval_config_contents(path, engine_state, stack);
} else {
let e = ParseError::FileNotFound(file.item, file.span);
report_error(&working_set, &e);
match engine_state.cwd_as_string(Some(stack)) {
Ok(cwd) => {
if let Ok(path) = canonicalize_with(&file.item, cwd) {
eval_config_contents(path, engine_state, stack);
} else {
let e = ParseError::FileNotFound(file.item, file.span);
report_error(&working_set, &e);
}
}
Err(e) => {
report_error(&working_set, &e);
}
}
} else if let Some(mut config_path) = nu_path::config_dir() {
config_path.push(NUSHELL_FOLDER);
@ -144,8 +149,7 @@ pub(crate) fn read_default_env_file(engine_state: &mut EngineState, stack: &mut
info!("read_config_file {}:{}:{}", file!(), line!(), column!());
// Merge the environment in case env vars changed in the config
#[allow(deprecated)]
match nu_engine::env::current_dir(engine_state, stack) {
match engine_state.cwd(Some(stack)) {
Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) {
let working_set = StateWorkingSet::new(engine_state);
@ -186,8 +190,7 @@ fn eval_default_config(
);
// Merge the environment in case env vars changed in the config
#[allow(deprecated)]
match nu_engine::env::current_dir(engine_state, stack) {
match engine_state.cwd(Some(stack)) {
Ok(cwd) => {
if let Err(e) = engine_state.merge_env(stack, cwd) {
let working_set = StateWorkingSet::new(engine_state);