We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
conda is broken if your home directory is read-only
Conda currently requires the user's home directory to be writable.

If the directory conda is installed into is writable (say a tmpfs) then you can get along way by using 
```shell
		./Miniconda3-latest-Linux-x86_64.sh -p $CONDA_DIR -b -f
		conda config --system --set always_yes yes
		conda config --system --set changeps1 no
		conda config --system --add envs_dirs $CONDA_DIR/envs
		conda config --system --add pkgs_dirs $CONDA_DIR/pkgs
```

However, this is foiled by the following line -> https://github.com/conda/conda/blob/7616b87ad87b80da16b8263011c9c708be98147c/conda/core/envs_manager.py#L18

```python
USER_ENVIRONMENTS_TXT_FILE = expand(join('~', '.conda', 'environments.txt'))
```

I'm not sure if this would even work on Windows?


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `conda/core/envs_manager.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 from __future__ import absolute_import, division, print_function, unicode_literals
3 
4 from logging import getLogger
5 from os import listdir
6 from os.path import dirname, isdir, isfile, join, normpath, split as path_split
7 
8 from ..base.constants import ROOT_ENV_NAME
9 from ..base.context import context
10 from ..common.compat import ensure_text_type, on_win, open
11 from ..common.path import expand, paths_equal
12 from ..gateways.disk.read import yield_lines
13 from ..gateways.disk.test import is_conda_environment
14 
15 log = getLogger(__name__)
16 
17 
18 USER_ENVIRONMENTS_TXT_FILE = expand(join('~', '.conda', 'environments.txt'))
19 
20 
21 def register_env(location):
22     location = normpath(location)
23 
24     if "placehold_pl" in location:
25         # Don't record envs created by conda-build.
26         return
27 
28     if location in yield_lines(USER_ENVIRONMENTS_TXT_FILE):
29         # Nothing to do. Location is already recorded in a known environments.txt file.
30         return
31 
32     with open(USER_ENVIRONMENTS_TXT_FILE, 'a') as fh:
33         fh.write(ensure_text_type(location))
34         fh.write('\n')
35 
36 
37 def unregister_env(location):
38     if isdir(location):
39         meta_dir = join(location, 'conda-meta')
40         if isdir(meta_dir):
41             meta_dir_contents = listdir(meta_dir)
42             if len(meta_dir_contents) > 1:
43                 # if there are any files left other than 'conda-meta/history'
44                 #   then don't unregister
45                 return
46 
47     _clean_environments_txt(USER_ENVIRONMENTS_TXT_FILE, location)
48 
49 
50 def list_all_known_prefixes():
51     all_env_paths = set()
52     if on_win:
53         home_dir_dir = dirname(expand('~'))
54         for home_dir in listdir(home_dir_dir):
55             environments_txt_file = join(home_dir_dir, home_dir, '.conda', 'environments.txt')
56             if isfile(environments_txt_file):
57                 all_env_paths.update(_clean_environments_txt(environments_txt_file))
58     else:
59         from os import geteuid
60         from pwd import getpwall
61         if geteuid() == 0:
62             search_dirs = tuple(pwentry.pw_dir for pwentry in getpwall()) or (expand('~'),)
63         else:
64             search_dirs = (expand('~'),)
65         for home_dir in search_dirs:
66             environments_txt_file = join(home_dir, '.conda', 'environments.txt')
67             if isfile(environments_txt_file):
68                 all_env_paths.update(_clean_environments_txt(environments_txt_file))
69 
70     # in case environments.txt files aren't complete, also add all known conda environments in
71     # all envs_dirs
72     envs_dirs = (envs_dir for envs_dir in context.envs_dirs if isdir(envs_dir))
73     all_env_paths.update(path for path in (
74         join(envs_dir, name) for envs_dir in envs_dirs for name in listdir(envs_dir)
75     ) if path not in all_env_paths and is_conda_environment(path))
76 
77     all_env_paths.add(context.root_prefix)
78     return sorted(all_env_paths)
79 
80 
81 def env_name(prefix):
82     if not prefix:
83         return None
84     if paths_equal(prefix, context.root_prefix):
85         return ROOT_ENV_NAME
86     maybe_envs_dir, maybe_name = path_split(prefix)
87     for envs_dir in context.envs_dirs:
88         if paths_equal(envs_dir, maybe_envs_dir):
89             return maybe_name
90     return prefix
91 
92 
93 def _clean_environments_txt(environments_txt_file, remove_location=None):
94     if not isfile(environments_txt_file):
95         return ()
96 
97     if remove_location:
98         remove_location = normpath(remove_location)
99     environments_txt_lines = tuple(yield_lines(environments_txt_file))
100     environments_txt_lines_cleaned = tuple(
101         prefix for prefix in environments_txt_lines
102         if prefix != remove_location and is_conda_environment(prefix)
103     )
104     if environments_txt_lines_cleaned != environments_txt_lines:
105         _rewrite_environments_txt(environments_txt_file, environments_txt_lines_cleaned)
106     return environments_txt_lines_cleaned
107 
108 
109 def _rewrite_environments_txt(environments_txt_file, prefixes):
110     try:
111         with open(environments_txt_file, 'w') as fh:
112             fh.write('\n'.join(prefixes))
113             fh.write('\n')
114     except (IOError, OSError) as e:
115         log.info("File not cleaned: %s", environments_txt_file)
116         log.debug('%r', e, exc_info=True)
117
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/conda/core/envs_manager.py b/conda/core/envs_manager.py
--- a/conda/core/envs_manager.py
+++ b/conda/core/envs_manager.py
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import absolute_import, division, print_function, unicode_literals
 
+from errno import EACCES
 from logging import getLogger
 from os import listdir
 from os.path import dirname, isdir, isfile, join, normpath, split as path_split
@@ -29,9 +30,17 @@
         # Nothing to do. Location is already recorded in a known environments.txt file.
         return
 
-    with open(USER_ENVIRONMENTS_TXT_FILE, 'a') as fh:
-        fh.write(ensure_text_type(location))
-        fh.write('\n')
+    try:
+        with open(USER_ENVIRONMENTS_TXT_FILE, 'a') as fh:
+            fh.write(ensure_text_type(location))
+            fh.write('\n')
+    except EnvironmentError as e:
+        if e.errno == EACCES:
+            log.warn("Unable to register environment. Path not writable.\n"
+                     "  environment location: %s\n"
+                     "  registry file: %s", location, USER_ENVIRONMENTS_TXT_FILE)
+        else:
+            raise
 
 
 def unregister_env(location):
@@ -111,6 +120,6 @@
         with open(environments_txt_file, 'w') as fh:
             fh.write('\n'.join(prefixes))
             fh.write('\n')
-    except (IOError, OSError) as e:
+    except EnvironmentError as e:
         log.info("File not cleaned: %s", environments_txt_file)
         log.debug('%r', e, exc_info=True)

{"golden_diff": "diff --git a/conda/core/envs_manager.py b/conda/core/envs_manager.py\n--- a/conda/core/envs_manager.py\n+++ b/conda/core/envs_manager.py\n@@ -1,6 +1,7 @@\n # -*- coding: utf-8 -*-\n from __future__ import absolute_import, division, print_function, unicode_literals\n \n+from errno import EACCES\n from logging import getLogger\n from os import listdir\n from os.path import dirname, isdir, isfile, join, normpath, split as path_split\n@@ -29,9 +30,17 @@\n         # Nothing to do. Location is already recorded in a known environments.txt file.\n         return\n \n-    with open(USER_ENVIRONMENTS_TXT_FILE, 'a') as fh:\n-        fh.write(ensure_text_type(location))\n-        fh.write('\\n')\n+    try:\n+        with open(USER_ENVIRONMENTS_TXT_FILE, 'a') as fh:\n+            fh.write(ensure_text_type(location))\n+            fh.write('\\n')\n+    except EnvironmentError as e:\n+        if e.errno == EACCES:\n+            log.warn(\"Unable to register environment. Path not writable.\\n\"\n+                     \"  environment location: %s\\n\"\n+                     \"  registry file: %s\", location, USER_ENVIRONMENTS_TXT_FILE)\n+        else:\n+            raise\n \n \n def unregister_env(location):\n@@ -111,6 +120,6 @@\n         with open(environments_txt_file, 'w') as fh:\n             fh.write('\\n'.join(prefixes))\n             fh.write('\\n')\n-    except (IOError, OSError) as e:\n+    except EnvironmentError as e:\n         log.info(\"File not cleaned: %s\", environments_txt_file)\n         log.debug('%r', e, exc_info=True)\n", "issue": "conda is broken if your home directory is read-only\nConda currently requires the user's home directory to be writable.\r\n\r\nIf the directory conda is installed into is writable (say a tmpfs) then you can get along way by using \r\n```shell\r\n\t\t./Miniconda3-latest-Linux-x86_64.sh -p $CONDA_DIR -b -f\r\n\t\tconda config --system --set always_yes yes\r\n\t\tconda config --system --set changeps1 no\r\n\t\tconda config --system --add envs_dirs $CONDA_DIR/envs\r\n\t\tconda config --system --add pkgs_dirs $CONDA_DIR/pkgs\r\n```\r\n\r\nHowever, this is foiled by the following line -> https://github.com/conda/conda/blob/7616b87ad87b80da16b8263011c9c708be98147c/conda/core/envs_manager.py#L18\r\n\r\n```python\r\nUSER_ENVIRONMENTS_TXT_FILE = expand(join('~', '.conda', 'environments.txt'))\r\n```\r\n\r\nI'm not sure if this would even work on Windows?\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nfrom logging import getLogger\nfrom os import listdir\nfrom os.path import dirname, isdir, isfile, join, normpath, split as path_split\n\nfrom ..base.constants import ROOT_ENV_NAME\nfrom ..base.context import context\nfrom ..common.compat import ensure_text_type, on_win, open\nfrom ..common.path import expand, paths_equal\nfrom ..gateways.disk.read import yield_lines\nfrom ..gateways.disk.test import is_conda_environment\n\nlog = getLogger(__name__)\n\n\nUSER_ENVIRONMENTS_TXT_FILE = expand(join('~', '.conda', 'environments.txt'))\n\n\ndef register_env(location):\n    location = normpath(location)\n\n    if \"placehold_pl\" in location:\n        # Don't record envs created by conda-build.\n        return\n\n    if location in yield_lines(USER_ENVIRONMENTS_TXT_FILE):\n        # Nothing to do. Location is already recorded in a known environments.txt file.\n        return\n\n    with open(USER_ENVIRONMENTS_TXT_FILE, 'a') as fh:\n        fh.write(ensure_text_type(location))\n        fh.write('\\n')\n\n\ndef unregister_env(location):\n    if isdir(location):\n        meta_dir = join(location, 'conda-meta')\n        if isdir(meta_dir):\n            meta_dir_contents = listdir(meta_dir)\n            if len(meta_dir_contents) > 1:\n                # if there are any files left other than 'conda-meta/history'\n                #   then don't unregister\n                return\n\n    _clean_environments_txt(USER_ENVIRONMENTS_TXT_FILE, location)\n\n\ndef list_all_known_prefixes():\n    all_env_paths = set()\n    if on_win:\n        home_dir_dir = dirname(expand('~'))\n        for home_dir in listdir(home_dir_dir):\n            environments_txt_file = join(home_dir_dir, home_dir, '.conda', 'environments.txt')\n            if isfile(environments_txt_file):\n                all_env_paths.update(_clean_environments_txt(environments_txt_file))\n    else:\n        from os import geteuid\n        from pwd import getpwall\n        if geteuid() == 0:\n            search_dirs = tuple(pwentry.pw_dir for pwentry in getpwall()) or (expand('~'),)\n        else:\n            search_dirs = (expand('~'),)\n        for home_dir in search_dirs:\n            environments_txt_file = join(home_dir, '.conda', 'environments.txt')\n            if isfile(environments_txt_file):\n                all_env_paths.update(_clean_environments_txt(environments_txt_file))\n\n    # in case environments.txt files aren't complete, also add all known conda environments in\n    # all envs_dirs\n    envs_dirs = (envs_dir for envs_dir in context.envs_dirs if isdir(envs_dir))\n    all_env_paths.update(path for path in (\n        join(envs_dir, name) for envs_dir in envs_dirs for name in listdir(envs_dir)\n    ) if path not in all_env_paths and is_conda_environment(path))\n\n    all_env_paths.add(context.root_prefix)\n    return sorted(all_env_paths)\n\n\ndef env_name(prefix):\n    if not prefix:\n        return None\n    if paths_equal(prefix, context.root_prefix):\n        return ROOT_ENV_NAME\n    maybe_envs_dir, maybe_name = path_split(prefix)\n    for envs_dir in context.envs_dirs:\n        if paths_equal(envs_dir, maybe_envs_dir):\n            return maybe_name\n    return prefix\n\n\ndef _clean_environments_txt(environments_txt_file, remove_location=None):\n    if not isfile(environments_txt_file):\n        return ()\n\n    if remove_location:\n        remove_location = normpath(remove_location)\n    environments_txt_lines = tuple(yield_lines(environments_txt_file))\n    environments_txt_lines_cleaned = tuple(\n        prefix for prefix in environments_txt_lines\n        if prefix != remove_location and is_conda_environment(prefix)\n    )\n    if environments_txt_lines_cleaned != environments_txt_lines:\n        _rewrite_environments_txt(environments_txt_file, environments_txt_lines_cleaned)\n    return environments_txt_lines_cleaned\n\n\ndef _rewrite_environments_txt(environments_txt_file, prefixes):\n    try:\n        with open(environments_txt_file, 'w') as fh:\n            fh.write('\\n'.join(prefixes))\n            fh.write('\\n')\n    except (IOError, OSError) as e:\n        log.info(\"File not cleaned: %s\", environments_txt_file)\n        log.debug('%r', e, exc_info=True)\n", "path": "conda/core/envs_manager.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\nfrom errno import EACCES\nfrom logging import getLogger\nfrom os import listdir\nfrom os.path import dirname, isdir, isfile, join, normpath, split as path_split\n\nfrom ..base.constants import ROOT_ENV_NAME\nfrom ..base.context import context\nfrom ..common.compat import ensure_text_type, on_win, open\nfrom ..common.path import expand, paths_equal\nfrom ..gateways.disk.read import yield_lines\nfrom ..gateways.disk.test import is_conda_environment\n\nlog = getLogger(__name__)\n\n\nUSER_ENVIRONMENTS_TXT_FILE = expand(join('~', '.conda', 'environments.txt'))\n\n\ndef register_env(location):\n    location = normpath(location)\n\n    if \"placehold_pl\" in location:\n        # Don't record envs created by conda-build.\n        return\n\n    if location in yield_lines(USER_ENVIRONMENTS_TXT_FILE):\n        # Nothing to do. Location is already recorded in a known environments.txt file.\n        return\n\n    try:\n        with open(USER_ENVIRONMENTS_TXT_FILE, 'a') as fh:\n            fh.write(ensure_text_type(location))\n            fh.write('\\n')\n    except EnvironmentError as e:\n        if e.errno == EACCES:\n            log.warn(\"Unable to register environment. Path not writable.\\n\"\n                     \"  environment location: %s\\n\"\n                     \"  registry file: %s\", location, USER_ENVIRONMENTS_TXT_FILE)\n        else:\n            raise\n\n\ndef unregister_env(location):\n    if isdir(location):\n        meta_dir = join(location, 'conda-meta')\n        if isdir(meta_dir):\n            meta_dir_contents = listdir(meta_dir)\n            if len(meta_dir_contents) > 1:\n                # if there are any files left other than 'conda-meta/history'\n                #   then don't unregister\n                return\n\n    _clean_environments_txt(USER_ENVIRONMENTS_TXT_FILE, location)\n\n\ndef list_all_known_prefixes():\n    all_env_paths = set()\n    if on_win:\n        home_dir_dir = dirname(expand('~'))\n        for home_dir in listdir(home_dir_dir):\n            environments_txt_file = join(home_dir_dir, home_dir, '.conda', 'environments.txt')\n            if isfile(environments_txt_file):\n                all_env_paths.update(_clean_environments_txt(environments_txt_file))\n    else:\n        from os import geteuid\n        from pwd import getpwall\n        if geteuid() == 0:\n            search_dirs = tuple(pwentry.pw_dir for pwentry in getpwall()) or (expand('~'),)\n        else:\n            search_dirs = (expand('~'),)\n        for home_dir in search_dirs:\n            environments_txt_file = join(home_dir, '.conda', 'environments.txt')\n            if isfile(environments_txt_file):\n                all_env_paths.update(_clean_environments_txt(environments_txt_file))\n\n    # in case environments.txt files aren't complete, also add all known conda environments in\n    # all envs_dirs\n    envs_dirs = (envs_dir for envs_dir in context.envs_dirs if isdir(envs_dir))\n    all_env_paths.update(path for path in (\n        join(envs_dir, name) for envs_dir in envs_dirs for name in listdir(envs_dir)\n    ) if path not in all_env_paths and is_conda_environment(path))\n\n    all_env_paths.add(context.root_prefix)\n    return sorted(all_env_paths)\n\n\ndef env_name(prefix):\n    if not prefix:\n        return None\n    if paths_equal(prefix, context.root_prefix):\n        return ROOT_ENV_NAME\n    maybe_envs_dir, maybe_name = path_split(prefix)\n    for envs_dir in context.envs_dirs:\n        if paths_equal(envs_dir, maybe_envs_dir):\n            return maybe_name\n    return prefix\n\n\ndef _clean_environments_txt(environments_txt_file, remove_location=None):\n    if not isfile(environments_txt_file):\n        return ()\n\n    if remove_location:\n        remove_location = normpath(remove_location)\n    environments_txt_lines = tuple(yield_lines(environments_txt_file))\n    environments_txt_lines_cleaned = tuple(\n        prefix for prefix in environments_txt_lines\n        if prefix != remove_location and is_conda_environment(prefix)\n    )\n    if environments_txt_lines_cleaned != environments_txt_lines:\n        _rewrite_environments_txt(environments_txt_file, environments_txt_lines_cleaned)\n    return environments_txt_lines_cleaned\n\n\ndef _rewrite_environments_txt(environments_txt_file, prefixes):\n    try:\n        with open(environments_txt_file, 'w') as fh:\n            fh.write('\\n'.join(prefixes))\n            fh.write('\\n')\n    except EnvironmentError as e:\n        log.info(\"File not cleaned: %s\", environments_txt_file)\n        log.debug('%r', e, exc_info=True)\n", "path": "conda/core/envs_manager.py"}]}