Repository: conda/conda

Issue:
Package Cache FileNotFoundError
    "command": "/home/rodrigo/anaconda3/bin/conda install --yes --json --force-pscheck --prefix /home/rodrigo/anaconda3 glueviz==0.12.0 --dry-run"
    "user_agent": "conda/4.4.3 requests/2.18.4 CPython/3.5.4 Linux/4.4.0-104-generic ubuntu/16.04 glibc/2.23"

```
Traceback (most recent call last):
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 268, in _make_single_record
    repodata_record = read_repodata_json(extracted_package_dir)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/gateways/disk/read.py\", line 123, in read_repodata_json
    with open(join(extracted_package_directory, 'info', 'repodata_record.json')) as fi:
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/common/compat.py\", line 120, in open
    closefd=closefd)
FileNotFoundError: [Errno 2] No such file or directory: '/home/rodrigo/anaconda3/pkgs/r-data.table-1.10.4_1-r342he5fc37b_0/info/repodata_record.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 285, in _make_single_record
    index_json_record = read_index_json(extracted_package_dir)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/gateways/disk/read.py\", line 111, in read_index_json
    with open(join(extracted_package_directory, 'info', 'index.json')) as fi:
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/common/compat.py\", line 120, in open
    closefd=closefd)
FileNotFoundError: [Errno 2] No such file or directory: '/home/rodrigo/anaconda3/pkgs/r-data.table-1.10.4_1-r342he5fc37b_0/info/index.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/exceptions.py\", line 722, in __call__
    return func(*args, **kwargs)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/cli/main.py\", line 78, in _main
    exit_code = do_call(args, p)
  File \"/home/rodrigo/anaconda3/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/rodrigo/anaconda3/lib/python3.5/site-packages/conda/cli/main_install.py\", line 11, in execute
    install(args, parser, 'install')
  File \"/home/rodrigo/anaconda3/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/rodrigo/anaconda3/lib/python3.5/site-packages/conda/cli/install.py\", line 260, in handle_txn
    actions = unlink_link_transaction.make_legacy_action_groups(progressive_fetch_extract)[0]
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/link.py\", line 689, in make_legacy_action_groups
    pfe.prepare()
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/common/io.py\", line 415, in decorated
    return f(*args, **kwds)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 525, in prepare
    for prec in self.link_precs)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 525, in <genexpr>
    for prec in self.link_precs)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 419, in make_actions_for_record
    ), None)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 416, in <genexpr>
    pcrec for pcrec in concat(PackageCache(pkgs_dir).query(pref_or_spec)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 417, in <genexpr>
    for pkgs_dir in context.pkgs_dirs)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 116, in query
    return (pcrec for pcrec in itervalues(self._package_cache_records) if pcrec == param)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 209, in _package_cache_records
    self.load()
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 88, in load
    package_cache_record = self._make_single_record(base_name)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/core/package_cache.py\", line 307, in _make_single_record
    index_json_record = read_index_json(extracted_package_dir)
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/gateways/disk/read.py\", line 111, in read_index_json
    with open(join(extracted_package_directory, 'info', 'index.json')) as fi:
  File \"/home/rodrigo/anaconda3/lib/python3.5/site-packages/conda/common/compat.py\", line 120, in open
    closefd=closefd)
FileNotFoundError: [Errno 2] No such file or directory: '/home/rodrigo/anaconda3/pkgs/r-data.table-1.10.4_1-r342he5fc37b_0/info/index.json'
```


Discussion:

Resolving patch:
diff --git a/conda/core/package_cache.py b/conda/core/package_cache.py
--- a/conda/core/package_cache.py
+++ b/conda/core/package_cache.py
@@ -304,7 +304,14 @@ def _make_single_record(self, package_filename):
                             # to do is remove it and try extracting.
                             rm_rf(extracted_package_dir)
                         extract_tarball(package_tarball_full_path, extracted_package_dir)
-                        index_json_record = read_index_json(extracted_package_dir)
+                        try:
+                            index_json_record = read_index_json(extracted_package_dir)
+                        except (IOError, OSError, JSONDecodeError):
+                            # At this point, we can assume the package tarball is bad.
+                            # Remove everything and move on.
+                            rm_rf(package_tarball_full_path)
+                            rm_rf(extracted_package_dir)
+                            return None
                     else:
                         index_json_record = read_index_json_from_tarball(package_tarball_full_path)
                 except (EOFError, ReadError) as e:


Test patch: