# Buggy code
def load_linked_data(prefix, dist, rec=None):
    schannel, dname = dist2pair(dist)
    meta_file = join(prefix, "conda-meta", dname + ".json")
    if rec is None:
        try:
            with open(meta_file) as fi:
                rec = json.load(fi)
        except IOError:
            return None
    else:
        linked_data(prefix)
    url = rec.get("url")
    fn = rec.get("fn")
    if not fn:
        fn = rec["fn"] = url.rsplit("/", 1)[-1] if url else dname + ".tar.bz2"
    if fn[:-8] != dname:
        log.debug("Ignoring invalid package metadata file: %s" % meta_file)
        return None
    channel = rec.get("channel")
    if channel:
        channel = channel.rstrip("/")
        if not url or (url.startswith("file:") and channel[0] != "<unknown>"):
            url = rec["url"] = channel + "/" + fn
    channel, schannel = url_channel(url)
    rec["channel"] = channel
    rec["schannel"] = schannel
    rec["link"] = rec.get("link") or True
    cprefix = "" if schannel == "defaults" else schannel + "::"
    linked_data_[prefix][str(cprefix + dname)] = rec
    return rec


# Traceback (TypeError)
% conda create -p ~/anaclone --clone root
Source:      /Users/ijstokes/anaconda
Destination: /Users/ijstokes/anaclone
The following packages cannot be cloned out of the root environment:
- conda-4.1.1-py27_0
- conda-build-1.20.3-py27_0
Packages: 332
Files: 16134
An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:

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

clude the output of the command 'conda info' in your report.


Traceback (most recent call last):
File "/Users/ijstokes/anaconda/envs/ana40py35/bin/conda", line 6, in <module>
sys.exit(main())
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 120, in main
args_func(args, p)
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/main.py", line 127, in args_func
args.func(args, p)
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/main_create.py", line 57, in execute
install(args, parser, 'create')
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 218, in install
'unknown': args.unknown})
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/cli/install.py", line 87, in clone
fetch_args=fetch_args)
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/misc.py", line 353, in clone_env
force_extract=False, fetch_args=fetch_args)
File "/Users/ijstokes/anaconda/lib/python2.7/site-packages/conda/misc.py", line 56, in explicit
m = url_pat.match(spec)
TypeError: expected string or buffer

# Fixed code
def load_linked_data(prefix, dist, rec=None):
    schannel, dname = dist2pair(dist)
    meta_file = join(prefix, "conda-meta", dname + ".json")
    if rec is None:
        try:
            with open(meta_file) as fi:
                rec = json.load(fi)
        except IOError:
            return None
    else:
        linked_data(prefix)
    url = rec.get("url")
    fn = rec.get("fn")
    if not fn:
        fn = rec["fn"] = url.rsplit("/", 1)[-1] if url else dname + ".tar.bz2"
    if fn[:-8] != dname:
        log.debug("Ignoring invalid package metadata file: %s" % meta_file)
        return None
    channel = rec.get("channel")
    if channel:
        channel = channel.rstrip("/")
        if not url or (url.startswith("file:") and channel[0] != "<unknown>"):
            url = rec["url"] = channel + "/" + fn
    channel, schannel = url_channel(url)
    rec["url"] = url
    rec["channel"] = channel
    rec["schannel"] = schannel
    rec["link"] = rec.get("link") or True
    cprefix = "" if schannel == "defaults" else schannel + "::"
    linked_data_[prefix][str(cprefix + dname)] = rec
    return rec