# Buggy code
def explicit(
    specs, prefix, verbose=False, force_extract=True, fetch_args=None, index=None
):
    actions = defaultdict(list)
    actions["PREFIX"] = prefix
    actions["op_order"] = RM_FETCHED, FETCH, RM_EXTRACTED, EXTRACT, UNLINK, LINK
    linked = {name_dist(dist): dist for dist in install_linked(prefix)}
    fetch_args = fetch_args or {}
    index = index or {}
    verifies = []
    channels = {}
    for spec in specs:
        if spec == "@EXPLICIT":
            continue

        # Format: (url|path)(:#md5)?
        m = url_pat.match(spec)
        if m is None:
            sys.exit("Could not parse explicit URL: %s" % spec)
        url_p, fn, md5 = m.group("url_p"), m.group("fn"), m.group("md5")
        if not is_url(url_p):
            if url_p is None:
                url_p = curdir
            elif not isdir(url_p):
                sys.exit("Error: file not found: %s" % join(url_p, fn))
            url_p = utils_url_path(url_p).rstrip("/")
        url = "{0}/{1}".format(url_p, fn)

        # See if the URL refers to a package in our cache
        prefix = pkg_path = dir_path = None
        if url.startswith("file://"):
            prefix = cached_url(url)

        # If not, determine the channel name from the URL
        if prefix is None:
            _, schannel = url_channel(url)
            prefix = "" if schannel == "defaults" else schannel + "::"
        fn = prefix + fn
        dist = fn[:-8]

        pkg_path = is_fetched(dist)
        dir_path = is_extracted(dist)

        # Don't re-fetch unless there is an MD5 mismatch
        if pkg_path and (md5 and md5_file(pkg_path) != md5):
            # This removes any extracted copies as well
            actions[RM_FETCHED].append(dist)
            pkg_path = dir_path = None

        # Don't re-extract unless forced, or if we can't check the md5
        if dir_path and (force_extract or md5 and not pkg_path):
            actions[RM_EXTRACTED].append(dist)
            dir_path = None

        if not dir_path:
            if not pkg_path:
                _, conflict = find_new_location(dist)
                if conflict:
                    actions[RM_FETCHED].append(conflict)
                if fn not in index or index[fn].get(not_fetched):
                    channels[url_p + "/"] = (schannel, 0)
                actions[FETCH].append(dist)
                verifies.append((dist + ".tar.bz2", md5))
            actions[EXTRACT].append(dist)

        # unlink any installed package with that name
        name = name_dist(dist)
        if name in linked:
            actions[UNLINK].append(linked[name])
        actions[LINK].append(dist)

    # Pull the repodata for channels we are using
    if channels:
        index.update(fetch_index(channels, **fetch_args))

    # Finish the MD5 verification
    for fn, md5 in verifies:
        info = index.get(fn)
        if info is None:
            sys.exit("Error: no package '%s' in index" % fn)
        if md5 and "md5" not in info:
            sys.stderr.write("Warning: cannot lookup MD5 of: %s" % fn)
        if md5 and info["md5"] != md5:
            sys.exit(
                "MD5 mismatch for: %s\n   spec: %s\n   repo: %s"
                % (fn, md5, info["md5"])
            )

    execute_actions(actions, index=index, verbose=verbose)
    return actions


# Traceback (KeyError)
[root@hostname ~]# /opt/wakari/anaconda/bin/conda create -p ~/new-env --clone /opt/wakari/anaconda/
Using Anaconda Cloud api site https://api.anaconda.org
Fetching package metadata: ......
src_prefix: '/opt/wakari/anaconda'
dst_prefix: '/root/new-env'
Packages: 313
Files: 47057
An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:

https://github.com/conda/conda/issues

Include the output of the command 'conda info' in your report.
Traceback (most recent call last):
File "/opt/wakari/anaconda/bin/conda", line 6, in <module>
sys.exit(main())
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 139, in main
args_func(args, p)
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 146, in args_func
args.func(args, p)
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/cli/main_create.py", line 49, in execute
install.install(args, parser, 'create')
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 247, in install
clone(args.clone, prefix, json=args.json, quiet=args.quiet, index=index)
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 84, in clone
quiet=quiet, index=index)
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/misc.py", line 246, in clone_env
sorted_dists = r.dependency_sort(dists)
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/resolve.py", line 729, in dependency_sort
depends = lookup(value)
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/resolve.py", line 724, in lookup
return set(ms.name for ms in self.ms_depends(value + '.tar.bz2'))
File "/opt/wakari/anaconda/lib/python2.7/site-packages/conda/resolve.py", line 573, in ms_depends
deps = [MatchSpec(d) for d in self.index[fn].get('depends', [])]
KeyError: 'r-zoo-1.7_12-r3.2.2_1a.tar.bz2'
[root@hostname ~]#

# Fixed code
def explicit(
    specs, prefix, verbose=False, force_extract=True, fetch_args=None, index=None
):
    actions = defaultdict(list)
    actions["PREFIX"] = prefix
    actions["op_order"] = RM_FETCHED, FETCH, RM_EXTRACTED, EXTRACT, UNLINK, LINK
    linked = {name_dist(dist): dist for dist in install_linked(prefix)}
    fetch_args = fetch_args or {}
    index = index or {}
    verifies = []
    channels = {}
    for spec in specs:
        if spec == "@EXPLICIT":
            continue

        # Format: (url|path)(:#md5)?
        m = url_pat.match(spec)
        if m is None:
            sys.exit("Could not parse explicit URL: %s" % spec)
        url_p, fn, md5 = m.group("url_p"), m.group("fn"), m.group("md5")
        if not is_url(url_p):
            if url_p is None:
                url_p = curdir
            elif not isdir(url_p):
                sys.exit("Error: file not found: %s" % join(url_p, fn))
            url_p = utils_url_path(url_p).rstrip("/")
        url = "{0}/{1}".format(url_p, fn)

        # See if the URL refers to a package in our cache
        prefix = pkg_path = dir_path = None
        if url.startswith("file://"):
            prefix = cached_url(url)

        # If not, determine the channel name from the URL
        if prefix is None:
            _, schannel = url_channel(url)
            prefix = "" if schannel == "defaults" else schannel + "::"
        fn = prefix + fn
        dist = fn[:-8]

        pkg_path = is_fetched(dist)
        dir_path = is_extracted(dist)

        # Don't re-fetch unless there is an MD5 mismatch
        if pkg_path and (md5 and md5_file(pkg_path) != md5):
            # This removes any extracted copies as well
            actions[RM_FETCHED].append(dist)
            pkg_path = dir_path = None

        # Don't re-extract unless forced, or if we can't check the md5
        if dir_path and (force_extract or md5 and not pkg_path):
            actions[RM_EXTRACTED].append(dist)
            dir_path = None

        if not dir_path:
            if not pkg_path:
                _, conflict = find_new_location(dist)
                if conflict:
                    actions[RM_FETCHED].append(conflict)
                if fn not in index or index[fn].get("not_fetched"):
                    channels[url_p + "/"] = (schannel, 0)
                actions[FETCH].append(dist)
                verifies.append((dist + ".tar.bz2", md5))
            actions[EXTRACT].append(dist)

        # unlink any installed package with that name
        name = name_dist(dist)
        if name in linked:
            actions[UNLINK].append(linked[name])
        actions[LINK].append(dist)

    # Pull the repodata for channels we are using
    if channels:
        index.update(fetch_index(channels, **fetch_args))

    # Finish the MD5 verification
    for fn, md5 in verifies:
        info = index.get(fn)
        if info is None:
            sys.exit("Error: no package '%s' in index" % fn)
        if md5 and "md5" not in info:
            sys.stderr.write("Warning: cannot lookup MD5 of: %s" % fn)
        if md5 and info["md5"] != md5:
            sys.exit(
                "MD5 mismatch for: %s\n   spec: %s\n   repo: %s"
                % (fn, md5, info["md5"])
            )

    execute_actions(actions, index=index, verbose=verbose)
    return actions