Repository: spack/spack

Issue:
Avoid re-concretizing if the spec is already concrete
This fixes a problem we could see where asking spack to check a buildcache using a hash resulted in re-concretization and subsequent checking of a buildcache with a *different* hash.

E.g.:

```
spack buildcache check --rebuild-on-error --mirror-url https://some.location/mirror -s "/yrnbhxf"
...
==> [2020-11-30-23:29:12.399675] Error: Unable to determine whether spla@1.2.1%gcc@9.3.0+cuda~ipo+openmp~rocm~static build_type=Release arch=linux-ubuntu20.04-x86_64/2b2hcaq needs rebuilding, caught exception attempting to read from https://some.location/mirror/build_cache/linux-ubuntu20.04-x86_64-gcc-9.3.0-spla-1.2.1-2b2hcaqr2khzjvsu27ek767o3fnjdbtm.spec.yaml.
```

Notice how spack actually looked for `/2b2hcaq` rather than `/yrnbhxf`, which was what we asked it to look for.

Indeterministic concretization of spec by hash
### Steps to reproduce the issue

The current concretizer is indeterministic, which causes a problem where `spack spec /hash` gives something else than the spec that was installed (it reconcretizes?)

```console
$ docker run -it --rm stabbles/example-issue
$ spack -e ci spec -l -I /yrnbhxf
Input spec
--------------------------------
[+]  spla@1.2.1%gcc@9.3.0+cuda~ipo+openmp~rocm~static build_type=Release arch=linux-ubuntu20.04-x86_64
[+]      ^cuda@11.1.0%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]      ^mpich@3.3.2%gcc@9.3.0~argobots+fortran+hwloc+hydra+libxml2+pci+romio~slurm~verbs+wrapperrpath device=ch3 netmod=tcp patches=eb982de3366d48cbc55eb5e0df43373a45d9f51df208abf0835a72dc6c0b4774 pmi=pmi arch=linux-ubuntu20.04-x86_64
[+]          ^hwloc@2.2.0%gcc@9.3.0~cairo~cuda~gl~libudev+libxml2~netloc~nvml+pci+shared arch=linux-ubuntu20.04-x86_64
[+]              ^libpciaccess@0.16%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]              ^libxml2@2.9.10%gcc@9.3.0~python arch=linux-ubuntu20.04-x86_64
[+]                  ^libiconv@1.16%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]                  ^xz@5.2.5%gcc@9.3.0~pic arch=linux-ubuntu20.04-x86_64
[+]                  ^zlib@1.2.11%gcc@9.3.0+optimize+pic+shared arch=linux-ubuntu20.04-x86_64
[+]      ^openblas@0.3.12%gcc@9.3.0~consistent_fpcsr~ilp64+pic+shared threads=openmp arch=linux-ubuntu20.04-x86_64

Concretized
--------------------------------
[+]  yrnbhxf  spla@1.2.1%gcc@9.3.0+cuda~ipo+openmp~rocm~static build_type=Release arch=linux-ubuntu20.04-x86_64
[+]  gmy52m7      ^cmake@3.19.1%gcc@9.3.0~doc+ncurses+openssl+ownlibs~qt arch=linux-ubuntu20.04-x86_64
[+]  pzjdrll          ^ncurses@6.2%gcc@9.3.0~symlinks+termlib arch=linux-ubuntu20.04-x86_64
[+]  akuna5k              ^pkgconf@1.7.3%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  2zdbnfk          ^openssl@1.1.1h%gcc@9.3.0+systemcerts arch=linux-ubuntu20.04-x86_64
[+]  mvux77l              ^perl@5.32.0%gcc@9.3.0+cpanm+shared+threads arch=linux-ubuntu20.04-x86_64
[+]  pbx54fp                  ^berkeley-db@18.1.40%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  iowiob4                  ^gdbm@1.18.1%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  do42wzt                      ^readline@8.0%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  p7dmb5p              ^zlib@1.2.11%gcc@9.3.0+optimize+pic+shared arch=linux-ubuntu20.04-x86_64
[+]  yqxaqi3      ^cuda@11.1.0%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  q4tpd5i      ^mpich@3.3.2%gcc@9.3.0~argobots+fortran+hwloc+hydra+libxml2+pci+romio~slurm~verbs+wrapperrpath device=ch3 netmod=tcp patches=eb982de3366d48cbc55eb5e0df43373a45d9f51df208abf0835a72dc6c0b4774 pmi=pmi arch=linux-ubuntu20.04-x86_64
[+]  upn7gfp          ^findutils@4.6.0%gcc@9.3.0 patches=84b916c0bf8c51b7e7b28417692f0ad3e7030d1f3c248ba77c42ede5c1c5d11e,bd9e4e5cc280f9753ae14956c4e4aa17fe7a210f55dd6c84aa60b12d106d47a2 arch=linux-ubuntu20.04-x86_64
[+]  sxa5vpx              ^autoconf@2.69%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  kpggc3j                  ^m4@1.4.18%gcc@9.3.0+sigsegv patches=3877ab548f88597ab2327a2230ee048d2d07ace1062efe81fc92e91b7f39cd00,fc9b61654a3ba1a8d6cd78ce087e7c96366c290bc8d2c299f09828d793b853c8 arch=linux-ubuntu20.04-x86_64
[+]  lkczm6t                      ^libsigsegv@2.12%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  rdeo35u              ^automake@1.16.2%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
 -   siarxpq              ^libtool@2.4.2%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
 -   2mkwa6s                  ^help2man@1.47.11%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  yt6n2ph                      ^gettext@0.21%gcc@9.3.0+bzip2+curses+git~libunistring+libxml2+tar+xz arch=linux-ubuntu20.04-x86_64
[+]  5m22qwv                          ^bzip2@1.0.8%gcc@9.3.0+shared arch=linux-ubuntu20.04-x86_64
[+]  2eqr3yc                              ^diffutils@3.7%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  w6zptbc                                  ^libiconv@1.16%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  a5l2lj7                          ^libxml2@2.9.10%gcc@9.3.0~python arch=linux-ubuntu20.04-x86_64
[+]  kamfyq4                              ^xz@5.2.5%gcc@9.3.0~pic arch=linux-ubuntu20.04-x86_64
[+]  wqwrumn                          ^tar@1.32%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  zntphih              ^texinfo@6.5%gcc@9.3.0 patches=12f6edb0c6b270b8c8dba2ce17998c580db01182d871ee32b7b6e4129bd1d23a,1732115f651cff98989cb0215d8f64da5e0f7911ebf0c13b064920f088f2ffe1 arch=linux-ubuntu20.04-x86_64
[+]  nvxxkza          ^hwloc@2.2.0%gcc@9.3.0~cairo~cuda~gl~libudev+libxml2~netloc~nvml+pci+shared arch=linux-ubuntu20.04-x86_64
[+]  ux5xkhg              ^libpciaccess@0.16%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  rustdy6                  ^util-macros@1.19.1%gcc@9.3.0 arch=linux-ubuntu20.04-x86_64
[+]  ynvweut      ^openblas@0.3.12%gcc@9.3.0~consistent_fpcsr~ilp64+pic+shared threads=openmp arch=linux-ubuntu20.04-x86_64
```

note how there's suddenly two uninstalled specs.

### Information on your system

* **Spack:** 0.16.0
* **Python:** 3.8.6
* **Platform:** linux-ubuntu20.04-zen2
* **Concretizer:** clingo




Discussion:
@haampie For me, this fixed the issue we were digging into this evening.  Hopefully it doesn't cause a bunch of test failures 😉 
Looks good, but needs a test
Which issue is this PR solving? Is that with the new concretizer or the original one?
https://github.com/spack/spack/issues/20191, I used to do `buildcache check -s /hash` which now reconcretizes to something different, so I have provided the correct hash, but it rehashes to something else during the check.
Maybe related is the weird concretization of mpich: https://github.com/spack/spack/pull/20176

Resolving patch:
diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py
--- a/lib/spack/spack/spec.py
+++ b/lib/spack/spack/spec.py
@@ -2442,6 +2442,9 @@ def _new_concretize(self, tests=False):
             raise spack.error.SpecError(
                 "Spec has no name; cannot concretize an anonymous spec")
 
+        if self._concrete:
+            return
+
         result = spack.solver.asp.solve([self], tests=tests)
         if not result.satisfiable:
             result.print_cores()


Test patch: