From 0d4cd745dc003a07b96e62adccb21c4f082540fc Mon Sep 17 00:00:00 2001
From: Martin Lang <martin.lang@mpsd.mpg.de>
Date: Wed, 5 Feb 2025 09:17:04 +0100
Subject: [PATCH] Fix insertion of gcc dependence in intel-oneapi-compilers
 module

---
 src/mpsd_software_manager/spack.py | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/mpsd_software_manager/spack.py b/src/mpsd_software_manager/spack.py
index ad86f18..8764fed 100644
--- a/src/mpsd_software_manager/spack.py
+++ b/src/mpsd_software_manager/spack.py
@@ -334,9 +334,15 @@ def refresh_modules(compilers: dict[str, Any] | None = None) -> None:
         with module_file.open() as f:
             content = f.read()
         content = content.replace('family("compiler")', 'family("intel_compiler")')
-        # insert gcc as dependency before other dependencies
-        content = content.replace(
-            "depends_on(", f'depends_on("{gcc_module}")\ndepends_on(', 1
+        # insert gcc as dependency before other dependencies and before modifying
+        # MODULE_PATH
+        insertion_point = min(
+            content.find("depends_on("), content.find('prepend_path("MODULEPATH"')
+        )
+        content = (
+            content[:insertion_point]
+            + f'depends_on("{gcc_module}")\n'
+            + content[insertion_point:]
         )
         with module_file.open("w") as f:
             f.write(content)
-- 
GitLab