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

@ -984,6 +984,20 @@ impl EngineState {
}
}
/// Like `EngineState::cwd()`, but returns a String instead of a PathBuf for convenience.
pub fn cwd_as_string(&self, stack: Option<&Stack>) -> Result<String, ShellError> {
let cwd = self.cwd(stack)?;
cwd.into_os_string()
.into_string()
.map_err(|err| ShellError::NonUtf8Custom {
msg: format!(
"The current working directory is not a valid utf-8 string: {:?}",
err
),
span: Span::unknown(),
})
}
// TODO: see if we can completely get rid of this
pub fn get_file_contents(&self) -> &[CachedFile] {
&self.files