Implement PWD recovery (#12779)

This PR has two parts. The first part is the addition of the
`Stack::set_pwd()` API. It strips trailing slashes from paths for
convenience, but will reject otherwise bad paths, leaving PWD in a good
state. This should reduce the impact of faulty code incorrectly trying
to set PWD.
(https://github.com/nushell/nushell/pull/12760#issuecomment-2095393012)

The second part is implementing a PWD recovery mechanism. PWD can become
bad even when we did nothing wrong. For example, Unix allows you to
remove any directory when another process might still be using it, which
means PWD can just "disappear" under our nose. This PR makes it possible
to use `cd` to reset PWD into a good state. Here's a demonstration:

```sh
mkdir /tmp/foo
cd /tmp/foo

# delete "/tmp/foo" in a subshell, because Nushell is smart and refuse to delete PWD
nu -c 'cd /; rm -r /tmp/foo'

ls          # Error:   × $env.PWD points to a non-existent directory
            # help: Use `cd` to reset $env.PWD into a good state

cd /
pwd         # prints /
```

Also, auto-cd should be working again.
This commit is contained in:
YizhePKU
2024-05-11 00:06:33 +08:00
committed by GitHub
parent 70c01bbb26
commit b9a7faad5a
9 changed files with 214 additions and 33 deletions

View File

@ -1,6 +1,6 @@
use nu_protocol::{
engine::{EngineState, Stack},
report_error_new, Range, ShellError, Span, Value,
Range, ShellError, Span, Value,
};
use std::{ops::Bound, path::PathBuf};
@ -13,10 +13,9 @@ pub fn get_init_cwd() -> PathBuf {
}
pub fn get_guaranteed_cwd(engine_state: &EngineState, stack: &Stack) -> PathBuf {
engine_state.cwd(Some(stack)).unwrap_or_else(|e| {
report_error_new(engine_state, &e);
crate::util::get_init_cwd()
})
engine_state
.cwd(Some(stack))
.unwrap_or(crate::util::get_init_cwd())
}
type MakeRangeError = fn(&str, Span) -> ShellError;