Repository: xonsh/xonsh

Issue:
Crash with FileNotFoundError when current working directory is deleted
<!--- Provide a general summary of the issue in the Title above -->
<!--- If you have a question along the lines of "How do I do this Bash command in xonsh"
please first look over the Bash to Xonsh translation guide: https://xon.sh/bash_to_xsh.html
If you don't find an answer there, please do open an issue! -->

## xonfig

<details>

```
$ xonfig
+------------------+----------------------+
| xonsh            | 0.10.1               |
| Git SHA          | e9b12c8b             |
| Commit Date      | Jul 24 22:47:37 2021 |
| Python           | 3.8.5                |
| PLY              | 3.11                 |
| have readline    | True                 |
| prompt toolkit   | 3.0.20               |
| shell type       | prompt_toolkit       |
| history backend  | sqlite               |
| pygments         | 2.10.0               |
| on posix         | True                 |
| on linux         | True                 |
| distro           | ubuntu               |
| on wsl           | False                |
| on darwin        | False                |
| on windows       | False                |
| on cygwin        | False                |
| on msys2         | False                |
| is superuser     | False                |
| default encoding | utf-8                |
| xonsh encoding   | utf-8                |
| encoding errors  | surrogateescape      |
| on jupyter       | False                |
| jupyter kernel   | None                 |
| xontrib 1        | abbrevs              |
| xontrib 2        | autojump             |
| xontrib 3        | prompt_ret_code      |
+------------------+----------------------+

```

</details>

## Expected Behavior
<!--- Tell us what should happen -->

Should not crash.

## Current Behavior
<!--- Tell us what happens instead of the expected behavior -->
<!--- If part of your bug report is a traceback, please first enter debug mode before triggering the error
To enter debug mode, set the environment variable `XONSH_DEBUG=1` _before_ starting `xonsh`.
On Linux and OSX, an easy way to to do this is to run `env XONSH_DEBUG=1 xonsh` -->

Xonsh crashed when current working directory is deleted

### Traceback (if applicable)

<details>

```
Traceback (most recent call last):
  File "/home/yikai/.local/lib/python3.8/site-packages/xonsh/__amalgam__.py", line 21338, in main
    sys.exit(main_xonsh(args))
  File "/home/yikai/.local/lib/python3.8/site-packages/xonsh/__amalgam__.py", line 21376, in main_xonsh
    shell.shell.cmdloop()
  File "/home/yikai/.local/lib/python3.8/site-packages/xonsh/ptk_shell/shell.py", line 390, in cmdloop
    line = self.precmd(line)
  File "/home/yikai/.local/lib/python3.8/site-packages/xonsh/__amalgam__.py", line 16393, in precmd
    self.precwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
Xonsh encountered an issue during launch
Failback to /home/yikai/.nix-profile/bin/fish
...
```

</details>

## Steps to Reproduce
<!--- Please try to write out a minimal reproducible snippet to trigger the bug, it will help us fix it! -->

```
mkdir /tmp/some_folder
cd /tmp/some_folder
rmdir /tmp/some_folder
cd
```

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**

Crash with FileNotFoundError when current working directory is deleted
<!--- Provide a general summary of the issue in the Title above -->
<!--- If you have a question along the lines of "How do I do this Bash command in xonsh"
please first look over the Bash to Xonsh translation guide: https://xon.sh/bash_to_xsh.html
If you don't find an answer there, please do open an issue! -->

## xonfig

<details>

```
$ xonfig
+------------------+----------------------+
| xonsh            | 0.10.1               |
| Git SHA          | e9b12c8b             |
| Commit Date      | Jul 24 22:47:37 2021 |
| Python           | 3.8.5                |
| PLY              | 3.11                 |
| have readline    | True                 |
| prompt toolkit   | 3.0.20               |
| shell type       | prompt_toolkit       |
| history backend  | sqlite               |
| pygments         | 2.10.0               |
| on posix         | True                 |
| on linux         | True                 |
| distro           | ubuntu               |
| on wsl           | False                |
| on darwin        | False                |
| on windows       | False                |
| on cygwin        | False                |
| on msys2         | False                |
| is superuser     | False                |
| default encoding | utf-8                |
| xonsh encoding   | utf-8                |
| encoding errors  | surrogateescape      |
| on jupyter       | False                |
| jupyter kernel   | None                 |
| xontrib 1        | abbrevs              |
| xontrib 2        | autojump             |
| xontrib 3        | prompt_ret_code      |
+------------------+----------------------+

```

</details>

## Expected Behavior
<!--- Tell us what should happen -->

Should not crash.

## Current Behavior
<!--- Tell us what happens instead of the expected behavior -->
<!--- If part of your bug report is a traceback, please first enter debug mode before triggering the error
To enter debug mode, set the environment variable `XONSH_DEBUG=1` _before_ starting `xonsh`.
On Linux and OSX, an easy way to to do this is to run `env XONSH_DEBUG=1 xonsh` -->

Xonsh crashed when current working directory is deleted

### Traceback (if applicable)

<details>

```
Traceback (most recent call last):
  File "/home/yikai/.local/lib/python3.8/site-packages/xonsh/__amalgam__.py", line 21338, in main
    sys.exit(main_xonsh(args))
  File "/home/yikai/.local/lib/python3.8/site-packages/xonsh/__amalgam__.py", line 21376, in main_xonsh
    shell.shell.cmdloop()
  File "/home/yikai/.local/lib/python3.8/site-packages/xonsh/ptk_shell/shell.py", line 390, in cmdloop
    line = self.precmd(line)
  File "/home/yikai/.local/lib/python3.8/site-packages/xonsh/__amalgam__.py", line 16393, in precmd
    self.precwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
Xonsh encountered an issue during launch
Failback to /home/yikai/.nix-profile/bin/fish
...
```

</details>

## Steps to Reproduce
<!--- Please try to write out a minimal reproducible snippet to trigger the bug, it will help us fix it! -->

```
mkdir /tmp/some_folder
cd /tmp/some_folder
rmdir /tmp/some_folder
cd
```

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**



Discussion:
Good catch! PR is very welcome!

Here should be the fix:

https://github.com/xonsh/xonsh/blob/e25ab34e328c09b3098b598346ddda06ab7b40b5/xonsh/base_shell.py#L358-L361
I will attempt to patch this when Hacktoberfest starts.
Good catch! PR is very welcome!

Here should be the fix:

https://github.com/xonsh/xonsh/blob/e25ab34e328c09b3098b598346ddda06ab7b40b5/xonsh/base_shell.py#L358-L361
I will attempt to patch this when Hacktoberfest starts.

Resolving patch:
diff --git a/xonsh/base_shell.py b/xonsh/base_shell.py
--- a/xonsh/base_shell.py
+++ b/xonsh/base_shell.py
@@ -4,6 +4,7 @@
 import os
 import sys
 import time
+from os.path import expanduser
 
 from xonsh.built_ins import XSH
 from xonsh.tools import (
@@ -357,7 +358,10 @@ def singleline(self, **kwargs):
 
     def precmd(self, line):
         """Called just before execution of line."""
-        self.precwd = os.getcwd()
+        try:
+            self.precwd = os.getcwd()
+        except FileNotFoundError:
+            self.precwd = expanduser("~")
         return line if self.need_more_lines else line.lstrip()
 
     def default(self, line, raw_line=None):


Test patch: