Skip to content

Copy env file and directory to support includes#138

Closed
adrienbernede wants to merge 5 commits intomainfrom
woptim/support_env_directory
Closed

Copy env file and directory to support includes#138
adrienbernede wants to merge 5 commits intomainfrom
woptim/support_env_directory

Conversation

@adrienbernede
Copy link
Copy Markdown
Member

@adrienbernede adrienbernede commented Nov 22, 2024

Solves #137

This allows spack.yaml files to include configuration files as long as they belong to the same directory or sub-directories.

Note:
spack env create is not run anymore for existing spack.yaml file. Copy works fine in my tests, but there might be a side effect.

@chapman39
Copy link
Copy Markdown
Collaborator

This doesn't appear to cause any issues in Serac, but I am worried there may be some consequence down the road. Does Spack not support moving external files to the Spack Environment that are included in the spack.yaml? Maybe we can create an issue in Spack.

@chapman39
Copy link
Copy Markdown
Collaborator

@adrienbernede personally im ok with merging but @white238 @cyrush thoughts?

Co-authored-by: Alex Tyler Chapman <100869159+chapman39@users.noreply.github.com>
@white238
Copy link
Copy Markdown
Member

@becker33 thoughts?

@becker33
Copy link
Copy Markdown
Member

If you aren't using --keep-relative in your spack env create command, it's supposed to be de-relativizing the paths -- sounds like that isn't working.

I'm able to reproduce it not working locally, but I'm not sure that I'll have time to debug it this week.

@becker33
Copy link
Copy Markdown
Member

Can you try applying this diff to Spack and see whether the change is still necessary?

$ git diff
diff --git a/lib/spack/spack/environment/environment.py b/lib/spack/spack/environment/environment.py
index 7877a84764..37ef654888 100644
--- a/lib/spack/spack/environment/environment.py
+++ b/lib/spack/spack/environment/environment.py
@@ -3015,7 +3015,7 @@ def included_config_scopes(self) -> List[spack.config.ConfigScope]:
         if missing:
             msg = "Detected {0} missing include path(s):".format(len(missing))
             msg += "\n   {0}".format("\n   ".join(missing))
-            raise spack.config.ConfigFileError(msg)
+            tty.warn(msg)
 
         return scopes

I have it still emiting a warning, a complete feature would avoid that but require a bit more engineering -- this should be enough to test whether it works for you and is worth moving forward.

@adrienbernede
Copy link
Copy Markdown
Member Author

@becker33 I tried you suggested change. It turned a failure to find included files into a failure to find the compilers.
Let me give some more context:

Uberenv instructs Spack to create an environment at a dest/dir/ from origin/dir/spack.yaml. But doing so, origin/dir/compilers.yaml is not copied other automatically. Since spack.yaml has a relative path, this result in a failure because dest/dir/compilers.yaml does not exists.

This PR fixes that by replacing spack create by a direct copy of the content of origin/dir to dest/dir.

@becker33
Copy link
Copy Markdown
Member

@adrienbernede when you use it with that change, what paths do you see in the created environment?

The reason I'm asking is that Spack is supposed to be turning them into absolute paths, which would get rid of the need to copy them at all. But it doesn't appear to be happening properly in your case, and I'm trying to figure out why.

@adrienbernede
Copy link
Copy Markdown
Member Author

@becker33 here is a reproducer with no uberenv involved:

## Radiuss Spack Configs
> git clone git@github.com:LLNL/radiuss-spack-configs.git
> git checkout woptim/config-updates
## Spack
> cd <path/to/spack>
> git checkout develop-2025-01-12
> . share/spack/setup-env.sh
## Let's go!
> spack env create -d ./spack_env <path/to/radiuss-spack-configs>/toss_4_x86_64_ib/spack.yaml
> cat <path/to/radiuss-spack-configs>/toss_4_x86_64_ib/spack.yaml
spack:
  include:
    - config.yaml
    - compilers.yaml
    - packages.yaml
  concretizer:
    reuse: false
    unify: false
  view: false
> cat spack_env/spack.yaml
spack:
  include:
    - config.yaml
    - compilers.yaml
    - packages.yaml
  concretizer:
    reuse: false
    unify: false
  view: false

I also tried using ./config.yaml, etc, with no success.

@becker33
Copy link
Copy Markdown
Member

@adrienbernede can you try Spack with this patch and confirm it works for your use case? I intend to propose it to the rest of the Spack team once you confirm it works for you:

diff --git a/lib/spack/spack/environment/environment.py b/lib/spack/spack/environment/environment.py
index 7877a84764..d13f1edf3a 100644
--- a/lib/spack/spack/environment/environment.py
+++ b/lib/spack/spack/environment/environment.py
@@ -2634,6 +2634,29 @@ def _ensure_env_dir():
 
     shutil.copy(envfile, target_manifest)
 
+    # Copy relative path includes that live inside the environment dir
+    try:
+        manifest = EnvironmentManifestFile(environment_dir)
+    except Exception as e:
+        msg = f"cannot initialize environment, '{environment_dir}' from manifest"
+        raise SpackEnvironmentError(msg) from e
+    else:
+        includes = manifest[TOP_LEVEL_KEY].get("include", [])
+        for include in includes:
+            if os.path.isabs(include):
+                continue
+
+            abspath = pathlib.Path(os.path.normpath(environment_dir / include))
+            if not abspath.is_relative_to(environment_dir):
+                # Warn that we are not copying relative path
+                msg = "Spack will not copy relative include path from outside environment"
+                msg += f" directory: {include}"
+                tty.warn(msg)
+                continue
+
+            orig_abspath = os.path.normpath(envfile.parent / include)
+            shutil.copy(orig_abspath, abspath)
+
 
 class EnvironmentManifestFile(collections.abc.Mapping):
     """Manages the in-memory representation of a manifest file, and its synchronization

@adrienbernede
Copy link
Copy Markdown
Member Author

adrienbernede commented Jan 21, 2025

@becker33 I confirm that tests passed with this change, causing the present PR to become obsolete.

@adrienbernede
Copy link
Copy Markdown
Member Author

@becker33 Please let us know when the fix is pushed. I'll be happy to use the following snapshot release.

@becker33
Copy link
Copy Markdown
Member

Fix is here: spack/spack#48689

@chapman39
Copy link
Copy Markdown
Collaborator

Should this PR be closed?

@adrienbernede
Copy link
Copy Markdown
Member Author

@chapman39 I need for these branch to exist a little longer.
Updating Spack to the latest release with the fix led to bugs.
You may close it, but please do not remove the branch.

@adrienbernede
Copy link
Copy Markdown
Member Author

I don't need the PR anymore, and pushed a tag for reference of the commit if necessary.

@adrienbernede adrienbernede deleted the woptim/support_env_directory branch February 13, 2025 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants