No longer crash when PWD is removed

This commit is contained in:
Anthony Scopatz 2017-02-26 12:49:26 -05:00
parent e572c85532
commit 4f3910fe32
4 changed files with 45 additions and 4 deletions

18
news/delcwd.rst Normal file
View file

@ -0,0 +1,18 @@
**Added:**
* Xonsh will issue a warning message when the current working
directory has been remove out from under it and not replaced
prior to running the next command.
**Changed:** None
**Deprecated:** None
**Removed:** None
**Fixed:**
* Xonsh will no longer crash is the current working directory is
removed out from it.
**Security:** None

View file

@ -375,8 +375,26 @@ class BaseShell(object):
def _fix_cwd(self):
"""Check if the cwd changed out from under us."""
env = builtins.__xonsh_env__
try:
cwd = os.getcwd()
if 'PWD' not in env:
except (FileNotFoundError, OSError):
cwd = None
if cwd is None:
# directory has been deleted out from under us, most likely
pwd = env.get('PWD', None)
if pwd is None:
# we have no idea where we are
env['PWD'] = '<invalid directory>'
elif os.path.isdir(pwd):
# unclear why os.getcwd() failed. do nothing.
pass
else:
# OK PWD is really gone.
msg = '{UNDERLINE_INTENSE_WHITE}{BACKGROUND_INTENSE_BLACK}'
msg += "xonsh: working directory does not exist: " + pwd
msg += '{NO_COLOR}'
self.print_color(msg, file=sys.stderr)
elif 'PWD' not in env:
# $PWD is missing from env, recreate it
env['PWD'] = cwd
elif os.path.realpath(cwd) != os.path.realpath(env['PWD']):

View file

@ -54,8 +54,10 @@ def get_git_branch():
def _get_hg_root(q):
_curpwd = os.getcwd()
_curpwd = builtins.__xonsh_env__['PWD']
while True:
if not os.path.isdir(_curpwd):
return False
if any([b.name == '.hg' for b in os.scandir(_curpwd)]):
q.put(_curpwd)
break

View file

@ -34,7 +34,10 @@ load_module_in_background('pkg_resources', debug='XONSH_DEBUG',
def _command_is_valid(cmd):
try:
cmd_abspath = os.path.abspath(os.path.expanduser(cmd))
except (FileNotFoundError, OSError):
return False
return cmd in builtins.__xonsh_commands_cache__ or \
(os.path.isfile(cmd_abspath) and os.access(cmd_abspath, os.X_OK))