Re-implement 06ac67c84a the right way since the state `__env__` was being confused by the shell environment. Fixes #6129.