From 31a6633e62b760b3bd7314b543cc4ebc43292090 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Sun, 18 Sep 2022 13:53:49 -0400 Subject: [PATCH] mdevenv: powershell <7.0 is EOL, prefer using pwsh.exe If neither pwsh.exe nor powershell.exe works, fallback to cmd.exe. This script could be failing because of virus scanner. --- mesonbuild/mdevenv.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mesonbuild/mdevenv.py b/mesonbuild/mdevenv.py index 8a5b5bacb..2fb790423 100644 --- a/mesonbuild/mdevenv.py +++ b/mesonbuild/mdevenv.py @@ -13,6 +13,8 @@ import typing as T if T.TYPE_CHECKING: from .backends import InstallData +POWERSHELL_EXES = {'pwsh.exe', 'powershell.exe'} + def add_arguments(parser: argparse.ArgumentParser) -> None: parser.add_argument('-C', dest='wd', action=RealPathAction, help='Directory to cd into before running') @@ -21,12 +23,17 @@ def add_arguments(parser: argparse.ArgumentParser) -> None: parser.add_argument('command', nargs=argparse.REMAINDER, help='Command to run in developer environment (default: interactive shell)') -def get_windows_shell() -> str: +def get_windows_shell() -> T.Optional[str]: mesonbuild = Path(__file__).parent script = mesonbuild / 'scripts' / 'cmd_or_ps.ps1' - command = ['powershell.exe', '-noprofile', '-executionpolicy', 'bypass', '-file', str(script)] - result = subprocess.check_output(command) - return result.decode().strip() + for shell in POWERSHELL_EXES: + try: + command = [shell, '-noprofile', '-executionpolicy', 'bypass', '-file', str(script)] + result = subprocess.check_output(command) + return result.decode().strip() + except (subprocess.CalledProcessError, OSError): + pass + return None def reduce_winepath(env: T.Dict[str, str]) -> None: winepath = env.get('WINEPATH') @@ -145,7 +152,9 @@ def run(options: argparse.Namespace) -> int: args = [shell_env] elif is_windows(): shell = get_windows_shell() - if shell in ['powershell.exe', 'pwsh.exe']: + if not shell: + mlog.warning('Failed to determine Windows shell, fallback to cmd.exe') + if shell in POWERSHELL_EXES: args = [shell, '-NoLogo', '-NoExit'] prompt = f'function global:prompt {{ "{prompt_prefix} PS " + $PWD + "> "}}' args += ['-Command', prompt]