From 10f309273ab838b982520e46740602a4b1b7da42 Mon Sep 17 00:00:00 2001 From: Massimiliano Culpo Date: Fri, 28 Mar 2025 20:14:12 +0100 Subject: [PATCH] Do not error when trying to convert corrupted compiler entries (#49759) fixes #49717 If no compiler is listed in the 'packages' section of the configuration, Spack will currently try to: 1. Look for a legacy compilers.yaml to convert 2. Look for compilers in PATH in that order. If an entry in compilers.yaml is corrupted, that should not result in an obscure error. Instead, it should just be skipped. Signed-off-by: Massimiliano Culpo --- lib/spack/spack/compilers/config.py | 11 ++++++++++- lib/spack/spack/test/compilers/conversion.py | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/spack/spack/compilers/config.py b/lib/spack/spack/compilers/config.py index 8214428278b..476bf193433 100644 --- a/lib/spack/spack/compilers/config.py +++ b/lib/spack/spack/compilers/config.py @@ -7,6 +7,7 @@ import os import re import sys +import warnings from typing import Any, Dict, List, Optional, Tuple import archspec.cpu @@ -337,7 +338,15 @@ def from_legacy_yaml(compiler_dict: Dict[str, Any]) -> List[spack.spec.Spec]: pkg_cls = spack.repo.PATH.get_pkg_class(pkg_name) pattern = re.compile(r"|".join(finder.search_patterns(pkg=pkg_cls))) filtered_paths = [x for x in candidate_paths if pattern.search(os.path.basename(x))] - detected = finder.detect_specs(pkg=pkg_cls, paths=filtered_paths) + try: + detected = finder.detect_specs(pkg=pkg_cls, paths=filtered_paths) + except Exception: + warnings.warn( + f"[{__name__}] cannot detect {pkg_name} from the " + f"following paths: {', '.join(filtered_paths)}" + ) + continue + for s in detected: for key in ("flags", "environment", "extra_rpaths"): if key in compiler_dict: diff --git a/lib/spack/spack/test/compilers/conversion.py b/lib/spack/spack/test/compilers/conversion.py index 84ce07f5ded..d9312c55b18 100644 --- a/lib/spack/spack/test/compilers/conversion.py +++ b/lib/spack/spack/test/compilers/conversion.py @@ -83,3 +83,12 @@ def tests_compiler_conversion_modules(mock_compiler): compiler_spec = CompilerFactory.from_legacy_yaml(mock_compiler)[0] assert compiler_spec.external assert compiler_spec.external_modules == modules + + +@pytest.mark.regression("49717") +def tests_compiler_conversion_corrupted_paths(mock_compiler): + """Tests that compiler entries with corrupted path do not raise""" + mock_compiler["paths"] = {"cc": "gcc", "cxx": "g++", "fc": "gfortran", "f77": "gfortran"} + # Test this call doesn't raise + compiler_spec = CompilerFactory.from_legacy_yaml(mock_compiler) + assert compiler_spec == []