# Buggy code
def _read(self):
    """Re-read Database from the data in the set location.

    This does no locking, with one exception: it will automatically
    migrate an index.yaml to an index.json if possible. This requires
    taking a write lock.

    """
    if os.path.isfile(self._index_path):
        # Read from JSON file if a JSON database exists
        self._read_from_file(self._index_path, format="json")

    elif os.path.isfile(self._old_yaml_index_path):
        if os.access(self._db_dir, os.R_OK | os.W_OK):
            # if we can write, then read AND write a JSON file.
            self._read_from_file(self._old_yaml_index_path, format="yaml")
            with WriteTransaction(self.lock, timeout=_db_lock_timeout):
                self._write(None, None, None)
        else:
            # Read chck for a YAML file if we can't find JSON.
            self._read_from_file(self._old_yaml_index_path, format="yaml")

    else:
        # The file doesn't exist, try to traverse the directory.
        # reindex() takes its own write lock, so no lock here.
        self.reindex(spack.store.layout)


# Traceback (KeyError)
amklinv@edison10:~/edison/spack/bin> ./spack -vd install xsdk arch=cray-CNL-ivybridge
==> Reading config file /global/u1/a/amklinv/edison/spack/etc/spack/defaults/packages.yaml
==> Reading config file /global/homes/a/amklinv/.spack/cray/packages.yaml
==> READ LOCK: /global/homes/a/amklinv/.spack/cache/providers/.builtin-index.yaml.lock[0:0] [Acquiring]
==> READ LOCK: /global/homes/a/amklinv/.spack/cache/providers/.builtin-index.yaml.lock[0:0] [Released]
==> '/usr/bin/ibv_devices'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> Reading config file /global/homes/a/amklinv/.spack/cray/compilers.yaml
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x2'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-cray'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-intel'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pgi'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pathscale'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc-cross-aarch64'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-mpich/7.4.1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x1'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-x2'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-cray'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-intel'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pgi'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'PrgEnv-pathscale'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'PrgEnv-gnu'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'unload' 'gcc-cross-aarch64'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'load' 'gcc/6.1.0'
==> '/opt/modules/3.2.10.4/bin/modulecmd' 'python' 'show' 'cray-hdf5-parallel/1.8.12'
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[6653323931864697468:1] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[6653323931864697468:1] [Released]
==> Installing xsdk
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[2794471412757580533:1] [Acquiring]
==> hypre is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/hypre-develop-jwh6ty3izsk6ukytgs2i23qxgo3i4lrq
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[2794471412757580533:1] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[840206737987988608:1] [Acquiring]
==> superlu-dist is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/superlu-dist-develop-c5jaojzxkpmqdyxz7xts4crqshkhaxd4
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[840206737987988608:1] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[4617400482960019937:1] [Acquiring]
==> trilinos is already installed in /global/u1/a/amklinv/edison/spack/opt/spack/cray-CNL-ivybridge/gcc-6.1.0/trilinos-develop-qaujvdhjder4h77x3td7z7jkfi27rcop
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Acquiring]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/lock[0:0] [Released]
==> READ LOCK: /global/u1/a/amklinv/edison/spack/opt/spack/.spack-db/prefix_lock[4617400482960019937:1] [Released]
Traceback (most recent call last):
File "./spack", line 212, in <module>
main(sys.argv)
File "./spack", line 208, in main
_main(args, unknown)
File "./spack", line 174, in _main
return_val = command(parser, args)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/cmd/install.py", line 346, in install
package.do_install(**kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/package.py", line 1185, in do_install
**kwargs
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/package.py", line 1157, in do_install
rec = spack.store.db.get_record(self.spec)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 81, in converter
return function(self, spec_like, *args, **kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 554, in get_record
key = self._get_matching_spec_key(spec, **kwargs)
File "/global/u1/a/amklinv/edison/spack/lib/spack/spack/database.py", line 549, in _get_matching_spec_key
raise KeyError("No such spec in database! %s" % spec)
KeyError: 'No such spec in database! trilinos@develop%gcc@6.1.0+boost~debug+hdf5+hypre+metis~mumps~python+shared~suite-sparse~superlu+superlu-dist+xsdkflags arch=cray-CNL-ivybridge^boost@1.63.0%gcc@6.1.0+atomic+chrono+date_time~debug+filesystem~graph~icu+iostreams+locale+log+math~mpi+multithreaded+program_options~python+random+regex+serialization+shared+signals~singlethreaded+system~taggedlayout+test+thread+timer+wave arch=cray-CNL-ivybridge^bzip2@1.0.6%gcc@6.1.0 arch=cray-CNL-ivybridge^cmake@3.7.1%gcc@6.1.0~doc+ncurses+openssl+ownlibs~qt arch=cray-CNL-ivybridge^glm@0.9.7.1%gcc@6.1.0 arch=cray-CNL-ivybridge^hdf5@1.8.12%gcc@6.1.0+cxx~debug+fortran+mpi+pic+shared~szip~threadsafe arch=cray-CNL-ivybridge^hypre@develop%gcc@6.1.0~internal-superlu+shared arch=cray-CNL-ivybridge^libsigsegv@2.10%gcc@6.1.0 arch=cray-CNL-ivybridge^m4@1.4.17%gcc@6.1.0+sigsegv arch=cray-CNL-ivybridge^matio@1.5.9%gcc@6.1.0+hdf5+zlib arch=cray-CNL-ivybridge^metis@5.1.0%gcc@6.1.0~debug~gdb~idx64~real64+shared arch=cray-CNL-ivybridge^mpich@7.4.1%gcc@6.1.0+hydra+pmi+romio~verbs arch=cray-CNL-ivybridge^ncurses@6.0%gcc@6.1.0 arch=cray-CNL-ivybridge^netcdf@4.4.1%gcc@6.1.0~cdmremote~dap~hdf4 maxdims=1024 maxvars=8192+mpi~parallel-netcdf+shared arch=cray-CNL-ivybridge^openblas@0.2.19%gcc@6.1.0~openmp+pic+shared arch=cray-CNL-ivybridge^openssl@1.0.2j%gcc@6.1.0 arch=cray-CNL-ivybridge^parmetis@4.0.3%gcc@6.1.0~debug~gdb+shared arch=cray-CNL-ivybridge^superlu-dist@develop%gcc@6.1.0 arch=cray-CNL-ivybridge^zlib@1.2.10%gcc@6.1.0+pic arch=cray-CNL-ivybridge'

# Fixed code
def _read(self):
    """Re-read Database from the data in the set location.

    This does no locking, with one exception: it will automatically
    migrate an index.yaml to an index.json if possible. This requires
    taking a write lock.

    """
    if os.path.isfile(self._index_path):
        # Read from JSON file if a JSON database exists
        self._read_from_file(self._index_path, format="json")

    elif os.path.isfile(self._old_yaml_index_path):
        if os.access(self._db_dir, os.R_OK | os.W_OK):
            # if we can write, then read AND write a JSON file.
            self._read_from_file(self._old_yaml_index_path, format="yaml")
            with WriteTransaction(self.lock, timeout=_db_lock_timeout):
                self._write(None, None, None)
        else:
            # Read chck for a YAML file if we can't find JSON.
            self._read_from_file(self._old_yaml_index_path, format="yaml")

    else:
        # The file doesn't exist, try to traverse the directory.
        # reindex() takes its own write lock, so no lock here.
        with WriteTransaction(self.lock, timeout=_db_lock_timeout):
            self._write(None, None, None)
        self.reindex(spack.store.layout)