# Buggy code
def _first_writable_envs_dir():
    # Starting in conda 4.3, we use the writability test on '..../pkgs/url.txt' to determine
    # writability of '..../envs'.
    from ..core.package_cache import PackageCache

    for envs_dir in context.envs_dirs:
        pkgs_dir = join(dirname(envs_dir), "pkgs")
        if PackageCache(pkgs_dir).is_writable:
            return envs_dir

    from ..exceptions import NotWritableError

    raise NotWritableError(context.envs_dirs[0])


# Traceback (PermissionError)
Traceback (most recent call last):
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/exceptions.py\", line 721, in __call__
return func(*args, **kwargs)
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/cli/main.py\", line 78, in _main
exit_code = do_call(args, p)
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/cli/conda_argparse.py\", line 75, in do_call
exit_code = getattr(module, func_name)(args, parser)
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/cli/main_install.py\", line 11, in execute
install(args, parser, 'install')
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/cli/install.py\", line 239, in install
handle_txn(progressive_fetch_extract, unlink_link_transaction, prefix, args, newenv)
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/cli/install.py\", line 269, in handle_txn
unlink_link_transaction.execute()
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/core/link.py\", line 220, in execute
self.verify()
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/common/io.py\", line 402, in decorated
return f(*args, **kwds)
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/core/link.py\", line 207, in verify
exceptions = self._verify(self.prefix_setups, self.prefix_action_groups)
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/core/link.py\", line 460, in _verify
cls._verify_transaction_level(prefix_setups),
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/core/link.py\", line 455, in <genexpr>
exceptions = tuple(exc for exc in concatv(
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/core/link.py\", line 323, in _verify_individual_level
error_result = axn.verify()
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/core/path_actions.py\", line 870, in verify
touch(USER_ENVIRONMENTS_TXT_FILE, mkdir=True, sudo_safe=True)
File \"/home/nbcommon/anaconda3_420/lib/python3.5/site-packages/conda/gateways/disk/update.py\", line 89, in touch
os.chown(path, uid, gid)
PermissionError: [Errno 1] Operation not permitted: '/home/nbcommon/.conda/environments.txt'

# Fixed code
def _first_writable_envs_dir():
    # Starting in conda 4.3, we use the writability test on '..../pkgs/url.txt' to determine
    # writability of '..../envs'.
    from ..core.package_cache import PackageCache

    for envs_dir in context.envs_dirs:
        pkgs_dir = join(dirname(envs_dir), "pkgs")
        if PackageCache(pkgs_dir).is_writable:
            return envs_dir

    from ..exceptions import NotWritableError

    raise NotWritableError(context.envs_dirs[0], None)