Repository: spack/spack

Issue:
lmod modules generated for intel packages include LMOD_* environment variables
When I build lmod modules from installed intel packages (including intel, intel-mkl, and intel-mpi) the generated modules include LMOD and related environment variables from the point-in-time environment. This breaks for us because our default lmod environment is different from the environment I'm building the modulefiles for.

### Spack version
```console
$ spack --version
0.14.1-415-cbcac72
```

### Steps to reproduce the issue

```console
$ spack install intel
$ spack module lmod refresh --delete-tree
$ module show intel/19.0.5 2>&1 | grep 'LMOD_'
```

### Error Message

```console
setenv("LMOD_CMD","/curc/sw/lmod/lmod/libexec/lmod")
setenv("LMOD_DIR","/curc/sw/lmod/lmod/libexec")
setenv("LMOD_PKG","/curc/sw/lmod/lmod")
setenv("LMOD_VERSION","6.3.7")
...
```

### Information on your system

1. RHEL7.6

```
modules:
  enable:
  - lmod
  lmod:
    hash_length: 0
    core_compilers:
    - gcc@4.8.5
    hierarchy:
    - mpi
    - lapack
    blacklist:
      - '%gcc'
      - '%intel'
    whitelist:
      - intel@19.0.5
      - gcc@9.3.0
      - intel-mkl
      - intel-mpi
      - openmpi
```

```
packages:
  intel:
    variants: auto_dispatch=AVX,CORE-AVX2,CORE-AVX512,SSE4.2
```

### General information

- [X] I have run `spack --version` and reported the version of Spack
- [X] I have searched the issues of this repo and believe this is not a duplicate


Discussion:
To be clear, this appears to _only_ be happening for intel packages.

```
$ find /curc/sw/modules/spack/spring2020/linux-rhel7-x86_64 -type f -exec grep LMOD_CMD {} +
/curc/sw/modules/spack/spring2020/linux-rhel7-x86_64/gcc/9.3.0/intel-mkl/2019.5.281.lua:setenv("LMOD_CMD", "/curc/sw/lmod/lmod/libexec/lmod")
/curc/sw/modules/spack/spring2020/linux-rhel7-x86_64/intel/19.0.5/intel-mkl/2019.5.281.lua:setenv("LMOD_CMD", "/curc/sw/lmod/lmod/libexec/lmod")
/curc/sw/modules/spack/spring2020/linux-rhel7-x86_64/intel/19.0.5/intel-mpi/2019.6.166.lua:setenv("LMOD_CMD", "/curc/sw/lmod/lmod/libexec/lmod")
/curc/sw/modules/spack/spring2020/linux-rhel7-x86_64/Core/intel/19.0.5.lua:setenv("LMOD_CMD", "/curc/sw/lmod/lmod/libexec/lmod")
```
I have tracked this down to https://github.com/spack/spack/blob/3f190a432e41edc2bc33027b63e7b345898dacb9/lib/spack/spack/build_systems/intel.py#L1018, where sourcing a file upstream appears to produce the noted environment variables. I'll look into that file.

Resolving patch:
diff --git a/lib/spack/spack/util/environment.py b/lib/spack/spack/util/environment.py
--- a/lib/spack/spack/util/environment.py
+++ b/lib/spack/spack/util/environment.py
@@ -597,7 +597,9 @@ def from_sourcing_file(filename, *arguments, **kwargs):
             'SHLVL', '_', 'PWD', 'OLDPWD', 'PS1', 'PS2', 'ENV',
             # Environment modules v4
             'LOADEDMODULES', '_LMFILES_', 'BASH_FUNC_module()', 'MODULEPATH',
-            'MODULES_(.*)', r'(\w*)_mod(quar|share)'
+            'MODULES_(.*)', r'(\w*)_mod(quar|share)',
+            # Lmod configuration
+            r'LMOD_(.*)', 'MODULERCFILE'
         ])
 
         # Compute the environments before and after sourcing


Test patch:
diff --git a/lib/spack/spack/test/data/sourceme_lmod.sh b/lib/spack/spack/test/data/sourceme_lmod.sh
new file mode 100644
--- /dev/null
+++ b/lib/spack/spack/test/data/sourceme_lmod.sh
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+#
+# 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)
+
+export LMOD_VARIABLE=foo
+export LMOD_ANOTHER_VARIABLE=bar
+export NEW_VAR=new
diff --git a/lib/spack/spack/test/environment_modifications.py b/lib/spack/spack/test/environment_modifications.py
--- a/lib/spack/spack/test/environment_modifications.py
+++ b/lib/spack/spack/test/environment_modifications.py
@@ -437,3 +437,14 @@ def test_from_environment_diff(before, after, search_list):
 
     for item in search_list:
         assert item in mod
+
+
+@pytest.mark.regression('15775')
+def test_blacklist_lmod_variables():
+    # Construct the list of environment modifications
+    file = os.path.join(datadir, 'sourceme_lmod.sh')
+    env = EnvironmentModifications.from_sourcing_file(file)
+
+    # Check that variables related to lmod are not in there
+    modifications = env.group_by_name()
+    assert not any(x.startswith('LMOD_') for x in modifications)