From da537d2211f60b8e02f4acd6677b98f82a0cf2ff Mon Sep 17 00:00:00 2001
From: Massimiliano Culpo <massimiliano.culpo@gmail.com>
Date: Thu, 25 Oct 2018 23:56:03 +0200
Subject: [PATCH] Fix autoload of direct dependencies for python (#9630)

fixes #9624

merge_config_rules was using `strict=False` to check if a spec
satisfies a constraint, which loosely translates to "this spec has
no conflict with the constraint, so I can potentially add it to the
spec". We want instead `strict=True` which means "the spec satisfies
the constraint right now".
---
 lib/spack/spack/modules/common.py               |  2 +-
 .../modules/tcl/autoload_with_constraints.yaml  |  8 ++++++++
 lib/spack/spack/test/modules/tcl.py             | 17 +++++++++++++++++
 3 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 lib/spack/spack/test/data/modules/tcl/autoload_with_constraints.yaml

diff --git a/lib/spack/spack/modules/common.py b/lib/spack/spack/modules/common.py
index 1ed826a844..6e0d062156 100644
--- a/lib/spack/spack/modules/common.py
+++ b/lib/spack/spack/modules/common.py
@@ -173,7 +173,7 @@ def merge_config_rules(configuration, spec):
         if constraint.endswith(':'):
             constraint = constraint.strip(':')
             override = True
-        if spec.satisfies(constraint):
+        if spec.satisfies(constraint, strict=True):
             if override:
                 spec_configuration = {}
             update_dictionary_extending_lists(spec_configuration, action)
diff --git a/lib/spack/spack/test/data/modules/tcl/autoload_with_constraints.yaml b/lib/spack/spack/test/data/modules/tcl/autoload_with_constraints.yaml
new file mode 100644
index 0000000000..52796cad5b
--- /dev/null
+++ b/lib/spack/spack/test/data/modules/tcl/autoload_with_constraints.yaml
@@ -0,0 +1,8 @@
+enable:
+  - tcl
+tcl:
+  ^mpich2:
+    autoload: 'direct'
+
+  ^python:
+    autoload: 'direct'
diff --git a/lib/spack/spack/test/modules/tcl.py b/lib/spack/spack/test/modules/tcl.py
index 9846b20482..b7b6a023ce 100644
--- a/lib/spack/spack/test/modules/tcl.py
+++ b/lib/spack/spack/test/modules/tcl.py
@@ -276,3 +276,20 @@ def test_blacklist_implicits(
         for item in callpath_specs:
             writer = writer_cls(item)
             assert writer.conf.blacklisted
+
+    @pytest.mark.regression('9624')
+    @pytest.mark.db
+    def test_autoload_with_constraints(
+            self, modulefile_content, module_configuration, database
+    ):
+        """Tests the automatic loading of direct dependencies."""
+
+        module_configuration('autoload_with_constraints')
+
+        # Test the mpileaks that should have the autoloaded dependencies
+        content = modulefile_content('mpileaks ^mpich2')
+        assert len([x for x in content if 'is-loaded' in x]) == 2
+
+        # Test the mpileaks that should NOT have the autoloaded dependencies
+        content = modulefile_content('mpileaks ^mpich')
+        assert len([x for x in content if 'is-loaded' in x]) == 0
-- 
GitLab