Repository: spack/spack

Issue:
packages.yaml can't set version for go (problem with 2 vs 3 digit version numbers?)
I have an `etc/spack/packages.yaml` looks like this (lines elided):

```yaml
packages:
  all:
    compiler: [gcc@5.4.0, gcc@4.8.5, gcc@4.8.3]
  ack:
    version: [2.16]
[...]
  emacs:
    version: [25.1]
    variants: +X
[...]
  go:
    version: [1.8]
    # tests have two false positives, user_namespaces and threads/exceptions
    variants: ~test
  hdf5:
    version: [1.10.0-patch1]
[...]
```

As a control, I can `spack spec emacs` and it reports:

```
Concretized
--------------------------------
emacs@25.1%gcc@5.4.0+X toolkit=gtk arch=linux-centos7-x86_64
```

If I change the emacs version to `25.2`, then I get 

```
Concretized
--------------------------------
emacs@25.2%gcc@5.4.0+X toolkit=gtk arch=linux-centos7-x86_64
```

so I'm using the right file, and etc...

In spite of the constraint to version 1.8 in the yaml file, `spack spec go` sez:

```
Concretized
--------------------------------
go@1.8.1%gcc@5.4.0 arch=linux-centos7-x86_64
```

`spack spec go@1.8` sez:

```
Concretized
--------------------------------
go@1.8%gcc@5.4.0 arch=linux-centos7-x86_64
```

so the version seems to exist, and etc...

If I change the go constraint in the yaml file to `1.7.5`, I get:

```
Concretized
--------------------------------
go@1.7.5%gcc@5.4.0 arch=linux-centos7-x86_64
```

Which kind of leaves my marbles rolling around on the floor.




Discussion:
Spack gets confused when prioritizing versions for go in this case because of the mix of X.Y and X.Y.Z version formats (e.g. this in part occurs because all instances of 1.8.x satisfy 1.8). I'm currently looking into a solution.

Resolving patch:
diff --git a/lib/spack/spack/package_prefs.py b/lib/spack/spack/package_prefs.py
--- a/lib/spack/spack/package_prefs.py
+++ b/lib/spack/spack/package_prefs.py
@@ -112,9 +112,17 @@ def __call__(self, spec):
 
         # integer is the index of the first spec in order that satisfies
         # spec, or it's a number larger than any position in the order.
-        return next(
+        match_index = next(
             (i for i, s in enumerate(spec_order) if spec.satisfies(s)),
             len(spec_order))
+        if match_index < len(spec_order) and spec_order[match_index] == spec:
+            # If this is called with multiple specs that all satisfy the same
+            # minimum index in spec_order, the one which matches that element
+            # of spec_order exactly is considered slightly better. Note
+            # that because this decreases the value by less than 1, it is not
+            # better than a match which occurs at an earlier index.
+            match_index -= 0.5
+        return match_index
 
     @classproperty
     @classmethod
diff --git a/var/spack/repos/builtin.mock/packages/mixedversions/package.py b/var/spack/repos/builtin.mock/packages/mixedversions/package.py
new file mode 100644
--- /dev/null
+++ b/var/spack/repos/builtin.mock/packages/mixedversions/package.py
@@ -0,0 +1,36 @@
+##############################################################################
+# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
+# Produced at the Lawrence Livermore National Laboratory.
+#
+# This file is part of Spack.
+# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
+# LLNL-CODE-647188
+#
+# For details, see https://github.com/llnl/spack
+# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License (as
+# published by the Free Software Foundation) version 2.1, February 1999.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
+# conditions of the GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##############################################################################
+from spack import *
+
+
+class Mixedversions(Package):
+    url = "http://www.fake-mixedversions.org/downloads/mixedversions-1.0.tar.gz"
+
+    version('2.0.1', 'hashc')
+    version('2.0', 'hashb')
+    version('1.0.1', 'hasha')
+
+    def install(self, spec, prefix):
+        pass


Test patch:
diff --git a/lib/spack/spack/test/concretize_preferences.py b/lib/spack/spack/test/concretize_preferences.py
--- a/lib/spack/spack/test/concretize_preferences.py
+++ b/lib/spack/spack/test/concretize_preferences.py
@@ -102,6 +102,11 @@ def test_preferred_versions(self):
         spec = concretize('mpileaks')
         assert spec.version == spack.spec.Version('2.2')
 
+    def test_preferred_versions_mixed_version_types(self):
+        update_packages('mixedversions', 'version', ['2.0'])
+        spec = concretize('mixedversions')
+        assert spec.version == spack.spec.Version('2.0')
+
     def test_preferred_providers(self):
         """Test preferred providers of virtual packages are
         applied correctly