Repository: spack/spack

Issue:
With Clingo arbitrary external package versions are not allowed 
<!-- Explain, in a clear and concise way, the command you ran and the result you were trying to achieve.
Example: "I ran `spack find` to list all the installed packages and ..." -->

### Steps to reproduce the issue

Try and solve / concretise an external package that previously worked with the old concretiser.

```console


$ spack solve slurm
==> The following constraints are unsatisfiable:
  external_only("slurm")
  root("slurm")
  version_declared("slurm","19-05-6-1",2)
  version_satisfies("slurm","19.05.6") :- external_spec("slurm",0).


```

With the following external definition:

```
  slurm:
    buildable: false
    externals:
    - spec: slurm@19.05.6
      prefix: /usr
```

### Error Message

```console
$ spack -d spec slurm
==> [2020-12-04-14:40:10.678728] Imported spec from built-in commands
==> [2020-12-04-14:40:10.679839] Imported spec from built-in commands
Input spec
--------------------------------
slurm

Concretized
--------------------------------
==> [2020-12-04-14:40:10.682664] Reading config file /dcsrsoft/spack/hetre/v0.2/spack/etc/spack/packages.yaml
==> [2020-12-04-14:40:10.714131] Reading config file /dcsrsoft/spack/hetre/v0.2/spack/etc/spack/compilers.yaml
==> [2020-12-04-14:40:10.727205] Reading config file /dcsrsoft/spack/hetre/v0.2/spack/var/spack/environments/S6g1-IB-test1/spack.yaml
==> [2020-12-04-14:40:10.839162] Reading config file /dcsrsoft/spack/hetre/v0.2/spack/etc/spack/defaults/repos.yaml
==> [2020-12-04-14:40:10.841616] Reading config file /dcsrsoft/spack/hetre/v0.2/spack/etc/spack/repos.yaml
==> [2020-12-04-14:40:10.842923] Reading config file /users/dcsrsoft/.spack/linux/repos.yaml
==> [2020-12-04-14:40:13.190121] Reading config file /dcsrsoft/spack/hetre/v0.2/spack/etc/spack/compilers.yaml
==> [2020-12-04-14:40:13.249151] Reading config file /dcsrsoft/spack/hetre/v0.2/spack/etc/spack/defaults/packages.yaml
==> [2020-12-04-14:40:13.272832] Reading config file /dcsrsoft/spack/hetre/v0.2/spack/etc/spack/packages.yaml
==> [2020-12-04-14:40:22.598398] The following constraints are unsatisfiable:
  external_only("slurm")
  root("slurm")
  version_declared("slurm","19-05-6-1",1)
  version_satisfies("slurm","19.05.6") :- external_spec("slurm",0).
==> [2020-12-04-14:40:22.603311] UnsatisfiableSpecError: slurm does not satisfy unknown
==> [2020-12-04-14:40:22.605218] Error: slurm does not satisfy unknown
Traceback (most recent call last):
  File "/dcsrsoft/spack/hetre/v0.2/spack/lib/spack/spack/main.py", line 762, in main
    return _invoke_command(command, parser, args, unknown)
  File "/dcsrsoft/spack/hetre/v0.2/spack/lib/spack/spack/main.py", line 490, in _invoke_command
    return_val = command(parser, args)
  File "/dcsrsoft/spack/hetre/v0.2/spack/lib/spack/spack/cmd/spec.py", line 102, in spec
    spec.concretize()
  File "/dcsrsoft/spack/hetre/v0.2/spack/lib/spack/spack/spec.py", line 2469, in concretize
    self._new_concretize(tests)
  File "/dcsrsoft/spack/hetre/v0.2/spack/lib/spack/spack/spec.py", line 2448, in _new_concretize
    raise spack.error.UnsatisfiableSpecError(
spack.error.UnsatisfiableSpecError: slurm does not satisfy unknown
```



### Information on your system

```
$ spack --version
0.16.0-163-98517af
```

<!-- Please include the output of `spack debug report` -->

<!-- If you have any relevant configuration detail (custom `packages.yaml` or `modules.yaml`, etc.) you can add that here as well. -->

### Additional information

As suggested by @alalazo changing the declared version to match one in package.py resolves the immediate issue.

```
  slurm:
    buildable: false
    externals:
    - spec: slurm@19-05-6-1
      prefix: /usr
```

<!-- These boxes can be checked by replacing [ ] with [x] or by clicking them after submitting the issue. -->
- [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

<!-- We encourage you to try, as much as possible, to reduce your problem to the minimal example that still reproduces the issue. That would help us a lot in fixing it quickly and effectively!

If you want to ask a question about the tool (how to use it, what it can currently do, etc.), try the `#general` channel on our Slack first. We have a welcoming community and chances are you'll get your reply faster and without opening an issue.

Other than that, thanks for taking the time to contribute to Spack! -->



Discussion:
This must be a weird interaction of our solver setup and the version semantics. A configuration file like:
```yaml
packages:
  slurm:
    buildable: false
    externals:
    - spec: slurm@foo
      prefix: /usr
```
works perfectly, but I can reproduce the issue with the `packages.yaml` posted above. In the logic program I could find:
```
version_satisfies("slurm", "19.05.6") :- 1 { version("slurm", "19-05-6-1") } 1.
1 { version("slurm", "19-05-6-1") } 1 :- version_satisfies("slurm", "19.05.6").
```
which is clearly wrong. I think `19.05.6` has been considered not a fixed version, but a range.

Resolving patch:
diff --git a/lib/spack/spack/solver/asp.py b/lib/spack/spack/solver/asp.py
--- a/lib/spack/spack/solver/asp.py
+++ b/lib/spack/spack/solver/asp.py
@@ -1060,6 +1060,7 @@ def external_packages(self):
             for id, spec in enumerate(external_specs):
                 self.gen.newline()
                 spec_id = fn.external_spec(pkg_name, id)
+                self.possible_versions[spec.name].add(spec.version)
                 clauses = self.spec_clauses(spec, body=True)
                 # This is an iff below, wish it could be written in a
                 # more compact form
diff --git a/var/spack/repos/builtin.mock/packages/external-buildable-with-variant/package.py b/var/spack/repos/builtin.mock/packages/external-buildable-with-variant/package.py
new file mode 100644
--- /dev/null
+++ b/var/spack/repos/builtin.mock/packages/external-buildable-with-variant/package.py
@@ -0,0 +1,15 @@
+# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
+# Spack Project Developers. See the top-level COPYRIGHT file for details.
+#
+# SPDX-License-Identifier: (Apache-2.0 OR MIT)
+
+from spack import *
+
+
+class ExternalBuildableWithVariant(Package):
+    homepage = "http://somewhere.com"
+    url = "http://somewhere.com/module-1.0.tar.gz"
+
+    version('1.0', '1234567890abcdef1234567890abcdef')
+
+    variant('baz', default=False, description='nope')
diff --git a/var/spack/repos/builtin.mock/packages/externaltool/package.py b/var/spack/repos/builtin.mock/packages/externaltool/package.py
--- a/var/spack/repos/builtin.mock/packages/externaltool/package.py
+++ b/var/spack/repos/builtin.mock/packages/externaltool/package.py
@@ -12,5 +12,6 @@ class Externaltool(Package):
 
     version('1.0', '1234567890abcdef1234567890abcdef')
     version('0.9', '1234567890abcdef1234567890abcdef')
+    version('0.8.1', '1234567890abcdef1234567890abcdef')
 
     depends_on('externalprereq')


Test patch:
diff --git a/lib/spack/spack/test/concretize.py b/lib/spack/spack/test/concretize.py
--- a/lib/spack/spack/test/concretize.py
+++ b/lib/spack/spack/test/concretize.py
@@ -979,3 +979,21 @@ def test_all_patches_applied(self):
     def test_dont_select_version_that_brings_more_variants_in(self):
         s = Spec('dep-with-variants-if-develop-root').concretized()
         assert s['dep-with-variants-if-develop'].satisfies('@1.0')
+
+    @pytest.mark.regression('20244')
+    @pytest.mark.parametrize('spec_str,is_external,expected', [
+        # These are all externals, and 0_8 is a version not in package.py
+        ('externaltool@1.0', True, '@1.0'),
+        ('externaltool@0.9', True, '@0.9'),
+        ('externaltool@0_8', True, '@0_8'),
+        # This external package is buildable, has a custom version
+        # in packages.yaml that is greater than the ones in package.py
+        # and specifies a variant
+        ('external-buildable-with-variant +baz', True, '@1.1.special +baz'),
+        ('external-buildable-with-variant ~baz', False, '@1.0 ~baz'),
+        ('external-buildable-with-variant@1.0: ~baz', False, '@1.0 ~baz'),
+    ])
+    def test_external_package_versions(self, spec_str, is_external, expected):
+        s = Spec(spec_str).concretized()
+        assert s.external == is_external
+        assert s.satisfies(expected)
diff --git a/lib/spack/spack/test/data/config/packages.yaml b/lib/spack/spack/test/data/config/packages.yaml
--- a/lib/spack/spack/test/data/config/packages.yaml
+++ b/lib/spack/spack/test/data/config/packages.yaml
@@ -9,6 +9,8 @@ packages:
       prefix: /path/to/external_tool
     - spec: externaltool@0.9%gcc@4.5.0
       prefix: /usr
+    - spec: externaltool@0_8%gcc@4.5.0
+      prefix: /usr
   externalvirtual:
     buildable: False
     externals:
@@ -27,3 +29,8 @@ packages:
     externals:
     - spec:  requires-virtual@2.0
       prefix: /usr
+  'external-buildable-with-variant':
+    buildable: True
+    externals:
+      - spec: external-buildable-with-variant@1.1.special +baz
+        prefix: /usr