# Buggy code
def remove_actions(prefix, specs, index, force=False, pinned=True):
    r = Resolve(index)
    linked = linked_data(prefix)
    linked_dists = [d for d in linked.keys()]

    if force:
        mss = list(map(MatchSpec, specs))
        nlinked = {
            r.package_name(dist): dist
            for dist in linked_dists
            if not any(r.match(ms, dist) for ms in mss)
        }
    else:
        add_defaults_to_specs(r, linked_dists, specs, update=True)
        nlinked = {
            r.package_name(dist): dist
            for dist in (Dist(fn) for fn in r.remove(specs, r.installed))
        }

    if pinned:
        pinned_specs = get_pinned_specs(prefix)
        log.debug("Pinned specs=%s" % pinned_specs)

    linked = {r.package_name(dist): dist for dist in linked_dists}

    actions = ensure_linked_actions(r.dependency_sort(nlinked), prefix)
    for old_dist in reversed(r.dependency_sort(linked)):
        # dist = old_fn + '.tar.bz2'
        name = r.package_name(old_dist)
        if old_dist == nlinked.get(name):
            continue
        if pinned and any(r.match(ms, old_dist.to_filename()) for ms in pinned_specs):
            msg = "Cannot remove %s because it is pinned. Use --no-pin to override."
            raise CondaRuntimeError(msg % old_dist.to_filename())
        if (
            context.conda_in_root
            and name == "conda"
            and name not in nlinked
            and not context.force
        ):
            if any(s.split(" ", 1)[0] == "conda" for s in specs):
                raise RemoveError("'conda' cannot be removed from the root environment")
            else:
                raise RemoveError(
                    "Error: this 'remove' command cannot be executed because it\n"
                    "would require removing 'conda' dependencies"
                )
        add_unlink(actions, old_dist)

    return actions


# Traceback (KeyError)
Current conda install:

platform : linux-64

conda version : 4.3.4

conda is private : False

conda-env version : 4.3.4

conda-build version : not installed

python version : 2.7.13.final.0

requests version : 2.12.4

root environment : /home/travis/miniconda  (writable)

default environment : /home/travis/miniconda/envs/test

envs directories : /home/travis/miniconda/envs

package cache : /home/travis/miniconda/pkgs

channel URLs : https://conda.anaconda.org/astropy-ci-extras astropy/linux-64

https://conda.anaconda.org/astropy-ci-extras astropy/noarch

https://conda.anaconda.org/astropy/linux-64

https://conda.anaconda.org/astropy/noarch

https://conda.anaconda.org/astropy-ci-extras/linux-64

https://conda.anaconda.org/astropy-ci-extras/noarch

https://repo.continuum.io/pkgs/free/linux-64

https://repo.continuum.io/pkgs/free/noarch

https://repo.continuum.io/pkgs/r/linux-64

https://repo.continuum.io/pkgs/r/noarch

https://repo.continuum.io/pkgs/pro/linux-64

https://repo.continuum.io/pkgs/pro/noarch

config file : /home/travis/.condarc

offline mode : False

user-agent : conda/4.3.4 requests/2.12.4 CPython/2.7.13 Linux/4.8.12-040812-generic debian/wheezy/sid glibc/2.15

UID:GID : 1000:1000

`$ /home/travis/miniconda/envs/test/bin/conda remove astropy --force`

Traceback (most recent call last):

File "/home/travis/miniconda/lib/python2.7/site-packages/conda/exceptions.py", line 617, in conda_exception_handler

return_value = func(*args, **kwargs)

File "/home/travis/miniconda/lib/python2.7/site-packages/conda/cli/main.py", line 158, in _main

exit_code = args.func(args, p)

File "/home/travis/miniconda/lib/python2.7/site-packages/conda/cli/main_remove.py", line 164, in execute

pinned=args.pinned))

File "/home/travis/miniconda/lib/python2.7/site-packages/conda/plan.py", line 776, in remove_actions

if pinned and any(r.match(ms, old_dist.to_filename()) for ms in pinned_specs):

File "/home/travis/miniconda/lib/python2.7/site-packages/conda/plan.py", line 776, in <genexpr>

if pinned and any(r.match(ms, old_dist.to_filename()) for ms in pinned_specs):

File "/home/travis/miniconda/lib/python2.7/site-packages/conda/resolve.py", line 474, in match

rec = self.index[fkey]

KeyError: u'astropy-1.0.3-np19py33_0.tar.bz2'

# Fixed code
def remove_actions(prefix, specs, index, force=False, pinned=True):
    r = Resolve(index)
    linked = linked_data(prefix)
    linked_dists = [d for d in linked.keys()]

    if force:
        mss = list(map(MatchSpec, specs))
        nlinked = {
            r.package_name(dist): dist
            for dist in linked_dists
            if not any(r.match(ms, dist) for ms in mss)
        }
    else:
        add_defaults_to_specs(r, linked_dists, specs, update=True)
        nlinked = {
            r.package_name(dist): dist
            for dist in (Dist(fn) for fn in r.remove(specs, r.installed))
        }

    if pinned:
        pinned_specs = get_pinned_specs(prefix)
        log.debug("Pinned specs=%s" % pinned_specs)

    linked = {r.package_name(dist): dist for dist in linked_dists}

    actions = ensure_linked_actions(r.dependency_sort(nlinked), prefix)
    for old_dist in reversed(r.dependency_sort(linked)):
        # dist = old_fn + '.tar.bz2'
        name = r.package_name(old_dist)
        if old_dist == nlinked.get(name):
            continue
        if pinned and any(r.match(ms, old_dist) for ms in pinned_specs):
            msg = "Cannot remove %s because it is pinned. Use --no-pin to override."
            raise CondaRuntimeError(msg % old_dist.to_filename())
        if (
            context.conda_in_root
            and name == "conda"
            and name not in nlinked
            and not context.force
        ):
            if any(s.split(" ", 1)[0] == "conda" for s in specs):
                raise RemoveError("'conda' cannot be removed from the root environment")
            else:
                raise RemoveError(
                    "Error: this 'remove' command cannot be executed because it\n"
                    "would require removing 'conda' dependencies"
                )
        add_unlink(actions, old_dist)

    return actions