From edcc33463162995a1557122e34fa4a691077e980 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Wed, 12 Aug 2020 13:38:08 +0200 Subject: [PATCH] Respect $PATH ordering when registering compilers (#17967) --- lib/spack/spack/compilers/__init__.py | 4 +--- lib/spack/spack/test/cmd/compiler.py | 32 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/spack/spack/compilers/__init__.py b/lib/spack/spack/compilers/__init__.py index 36291a8f63..c11bafe47d 100644 --- a/lib/spack/spack/compilers/__init__.py +++ b/lib/spack/spack/compilers/__init__.py @@ -576,9 +576,7 @@ def _default(search_paths): ) command_arguments.append(detect_version_args) - # Reverse it here so that the dict creation (last insert wins) - # does not spoil the intended precedence. - return reversed(command_arguments) + return command_arguments fn = getattr( operating_system, 'arguments_to_detect_version_fn', _default diff --git a/lib/spack/spack/test/cmd/compiler.py b/lib/spack/spack/test/cmd/compiler.py index 61c67ccecd..c5c354221e 100644 --- a/lib/spack/spack/test/cmd/compiler.py +++ b/lib/spack/spack/test/cmd/compiler.py @@ -250,3 +250,35 @@ def test_compiler_find_prefer_no_suffix( assert clang['paths']['cc'] == str(clangdir.join('clang')) assert clang['paths']['cxx'] == str(clangdir.join('clang++')) + + +def test_compiler_find_path_order( + no_compilers_yaml, working_env, clangdir): + """Ensure that we find compilers that come first in the PATH first + """ + + with clangdir.as_cwd(): + os.mkdir('first_in_path') + shutil.copy('gcc-8', 'first_in_path/gcc-8') + shutil.copy('g++-8', 'first_in_path/g++-8') + shutil.copy('gfortran-8', 'first_in_path/gfortran-8') + + # the first_in_path folder should be searched first + os.environ['PATH'] = '{0}:{1}'.format( + str(clangdir.join("first_in_path")), + str(clangdir), + ) + + compiler('find', '--scope=site') + + config = spack.compilers.get_compiler_config('site', False) + + gcc = next(c['compiler'] for c in config + if c['compiler']['spec'] == 'gcc@8.4.0') + + assert gcc['paths'] == { + 'cc': str(clangdir.join('first_in_path', 'gcc-8')), + 'cxx': str(clangdir.join('first_in_path', 'g++-8')), + 'f77': str(clangdir.join('first_in_path', 'gfortran-8')), + 'fc': str(clangdir.join('first_in_path', 'gfortran-8')), + }