Repository: pypa/pipenv

Issue:
Error when exit from pipenv shell
Windows 10 x64 + python 3.6.3

```
λ exit
Traceback (most recent call last):
  File "c:\tools\anaconda3\lib\site-packages\pipenv\core.py", line 2107, in do_shell
    c = pexpect.spawn(
AttributeError: module 'pexpect' has no attribute 'spawn'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\tools\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\tools\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\tools\Anaconda3\Scripts\pipenv.exe\__main__.py", line 9, in <module>
  File "c:\tools\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\tools\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\vendor\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\cli.py", line 281, in shell
    core.do_shell(three=three, python=python, fancy=fancy, shell_args=shell_args)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\core.py", line 2123, in do_shell
    pew.workon_cmd([workon_name])
  File "c:\tools\anaconda3\lib\site-packages\pipenv\patched\pew\pew.py", line 368, in workon_cmd
    shell(env, cwd=project_dir)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\patched\pew\pew.py", line 208, in shell
    fork_cmder(env, cwd)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\patched\pew\pew.py", line 179, in fork_cmder
    fork_shell(env, shell_cmd, cwd)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\patched\pew\pew.py", line 154, in fork_shell
    inve(env, *shellcmd, cwd=cwd)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\patched\pew\pew.py", line 137, in inve
    return check_call([command] + list(args), shell=windows, **kwargs)
  File "c:\tools\anaconda3\lib\site-packages\pipenv\patched\pew\_utils.py", line 44, in call
    return f([ex] + list(cmd[1:]), **kwargs)  # list-conversion is required in case `cmd` is a tuple
  File "c:\tools\anaconda3\lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['C:\\Windows\\system32\\cmd.EXE', '/k', '%CMDER_ROOT%\\vendor\\init.bat']' returned non-zero exit status 1.
```


Discussion:
Are you running this inside cmder?
Yes
we dont **officially** support cmder, but this is probably a duplicate of #1689 so closing in favor of that
Actually it is not (at least I don’t think so because it is still reproducible after #1715). I’ll try to investigate if this is amendable, but no promises atm.
This is actually reproducible without Cmder. The thing is cmd.exe (also PowerShell, both Core or not) exits with *whatever the last command it executed exited with*. So this works:

```
> pipenv shell
(subshell) > dir /w
[directory content...]
(subshell) > exit
>
```

But this spits out an error:

```
> pipenv shell
(subshell) > meh
'meh' is not recognized as an internal or external command,
operable program or batch file.
(subshell) > exit
[long traceback]
  File "Lib\subprocess.py", line 291, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['C:\\WINDOWS\\system32\\cmd.EXE']' returned non-zero exit status 9009.
>
```

I don’t have a good solution but to suppress the exception.
Why don't we just move back to using a subprocess?
It won’t help in this case. Even if we use a subprocess, the subprocess of the subprocess will still report an error, which will bubble up unless we suppress it somewhere. I suspect this problem have existed all along (it affects all pew users), just never surfaced because 1. Windows users are scarce, and 2. nobody could reproduce it due to its obscure nature.

Resolving patch:
diff --git a/pipenv/patched/pew/pew.py b/pipenv/patched/pew/pew.py
--- a/pipenv/patched/pew/pew.py
+++ b/pipenv/patched/pew/pew.py
@@ -153,7 +153,16 @@ def fork_shell(env, shellcmd, cwd):
     if 'VIRTUAL_ENV' in os.environ:
         err("Be aware that this environment will be nested on top "
             "of '%s'" % Path(os.environ['VIRTUAL_ENV']).name)
-    inve(env, *shellcmd, cwd=cwd)
+    try:
+        inve(env, *shellcmd, cwd=cwd)
+    except CalledProcessError:
+        # These shells report errors when the last command executed in the
+        # subshell in an error. This causes the subprocess to fail, which is
+        # not what we want. Stay silent for them, there's nothing we can do.
+        shell_name, _ = os.path.splitext(os.path.basename(shellcmd[0]))
+        suppress_error = shell_name.tolower() in ['cmd', 'powershell', 'pwsh']
+        if not suppress_error:
+            raise
 
 
 def fork_bash(env, cwd):


Test patch: