From 7a77ecbdb6f2c7cf8bd2855c1def95a734aeccbb Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Mon, 3 Apr 2023 22:54:18 +0200 Subject: modules: remove default symlink on uninstall (#36454) When app is uninstalled, if it matches a default, then remove the default symlink targeting its modulefile. Until now, when a default were uninstalled, the default symlink were left pointing to a nonexistent modulefile. --- lib/spack/spack/modules/common.py | 13 +++++++++++++ lib/spack/spack/test/modules/common.py | 3 +++ 2 files changed, 16 insertions(+) (limited to 'lib') diff --git a/lib/spack/spack/modules/common.py b/lib/spack/spack/modules/common.py index 3995233dba..66fe145f35 100644 --- a/lib/spack/spack/modules/common.py +++ b/lib/spack/spack/modules/common.py @@ -935,6 +935,7 @@ class BaseModuleFileWriter(object): if os.path.exists(mod_file): try: os.remove(mod_file) # Remove the module file + self.remove_module_defaults() # Remove default targeting module file os.removedirs( os.path.dirname(mod_file) ) # Remove all the empty directories from the leaf up @@ -942,6 +943,18 @@ class BaseModuleFileWriter(object): # removedirs throws OSError on first non-empty directory found pass + def remove_module_defaults(self): + if not any(self.spec.satisfies(default) for default in self.conf.defaults): + return + + # This spec matches a default, symlink needs to be removed as we remove the module + # file it targets. + default_symlink = os.path.join(os.path.dirname(self.layout.filename), "default") + try: + os.unlink(default_symlink) + except OSError: + pass + @contextlib.contextmanager def disable_modules(): diff --git a/lib/spack/spack/test/modules/common.py b/lib/spack/spack/test/modules/common.py index 14b6b051a6..0dc12d5bbe 100644 --- a/lib/spack/spack/test/modules/common.py +++ b/lib/spack/spack/test/modules/common.py @@ -77,6 +77,9 @@ def test_modules_default_symlink( assert os.path.islink(link_path) assert os.readlink(link_path) == mock_module_filename + generator.remove() + assert not os.path.lexists(link_path) + class MockDb(object): def __init__(self, db_ids, spec_hash_to_db): -- cgit v1.2.3-70-g09d2