Repository: spack/spack

Issue:
`spack gc` broken by recent hash/database changes
### Steps to reproduce

It seems that the recent changes to hashing/the database have caused some fallout. @tgamblin
```console
$ spack install libnl
$ spack gc
```

### Error message

```
==> The following packages will be uninstalled:

    -- linux-fedora36-zen2 / gcc@12.1.1 -----------------------------
    u2gyyfy berkeley-db@18.1.40  ioyk45v findutils@4.9.0  ljupqcp libsigsegv@2.13  5ocoz6k pkgconf@1.8.0
    n6k6b3n bison@3.8.2          pbm2akn flex@2.6.3       3jtzoro m4@1.4.19        xzkstqb readline@8.1
    tygbpey bzip2@1.0.8          ruwwhkx gdbm@1.19        iao5k37 ncurses@6.2      dctzm6n zlib@1.2.12
    l732qd2 diffutils@3.8        iiiclwt libiconv@1.16    qp5cnif perl@5.34.1

==> Do you want to proceed? [y/N] y
==> Successfully uninstalled pkgconf@1.8.0%gcc@12.1.1 arch=linux-fedora36-zen2/5ocoz6k
==> Successfully uninstalled findutils@4.9.0%gcc@12.1.1 patches=440b954 arch=linux-fedora36-zen2/ioyk45v
==> Successfully uninstalled diffutils@3.8%gcc@12.1.1 arch=linux-fedora36-zen2/l732qd2
==> Successfully uninstalled perl@5.34.1%gcc@12.1.1+cpanm+shared+threads arch=linux-fedora36-zen2/qp5cnif
==> Successfully uninstalled bison@3.8.2%gcc@12.1.1 arch=linux-fedora36-zen2/n6k6b3n
==> Successfully uninstalled flex@2.6.3%gcc@12.1.1+lex~nls arch=linux-fedora36-zen2/pbm2akn
==> Error: unexpected error [cannot proceed uninstalling specs with remaining link or run dependents libiconv, libsigsegv, berkeley-db, zlib, m4, ncurses, bzip2, readline, gdbm]
```
Sprinkling some debugging messages into the code reveals that, e.g., `m4` cannot be uninstalled because it has the following dependencies:
```
3jtzorojmanr7w44avetshrz6nehx4bu
bison ('build', 'run')--> m4
flex ('build',)--> m4
libnl ('build',)--> m4
```
However, `bison` has just been uninstalled. Looking into the database, an entry for `bison` is indeed still there. `spack reindex` also doesn't remove it:
```
==> Warning: bison@3.8.2%gcc@12.1.1 arch=linux-fedora36-zen2/n6k6b3n is being registered in the database with prefix $SPACK/opt/spack/linux-fedora36-zen2/gcc-12.1.1/bison-3.8.2-n6k6b3ny4z7yc2xnmtycbxt5rfdnpaaf, but this directory does not contain an installation of the spec, due to: Install prefix $SPACK/opt/spack/linux-fedora36-zen2/gcc-12.1.1/bison-3.8.2-n6k6b3ny4z7yc2xnmtycbxt5rfdnpaaf does not exist.
```

### Information on your system

* **Spack:** 0.18.0.dev0 (73b69cfeecddba69ebc8e9e65d4718f22910ab1b)
* **Python:** 3.10.4
* **Platform:** linux-fedora36-zen2
* **Concretizer:** clingo

### General information

- [X] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [X] I have searched the issues of this repo and believe this is not a duplicate
- [X] I have run the failing commands in debug mode and reported the output


Discussion:

Resolving patch:
diff --git a/lib/spack/spack/cmd/uninstall.py b/lib/spack/spack/cmd/uninstall.py
--- a/lib/spack/spack/cmd/uninstall.py
+++ b/lib/spack/spack/cmd/uninstall.py
@@ -235,7 +235,7 @@ def is_ready(dag_hash):
 
         # If this spec is only used as a build dependency, we can uninstall
         return all(
-            dspec.deptypes == ("build",)
+            dspec.deptypes == ("build",) or not dspec.parent.installed
             for dspec in record.spec.edges_from_dependents()
         )
 


Test patch: