Fix traceback when cwd does not exist

Fixes #7154.