Repository: spack/spack

Issue:
TCL Modulefiles not loadable by default anymore
@becker33 Recently, I have not been able to use the spack-generated tcl modulefiles. I believe this is due to the recent changes in https://github.com/spack/spack/pull/23703. It looks like the default filepath was flipped? Was that intented? Defaults [before](https://github.com/spack/spack/blob/9b99f85abf208612fe8ff7d108b6a68d970706d8/etc/spack/defaults/config.yaml#L37-L40) and [after](https://github.com/spack/spack/blob/c6d21fa154049241435aec0e5dcf929cfd8d4cc7/lib/spack/spack/modules/common.py#L223-L226).

### Steps to reproduce the issue

```console
$ spack module tcl refresh -y
$ module avail
<none of the spack-generated modulefiles appear>
```

### Error Message

<!-- If Spack reported an error, provide the error message. If it did not report an error but the output appears incorrect, provide the incorrect output. If there was no error message and no output but the result is incorrect, describe how it does not match what you expect. -->

### Information on your system

<!-- Please include the output of `spack debug report` -->

```console
$ spack debug report
* **Spack:** 0.16.2-2942-4a7b0af
* **Python:** 3.6.8
* **Platform:** linux-rhel7-power9le
* **Concretizer:** original
```

<!-- If you have any relevant configuration detail (custom `packages.yaml` or `modules.yaml`, etc.) you can add that here as well. -->

### Additional information

<!-- 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
- [ ] 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:
What does your `MODULEPATH` look like? I noticed a bug in our `setup-env.sh`, but that shouldn't be new (#24104).
@adamjstewart For me nothing is added to `MODULEPATH` after sourcing `setup-env.sh`:

```console
$ which spack
/usr/bin/which: no spack in (/projects/opt/x86_64/git/2.17.1/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/ibutils/bin:/home/stam/.local/bin:/home/stam/bin)
$ echo $MODULEPATH
/usr/share/Modules/modulefiles:/etc/modulefiles:/opt/modulefiles:/usr/local/modulefiles:/projects/darwin/modulefiles/centos7:/projects/darwin/modulefiles/oneAPI/centos7/x86_64
$ source share/spack/setup-env.sh
$ which spack
~/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/bin/spack
$ echo $MODULEPATH
/usr/share/Modules/modulefiles:/etc/modulefiles:/opt/modulefiles:/usr/local/modulefiles:/projects/darwin/modulefiles/centos7:/projects/darwin/modulefiles/oneAPI/centos7/x86_64
```

Also, I tried flipping the defaults mentioned in the description, and that does install the tcl modulefiles to the `$SPACK_ROOT/share/spack/modules` directory as it was before. But this didn't affect anything with `MODULEPATH`.
What does your `modules.yaml` look like?
```console
$ spack config blame modules
---                                                                                                                   modules:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/linux/modules.yaml:17    default:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/linux/modules.yaml:18      prefix_inspections:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/linux/modules.yaml:19        lib:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/linux/modules.yaml:20        - LD_LIBRARY_PATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/linux/modules.yaml:21        lib64:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/linux/modules.yaml:22        - LD_LIBRARY_PATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:46            enable:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:46            - tcl
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:47            # Default configurations if lmod is enabled
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:50            lmod:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:51              hierarchy:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:52              - mpi
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:18          prefix_inspections:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:19            bin:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:20            - PATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:21            man:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:22            - MANPATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:23            share/man:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:24            - MANPATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:25            share/aclocal:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:26            - ACLOCAL_PATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:27            lib/pkgconfig:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:28            - PKG_CONFIG_PATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:29            lib64/pkgconfig:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:30            - PKG_CONFIG_PATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:31            share/pkgconfig:
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:32            - PKG_CONFIG_PATH
                                                                                                                          ? ''
                                                                                                                          : - CMAKE_PREFIX_PATH
/home/stam/projects/pilotlight/tests/spack/tmp_darwin_5.9.1_xl_r_16.1/spack/etc/spack/defaults/modules.yaml:34          # These are configurations for the module set named "default"
```
Hmm, I'm not seeing anything obvious. @becker33 or @alalazo should take a look.

Resolving patch:
diff --git a/lib/spack/spack/main.py b/lib/spack/spack/main.py
--- a/lib/spack/spack/main.py
+++ b/lib/spack/spack/main.py
@@ -32,6 +32,7 @@
 import spack.config
 import spack.cmd
 import spack.environment as ev
+import spack.modules
 import spack.paths
 import spack.repo
 import spack.store
@@ -647,14 +648,8 @@ def shell_set(var, value):
         'tcl': list(),
         'lmod': list()
     }
-    module_roots = spack.config.get('modules:default:roots', {})
-    module_roots = spack.config.merge_yaml(
-        module_roots, spack.config.get('config:module_roots', {}))
-    module_roots = dict(
-        (k, v) for k, v in module_roots.items() if k in module_to_roots
-    )
-    for name, path in module_roots.items():
-        path = spack.util.path.canonicalize_path(path)
+    for name in module_to_roots.keys():
+        path = spack.modules.common.root_path(name, 'default')
         module_to_roots[name].append(path)
 
     other_spack_instances = spack.config.get(
diff --git a/lib/spack/spack/modules/common.py b/lib/spack/spack/modules/common.py
--- a/lib/spack/spack/modules/common.py
+++ b/lib/spack/spack/modules/common.py
@@ -221,8 +221,8 @@ def root_path(name, module_set_name):
         root folder for module file installation
     """
     defaults = {
-        'lmod': '$spack/share/spack/modules',
-        'tcl': '$spack/share/spack/lmod',
+        'lmod': '$spack/share/spack/lmod',
+        'tcl': '$spack/share/spack/modules',
     }
     # Root folders where the various module files should be written
     roots = spack.config.get('modules:%s:roots' % module_set_name, {})


Test patch: