Repository: conda/conda

Issue:
os.getcwd() FileNotFoundError
```
error: FileNotFoundError(2, 'No such file or directory')
command: /root/anaconda3/bin/conda shell.posix deactivate
user_agent: conda/4.4.4 requests/2.18.4 CPython/3.6.3 Linux/4.13.0-kali1-amd64 kali/2017.3 glibc/2.25
_messageid: -9223372036844975611
_messagetime: 1514233339489 / 2017-12-25 14:22:19

Traceback (most recent call last):
  File "/root/anaconda3/lib/python3.6/site-packages/conda/cli/main.py", line 110, in main
    from ..activate import main as activator_main
  File "/root/anaconda3/lib/python3.6/site-packages/conda/activate.py", line 12, in <module>
    context.__init__()  # oOn import, context does not include SEARCH_PATH. This line fixes that.
  File "/root/anaconda3/lib/python3.6/site-packages/conda/base/context.py", line 214, in __init__
    argparse_args=argparse_args)
  File "/root/anaconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 723, in __init__
    self._set_search_path(search_path)
  File "/root/anaconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 729, in _set_search_path
    self._set_raw_data(load_file_configs(search_path))
  File "/root/anaconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 366, in load_file_configs
    expanded_paths = tuple(expand(path) for path in search_path)
  File "/root/anaconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 366, in <genexpr>
    expanded_paths = tuple(expand(path) for path in search_path)
  File "/root/anaconda3/lib/python3.6/site-packages/conda/common/path.py", line 49, in expand
    return abspath(expanduser(expandvars(path)))
  File "/root/anaconda3/lib/python3.6/posixpath.py", line 374, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
```


Discussion:
```
error: FileNotFoundError(2, 'No such file or directory')
command: /Users/limxing/anaconda3/bin/conda install iris
user_agent: conda/4.4.4 requests/2.18.4 CPython/3.6.4 Darwin/17.3.0 OSX/10.13.2
_messageid: -9223372036844475580
_messagetime: 1514289512434 / 2017-12-26 05:58:32

Traceback (most recent call last):
  File "/Users/limxing/anaconda3/lib/python3.6/site-packages/conda/exceptions.py", line 724, in __call__
    return func(*args, **kwargs)
  File "/Users/limxing/anaconda3/lib/python3.6/site-packages/conda/cli/main.py", line 74, in _main
    context.__init__(argparse_args=args)
  File "/Users/limxing/anaconda3/lib/python3.6/site-packages/conda/base/context.py", line 214, in __init__
    argparse_args=argparse_args)
  File "/Users/limxing/anaconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 723, in __init__
    self._set_search_path(search_path)
  File "/Users/limxing/anaconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 729, in _set_search_path
    self._set_raw_data(load_file_configs(search_path))
  File "/Users/limxing/anaconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 366, in load_file_configs
    expanded_paths = tuple(expand(path) for path in search_path)
  File "/Users/limxing/anaconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 366, in <genexpr>
    expanded_paths = tuple(expand(path) for path in search_path)
  File "/Users/limxing/anaconda3/lib/python3.6/site-packages/conda/common/path.py", line 49, in expand
    return abspath(expanduser(expandvars(path)))
  File "/Users/limxing/anaconda3/lib/python3.6/posixpath.py", line 374, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
```
```
error: FileNotFoundError(2, 'No such file or directory')
command: /home/anti/miniconda3/bin/conda update --all
user_agent: conda/4.4.4 requests/2.18.4 CPython/3.6.4 Linux/3.10.0-693.11.1.el7.x86_64 centos/7 glibc/2.17
_messageid: -9223372036844375596
_messagetime: 1514305043219 / 2017-12-26 10:17:23

Traceback (most recent call last):
  File "/home/anti/miniconda3/lib/python3.6/site-packages/conda/exceptions.py", line 724, in __call__
    return func(*args, **kwargs)
  File "/home/anti/miniconda3/lib/python3.6/site-packages/conda/cli/main.py", line 74, in _main
    context.__init__(argparse_args=args)
  File "/home/anti/miniconda3/lib/python3.6/site-packages/conda/base/context.py", line 214, in __init__
    argparse_args=argparse_args)
  File "/home/anti/miniconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 723, in __init__
    self._set_search_path(search_path)
  File "/home/anti/miniconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 729, in _set_search_path
    self._set_raw_data(load_file_configs(search_path))
  File "/home/anti/miniconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 366, in load_file_configs
    expanded_paths = tuple(expand(path) for path in search_path)
  File "/home/anti/miniconda3/lib/python3.6/site-packages/conda/common/configuration.py", line 366, in <genexpr>
    expanded_paths = tuple(expand(path) for path in search_path)
  File "/home/anti/miniconda3/lib/python3.6/site-packages/conda/common/path.py", line 49, in expand
    return abspath(expanduser(expandvars(path)))
  File "/home/anti/miniconda3/lib/python3.6/posixpath.py", line 374, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
```
https://stackoverflow.com/questions/23889636/error-while-executing-os-getcwd

Resolving patch:
diff --git a/conda/base/context.py b/conda/base/context.py
--- a/conda/base/context.py
+++ b/conda/base/context.py
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import absolute_import, division, print_function, unicode_literals
 
+from errno import ENOENT
 from logging import getLogger
 import os
 from os.path import (abspath, basename, dirname, expanduser, isdir, isfile, join, normpath,
@@ -30,6 +31,16 @@
 except ImportError:  # pragma: no cover
     from .._vendor.toolz.itertoolz import concat, concatv, unique
 
+try:
+    os.getcwd()
+except (IOError, OSError) as e:
+    if e.errno == ENOENT:
+        # FileNotFoundError can occur when cwd has been deleted out from underneath the process.
+        # To resolve #6584, let's go with setting cwd to sys.prefix, and see how far we get.
+        os.chdir(sys.prefix)
+    else:
+        raise
+
 log = getLogger(__name__)
 
 _platform_map = {


Test patch: