We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
stderr: ERROR: Unable to locate a modulefile for 'PrgEnv-pgi' 
I am installing CrayCCE on the a Cray XC40 and am recieving the error.

```
stderr: ERROR: Unable to locate a modulefile for 'PrgEnv-pgi'\n)") 
```

Looking at the LOG I see...

```
module unload PrgEnv-gnu
module unload PrgEnv-intel
module unload PrgEnv-pgi

; stderr: ERROR: Unable to locate a modulefile for 'PrgEnv-pgi' 
```

In our system we do not even have `PrgEnv-pgi` so it is natural that we get this error, but we are not using pgi either so
it should be checked if such a module exists prior of unloading it.
Also `PrgEnv-pgi` is not listed as a dependency in the config file.

We are using module.tcl Modules Release 4.1.3.1 (2018-11-29)
and I know that EasyBuild prefers Lmod, but still module.tcl is supported.

Cheers
Henric

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `easybuild/easyblocks/generic/craytoolchain.py`
Content:
```
1 ##
2 # Copyright 2015-2020 Ghent University
3 #
4 # This file is part of EasyBuild,
5 # originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
6 # with support of Ghent University (http://ugent.be/hpc),
7 # the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
8 # Flemish Research Foundation (FWO) (http://www.fwo.be/en)
9 # and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
10 #
11 # https://github.com/easybuilders/easybuild
12 #
13 # EasyBuild is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation v2.
16 #
17 # EasyBuild is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with EasyBuild.  If not, see <http://www.gnu.org/licenses/>.
24 ##
25 """
26 EasyBuild support for installing Cray toolchains, implemented as an easyblock
27 
28 @author: Kenneth Hoste (Ghent University)
29 @author: Guilherme Peretti Pezzi (CSCS)
30 @author: Petar Forai (IMP/IMBA)
31 """
32 
33 from easybuild.easyblocks.generic.bundle import Bundle
34 from easybuild.tools.build_log import EasyBuildError
35 
36 
37 KNOWN_PRGENVS = ['PrgEnv-cray', 'PrgEnv-gnu', 'PrgEnv-intel', 'PrgEnv-pgi']
38 
39 
40 class CrayToolchain(Bundle):
41     """
42     Compiler toolchain: generate module file only, nothing to build/install
43     """
44 
45     def prepare_step(self, *args, **kwargs):
46         """Prepare build environment (skip loaded of dependencies)."""
47 
48         kwargs['load_tc_deps_modules'] = False
49 
50         super(CrayToolchain, self).prepare_step(*args, **kwargs)
51 
52     def make_module_dep(self):
53         """
54         Generate load/swap statements for dependencies in the module file
55         """
56         prgenv_mod = None
57 
58         # collect 'swap' statement for dependencies (except PrgEnv)
59         swap_deps = []
60         for dep in self.toolchain.dependencies:
61             mod_name = dep['full_mod_name']
62             # determine versionless module name, e.g. 'fftw/3.3.4.1' => 'fftw'
63             dep_name = '/'.join(mod_name.split('/')[:-1])
64 
65             if mod_name.startswith('PrgEnv'):
66                 prgenv_mod = mod_name
67             else:
68                 swap_deps.append(self.module_generator.swap_module(dep_name, mod_name).lstrip())
69 
70         self.log.debug("Swap statements for dependencies of %s: %s", self.full_mod_name, swap_deps)
71 
72         if prgenv_mod is None:
73             raise EasyBuildError("Could not find a PrgEnv-* module listed as dependency: %s",
74                                  self.toolchain.dependencies)
75 
76         # unload statements for other PrgEnv modules
77         prgenv_unloads = ['']
78         for prgenv in [prgenv for prgenv in KNOWN_PRGENVS if not prgenv_mod.startswith(prgenv)]:
79             prgenv_unloads.append(self.module_generator.unload_module(prgenv).strip())
80 
81         # load statement for selected PrgEnv module (only when not loaded yet)
82         prgenv_load = self.module_generator.load_module(prgenv_mod, recursive_unload=False)
83 
84         txt = '\n'.join(prgenv_unloads + [prgenv_load] + swap_deps)
85         return txt
86
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/easybuild/easyblocks/generic/craytoolchain.py b/easybuild/easyblocks/generic/craytoolchain.py
--- a/easybuild/easyblocks/generic/craytoolchain.py
+++ b/easybuild/easyblocks/generic/craytoolchain.py
@@ -76,7 +76,9 @@
         # unload statements for other PrgEnv modules
         prgenv_unloads = ['']
         for prgenv in [prgenv for prgenv in KNOWN_PRGENVS if not prgenv_mod.startswith(prgenv)]:
-            prgenv_unloads.append(self.module_generator.unload_module(prgenv).strip())
+            is_loaded_guard = self.module_generator.is_loaded(prgenv)
+            unload_stmt = self.module_generator.unload_module(prgenv).strip()
+            prgenv_unloads.append(self.module_generator.conditional_statement(is_loaded_guard, unload_stmt))
 
         # load statement for selected PrgEnv module (only when not loaded yet)
         prgenv_load = self.module_generator.load_module(prgenv_mod, recursive_unload=False)

{"golden_diff": "diff --git a/easybuild/easyblocks/generic/craytoolchain.py b/easybuild/easyblocks/generic/craytoolchain.py\n--- a/easybuild/easyblocks/generic/craytoolchain.py\n+++ b/easybuild/easyblocks/generic/craytoolchain.py\n@@ -76,7 +76,9 @@\n         # unload statements for other PrgEnv modules\n         prgenv_unloads = ['']\n         for prgenv in [prgenv for prgenv in KNOWN_PRGENVS if not prgenv_mod.startswith(prgenv)]:\n-            prgenv_unloads.append(self.module_generator.unload_module(prgenv).strip())\n+            is_loaded_guard = self.module_generator.is_loaded(prgenv)\n+            unload_stmt = self.module_generator.unload_module(prgenv).strip()\n+            prgenv_unloads.append(self.module_generator.conditional_statement(is_loaded_guard, unload_stmt))\n \n         # load statement for selected PrgEnv module (only when not loaded yet)\n         prgenv_load = self.module_generator.load_module(prgenv_mod, recursive_unload=False)\n", "issue": "stderr: ERROR: Unable to locate a modulefile for 'PrgEnv-pgi' \nI am installing CrayCCE on the a Cray XC40 and am recieving the error.\r\n\r\n```\r\nstderr: ERROR: Unable to locate a modulefile for 'PrgEnv-pgi'\\n)\") \r\n```\r\n\r\nLooking at the LOG I see...\r\n\r\n```\r\nmodule unload PrgEnv-gnu\r\nmodule unload PrgEnv-intel\r\nmodule unload PrgEnv-pgi\r\n\r\n; stderr: ERROR: Unable to locate a modulefile for 'PrgEnv-pgi' \r\n```\r\n\r\nIn our system we do not even have `PrgEnv-pgi` so it is natural that we get this error, but we are not using pgi either so\r\nit should be checked if such a module exists prior of unloading it.\r\nAlso `PrgEnv-pgi` is not listed as a dependency in the config file.\r\n\r\nWe are using module.tcl Modules Release 4.1.3.1 (2018-11-29)\r\nand I know that EasyBuild prefers Lmod, but still module.tcl is supported.\r\n\r\nCheers\r\nHenric\n", "before_files": [{"content": "##\n# Copyright 2015-2020 Ghent University\n#\n# This file is part of EasyBuild,\n# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),\n# with support of Ghent University (http://ugent.be/hpc),\n# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),\n# Flemish Research Foundation (FWO) (http://www.fwo.be/en)\n# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).\n#\n# https://github.com/easybuilders/easybuild\n#\n# EasyBuild is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation v2.\n#\n# EasyBuild is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with EasyBuild.  If not, see <http://www.gnu.org/licenses/>.\n##\n\"\"\"\nEasyBuild support for installing Cray toolchains, implemented as an easyblock\n\n@author: Kenneth Hoste (Ghent University)\n@author: Guilherme Peretti Pezzi (CSCS)\n@author: Petar Forai (IMP/IMBA)\n\"\"\"\n\nfrom easybuild.easyblocks.generic.bundle import Bundle\nfrom easybuild.tools.build_log import EasyBuildError\n\n\nKNOWN_PRGENVS = ['PrgEnv-cray', 'PrgEnv-gnu', 'PrgEnv-intel', 'PrgEnv-pgi']\n\n\nclass CrayToolchain(Bundle):\n    \"\"\"\n    Compiler toolchain: generate module file only, nothing to build/install\n    \"\"\"\n\n    def prepare_step(self, *args, **kwargs):\n        \"\"\"Prepare build environment (skip loaded of dependencies).\"\"\"\n\n        kwargs['load_tc_deps_modules'] = False\n\n        super(CrayToolchain, self).prepare_step(*args, **kwargs)\n\n    def make_module_dep(self):\n        \"\"\"\n        Generate load/swap statements for dependencies in the module file\n        \"\"\"\n        prgenv_mod = None\n\n        # collect 'swap' statement for dependencies (except PrgEnv)\n        swap_deps = []\n        for dep in self.toolchain.dependencies:\n            mod_name = dep['full_mod_name']\n            # determine versionless module name, e.g. 'fftw/3.3.4.1' => 'fftw'\n            dep_name = '/'.join(mod_name.split('/')[:-1])\n\n            if mod_name.startswith('PrgEnv'):\n                prgenv_mod = mod_name\n            else:\n                swap_deps.append(self.module_generator.swap_module(dep_name, mod_name).lstrip())\n\n        self.log.debug(\"Swap statements for dependencies of %s: %s\", self.full_mod_name, swap_deps)\n\n        if prgenv_mod is None:\n            raise EasyBuildError(\"Could not find a PrgEnv-* module listed as dependency: %s\",\n                                 self.toolchain.dependencies)\n\n        # unload statements for other PrgEnv modules\n        prgenv_unloads = ['']\n        for prgenv in [prgenv for prgenv in KNOWN_PRGENVS if not prgenv_mod.startswith(prgenv)]:\n            prgenv_unloads.append(self.module_generator.unload_module(prgenv).strip())\n\n        # load statement for selected PrgEnv module (only when not loaded yet)\n        prgenv_load = self.module_generator.load_module(prgenv_mod, recursive_unload=False)\n\n        txt = '\\n'.join(prgenv_unloads + [prgenv_load] + swap_deps)\n        return txt\n", "path": "easybuild/easyblocks/generic/craytoolchain.py"}], "after_files": [{"content": "##\n# Copyright 2015-2020 Ghent University\n#\n# This file is part of EasyBuild,\n# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),\n# with support of Ghent University (http://ugent.be/hpc),\n# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),\n# Flemish Research Foundation (FWO) (http://www.fwo.be/en)\n# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).\n#\n# https://github.com/easybuilders/easybuild\n#\n# EasyBuild is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation v2.\n#\n# EasyBuild is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with EasyBuild.  If not, see <http://www.gnu.org/licenses/>.\n##\n\"\"\"\nEasyBuild support for installing Cray toolchains, implemented as an easyblock\n\n@author: Kenneth Hoste (Ghent University)\n@author: Guilherme Peretti Pezzi (CSCS)\n@author: Petar Forai (IMP/IMBA)\n\"\"\"\n\nfrom easybuild.easyblocks.generic.bundle import Bundle\nfrom easybuild.tools.build_log import EasyBuildError\n\n\nKNOWN_PRGENVS = ['PrgEnv-cray', 'PrgEnv-gnu', 'PrgEnv-intel', 'PrgEnv-pgi']\n\n\nclass CrayToolchain(Bundle):\n    \"\"\"\n    Compiler toolchain: generate module file only, nothing to build/install\n    \"\"\"\n\n    def prepare_step(self, *args, **kwargs):\n        \"\"\"Prepare build environment (skip loaded of dependencies).\"\"\"\n\n        kwargs['load_tc_deps_modules'] = False\n\n        super(CrayToolchain, self).prepare_step(*args, **kwargs)\n\n    def make_module_dep(self):\n        \"\"\"\n        Generate load/swap statements for dependencies in the module file\n        \"\"\"\n        prgenv_mod = None\n\n        # collect 'swap' statement for dependencies (except PrgEnv)\n        swap_deps = []\n        for dep in self.toolchain.dependencies:\n            mod_name = dep['full_mod_name']\n            # determine versionless module name, e.g. 'fftw/3.3.4.1' => 'fftw'\n            dep_name = '/'.join(mod_name.split('/')[:-1])\n\n            if mod_name.startswith('PrgEnv'):\n                prgenv_mod = mod_name\n            else:\n                swap_deps.append(self.module_generator.swap_module(dep_name, mod_name).lstrip())\n\n        self.log.debug(\"Swap statements for dependencies of %s: %s\", self.full_mod_name, swap_deps)\n\n        if prgenv_mod is None:\n            raise EasyBuildError(\"Could not find a PrgEnv-* module listed as dependency: %s\",\n                                 self.toolchain.dependencies)\n\n        # unload statements for other PrgEnv modules\n        prgenv_unloads = ['']\n        for prgenv in [prgenv for prgenv in KNOWN_PRGENVS if not prgenv_mod.startswith(prgenv)]:\n            is_loaded_guard = self.module_generator.is_loaded(prgenv)\n            unload_stmt = self.module_generator.unload_module(prgenv).strip()\n            prgenv_unloads.append(self.module_generator.conditional_statement(is_loaded_guard, unload_stmt))\n\n        # load statement for selected PrgEnv module (only when not loaded yet)\n        prgenv_load = self.module_generator.load_module(prgenv_mod, recursive_unload=False)\n\n        txt = '\\n'.join(prgenv_unloads + [prgenv_load] + swap_deps)\n        return txt\n", "path": "easybuild/easyblocks/generic/craytoolchain.py"}]}