compiler.py: fix early return (#43898)

This commit is contained in:
Harmen Stoppels 2024-04-29 16:53:27 +02:00 committed by GitHub
parent 859745f1a9
commit 3e6e9829da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 7 deletions

View file

@ -20,6 +20,7 @@
import spack.compilers
import spack.error
import spack.schema.environment
import spack.spec
import spack.util.executable
import spack.util.libc
@ -683,8 +684,8 @@ def __str__(self):
@contextlib.contextmanager
def compiler_environment(self):
# yield immediately if no modules
if not self.modules:
# Avoid modifying os.environ if possible.
if not self.modules and not self.environment:
yield
return
@ -701,13 +702,9 @@ def compiler_environment(self):
spack.util.module_cmd.load_module(module)
# apply other compiler environment changes
env = spack.util.environment.EnvironmentModifications()
env.extend(spack.schema.environment.parse(self.environment))
env.apply_modifications()
spack.schema.environment.parse(self.environment).apply_modifications()
yield
except BaseException:
raise
finally:
# Restore environment regardless of whether inner code succeeded
os.environ.clear()

View file

@ -943,3 +943,17 @@ def test_detection_requires_c_compiler(detected_versions, expected_length):
"""
result = spack.compilers.make_compiler_list(detected_versions)
assert len(result) == expected_length
def test_compiler_environment(working_env):
"""Test whether environment modifications from compilers are applied in compiler_environment"""
os.environ.pop("TEST", None)
compiler = Compiler(
"gcc@=13.2.0",
operating_system="ubuntu20.04",
target="x86_64",
paths=["/test/bin/gcc", "/test/bin/g++"],
environment={"set": {"TEST": "yes"}},
)
with compiler.compiler_environment():
assert os.environ["TEST"] == "yes"