From f41d0214148c4adf21cbcdba2668de18ad66feb4 Mon Sep 17 00:00:00 2001
From: Eleni Mathioulaki <emathioulaki@athenarc.gr>
Date: Mon, 31 Jul 2023 12:48:37 +0200
Subject: [PATCH] rm spack.lock before env activation (to avoid errors with
 missing packages)

---
 .gitlab-ci.yml       | 9 +++++----
 install_spack_env.sh | 9 +++++----
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c75c3094..59ec4499 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -269,17 +269,18 @@ sync-gitlab-spack-instance:
     - spack load $EBRAINS_SPACK_COMPILER
     - spack compiler find
     - spack compiler list
-    # activate environment (and create it, if it doesn't exist)
+    # create environment, if it doesn't exist
     - spack env list | grep -q $SPACK_NFS_ENV && echo "Environment created already" || spack env create $SPACK_NFS_ENV
-    - spack env activate $SPACK_NFS_ENV
     # update environment site-configs
-    - rm -rf $SPACK_ENV/site-config && cp -r site-config $SPACK_ENV
+    - rm -rf $SPACK_ROOT/var/spack/environments/$SPACK_NFS_ENV/site-config && cp -r site-config $SPACK_ROOT/var/spack/environments/$SPACK_NFS_ENV
     # update spack.yaml: merge top-level and site-specific spack.yaml files
     - spack-python site-config/ymerge.py spack.yaml site-config/$SYSTEMNAME/spack.yaml > /tmp/spack.yaml
-    - cp /tmp/spack.yaml $SPACK_ENV/
+    - cp /tmp/spack.yaml $SPACK_ROOT/var/spack/environments/$SPACK_NFS_ENV
     # There is a known spack bug (https://github.com/spack/spack/issues/29447) in installing test dependencies for installation tests. The workaround suggested
     # in the issue is to NOT concretize separately, but simply remove the .lock file and let the enironment be concretized by the spack install command:
     - rm $SPACK_ROOT/var/spack/environments/$SPACK_NFS_ENV/spack.lock || echo "No spack.lock file"
+    # then activate environment
+    - spack env activate $SPACK_NFS_ENV
     - spack install -y -j2 --fresh --test root
     - spack reindex
   after_script:
diff --git a/install_spack_env.sh b/install_spack_env.sh
index 16311c47..f4ba7847 100644
--- a/install_spack_env.sh
+++ b/install_spack_env.sh
@@ -55,18 +55,19 @@ then
   spack env create $EBRAINS_SPACK_ENV
 fi
 
-# activate environment
-spack env activate $EBRAINS_SPACK_ENV
 # update environment site-configs
-rm -rf $SPACK_ENV/site-config && cp -r $EBRAINS_REPO/site-config $SPACK_ENV
+rm -rf $SPACK_ROOT/var/spack/environments/$EBRAINS_SPACK_ENV/site-config && cp -r $EBRAINS_REPO/site-config $SPACK_ROOT/var/spack/environments/$EBRAINS_SPACK_ENV
 # update spack.yaml: merge top-level and site-specific spack.yaml files
 spack-python $EBRAINS_REPO/site-config/ymerge.py $EBRAINS_REPO/spack.yaml $EBRAINS_REPO/site-config/$SYSTEMNAME/spack.yaml > /tmp/spack.yaml
-cp /tmp/spack.yaml $SPACK_ENV/
+cp /tmp/spack.yaml $SPACK_ROOT/var/spack/environments/$EBRAINS_SPACK_ENV/
 
 # There is a known spack bug (https://github.com/spack/spack/issues/29447) in installing test dependencies
 # for installation tests. The workaround suggested in the issue is to NOT concretize separately, but simply
 # remove the .lock file and let the environment be concretized by the spack install command:
 rm $SPACK_ROOT/var/spack/environments/$EBRAINS_SPACK_ENV/spack.lock || echo "No spack.lock file"
+
+# activate environment
+spack env activate $EBRAINS_SPACK_ENV
 # install the environment, use 2 jobs to reduce the amount of required RAM
 spack install -y -j2 --fresh --test root
 
-- 
GitLab