# Buggy code
def clone_env(prefix1, prefix2, verbose=True, quiet=False, fetch_args=None):
    """
    clone existing prefix1 into new prefix2
    """
    untracked_files = untracked(prefix1)

    # Discard conda and any package that depends on it
    drecs = linked_data(prefix1)
    filter = {}
    found = True
    while found:
        found = False
        for dist, info in iteritems(drecs):
            name = info["name"]
            if name in filter:
                continue
            if name == "conda":
                filter["conda"] = dist
                found = True
                break
            for dep in info.get("depends", []):
                if MatchSpec(dep).name in filter:
                    filter[name] = dist
                    found = True
    if filter:
        if not quiet:
            print(
                "The following packages cannot be cloned out of the root environment:"
            )
            for pkg in itervalues(filter):
                print(" - " + pkg)
            drecs = {
                dist: info
                for dist, info in iteritems(drecs)
                if info["name"] not in filter
            }

    # Resolve URLs for packages that do not have URLs
    r = None
    index = {}
    unknowns = [dist for dist, info in iteritems(drecs) if "url" not in info]
    notfound = []
    if unknowns:
        fetch_args = fetch_args or {}
        index = get_index(**fetch_args)
        r = Resolve(index, sort=True)
        for dist in unknowns:
            name = name_dist(dist)
            fn = dist2filename(dist)
            fkeys = [d for d in r.index.keys() if r.index[d]["fn"] == fn]
            if fkeys:
                del drecs[dist]
                dist = sorted(fkeys, key=r.version_key, reverse=True)[0]
                drecs[dist] = r.index[dist]
            else:
                notfound.append(fn)
    if notfound:
        what = "Package%s " % ("" if len(notfound) == 1 else "s")
        notfound = "\n".join(" - " + fn for fn in notfound)
        msg = "%s missing in current %s channels:%s" % (what, subdir, notfound)
        raise RuntimeError(msg)

    # Assemble the URL and channel list
    urls = {}
    resolver = None
    for dist, info in iteritems(drecs):
        fkey = dist + ".tar.bz2"
        if fkey not in index:
            info["not_fetched"] = True
            index[fkey] = info
            r = None
        urls[dist] = info["url"]

    if r is None:
        r = Resolve(index)
    dists = r.dependency_sort(urls.keys())
    urls = [urls[d] for d in dists]

    if verbose:
        print("Packages: %d" % len(dists))
        print("Files: %d" % len(untracked_files))

    for f in untracked_files:
        src = join(prefix1, f)
        dst = join(prefix2, f)
        dst_dir = dirname(dst)
        if islink(dst_dir) or isfile(dst_dir):
            os.unlink(dst_dir)
        if not isdir(dst_dir):
            os.makedirs(dst_dir)
        if islink(src):
            os.symlink(os.readlink(src), dst)
            continue

        try:
            with open(src, "rb") as fi:
                data = fi.read()
        except IOError:
            continue

        try:
            s = data.decode("utf-8")
            s = s.replace(prefix1, prefix2)
            data = s.encode("utf-8")
        except UnicodeDecodeError:  # data is binary
            pass

        with open(dst, "wb") as fo:
            fo.write(data)
        shutil.copystat(src, dst)

    actions = explicit(
        urls,
        prefix2,
        verbose=not quiet,
        index=index,
        force_extract=False,
        fetch_args=fetch_args,
    )
    return actions, untracked_files


# 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 clone_env(prefix1, prefix2, verbose=True, quiet=False, fetch_args=None):
    """
    clone existing prefix1 into new prefix2
    """
    untracked_files = untracked(prefix1)

    # Discard conda and any package that depends on it
    drecs = linked_data(prefix1)
    filter = {}
    found = True
    while found:
        found = False
        for dist, info in iteritems(drecs):
            name = info["name"]
            if name in filter:
                continue
            if name == "conda":
                filter["conda"] = dist
                found = True
                break
            for dep in info.get("depends", []):
                if MatchSpec(dep).name in filter:
                    filter[name] = dist
                    found = True
    if filter:
        if not quiet:
            print(
                "The following packages cannot be cloned out of the root environment:"
            )
            for pkg in itervalues(filter):
                print(" - " + pkg)
            drecs = {
                dist: info
                for dist, info in iteritems(drecs)
                if info["name"] not in filter
            }

    # Resolve URLs for packages that do not have URLs
    r = None
    index = {}
    unknowns = [dist for dist, info in iteritems(drecs) if "url" not in info]
    notfound = []
    if unknowns:
        fetch_args = fetch_args or {}
        index = get_index(**fetch_args)
        r = Resolve(index, sort=True)
        for dist in unknowns:
            name = name_dist(dist)
            fn = dist2filename(dist)
            fkeys = [d for d in r.index.keys() if r.index[d]["fn"] == fn]
            if fkeys:
                del drecs[dist]
                dist = sorted(fkeys, key=r.version_key, reverse=True)[0]
                drecs[dist] = r.index[dist]
            else:
                notfound.append(fn)
    if notfound:
        what = "Package%s " % ("" if len(notfound) == 1 else "s")
        notfound = "\n".join(" - " + fn for fn in notfound)
        msg = "%s missing in current %s channels:%s" % (what, subdir, notfound)
        raise RuntimeError(msg)

    # Assemble the URL and channel list
    urls = {}
    for dist, info in iteritems(drecs):
        fkey = dist + ".tar.bz2"
        if fkey not in index:
            info["not_fetched"] = True
            index[fkey] = info
            r = None
        urls[dist] = info["url"]

    if r is None:
        r = Resolve(index)
    dists = r.dependency_sort(urls.keys())
    urls = [urls[d] for d in dists]

    if verbose:
        print("Packages: %d" % len(dists))
        print("Files: %d" % len(untracked_files))

    for f in untracked_files:
        src = join(prefix1, f)
        dst = join(prefix2, f)
        dst_dir = dirname(dst)
        if islink(dst_dir) or isfile(dst_dir):
            os.unlink(dst_dir)
        if not isdir(dst_dir):
            os.makedirs(dst_dir)
        if islink(src):
            os.symlink(os.readlink(src), dst)
            continue

        try:
            with open(src, "rb") as fi:
                data = fi.read()
        except IOError:
            continue

        try:
            s = data.decode("utf-8")
            s = s.replace(prefix1, prefix2)
            data = s.encode("utf-8")
        except UnicodeDecodeError:  # data is binary
            pass

        with open(dst, "wb") as fo:
            fo.write(data)
        shutil.copystat(src, dst)

    actions = explicit(
        urls,
        prefix2,
        verbose=not quiet,
        index=index,
        force_extract=False,
        fetch_args=fetch_args,
    )
    return actions, untracked_files