From ca872f9c34f6b360d9d67a91294fa66452867e78 Mon Sep 17 00:00:00 2001 From: "John W. Parent" <45471568+johnwparent@users.noreply.github.com> Date: Fri, 1 Sep 2023 14:36:27 -0400 Subject: Windows: fix pwsh env activate/deactivate; load/unload (#39118) These commands are currently broken on powershell (Windows) due to improper use of the InvokeCommand commandlet and a lack of direct support for the `--pwsh` argument in `spack load`, `spack unload`, and `spack env deactivate`. --- lib/spack/spack/cmd/env.py | 7 +++++++ lib/spack/spack/cmd/load.py | 7 +++++++ lib/spack/spack/cmd/unload.py | 7 +++++++ lib/spack/spack/environment/shell.py | 4 ++-- lib/spack/spack/test/util/environment.py | 4 ++-- lib/spack/spack/util/environment.py | 13 ++++++------- 6 files changed, 31 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/cmd/env.py b/lib/spack/spack/cmd/env.py index 40600b96af..8a2f129bda 100644 --- a/lib/spack/spack/cmd/env.py +++ b/lib/spack/spack/cmd/env.py @@ -239,6 +239,13 @@ def env_deactivate_setup_parser(subparser): const="bat", help="print bat commands to activate the environment", ) + shells.add_argument( + "--pwsh", + action="store_const", + dest="shell", + const="pwsh", + help="print pwsh commands to activate the environment", + ) def env_deactivate(args): diff --git a/lib/spack/spack/cmd/load.py b/lib/spack/spack/cmd/load.py index e4cc7443f8..e68fe48dce 100644 --- a/lib/spack/spack/cmd/load.py +++ b/lib/spack/spack/cmd/load.py @@ -52,6 +52,13 @@ def setup_parser(subparser): const="bat", help="print bat commands to load the package", ) + shells.add_argument( + "--pwsh", + action="store_const", + dest="shell", + const="pwsh", + help="print pwsh commands to load the package", + ) subparser.add_argument( "--first", diff --git a/lib/spack/spack/cmd/unload.py b/lib/spack/spack/cmd/unload.py index 667cd4804c..1fecdc5b33 100644 --- a/lib/spack/spack/cmd/unload.py +++ b/lib/spack/spack/cmd/unload.py @@ -51,6 +51,13 @@ def setup_parser(subparser): const="bat", help="print bat commands to load the package", ) + shells.add_argument( + "--pwsh", + action="store_const", + dest="shell", + const="pwsh", + help="print pwsh commands to load the package", + ) subparser.add_argument( "-a", "--all", action="store_true", help="unload all loaded Spack packages" diff --git a/lib/spack/spack/environment/shell.py b/lib/spack/spack/environment/shell.py index 1ae2acd3c4..380e49fa0f 100644 --- a/lib/spack/spack/environment/shell.py +++ b/lib/spack/spack/environment/shell.py @@ -43,7 +43,7 @@ def activate_header(env, shell, prompt=None): # TODO: despacktivate # TODO: prompt elif shell == "pwsh": - cmds += "$Env:SPACK_ENV=%s\n" % env.path + cmds += "$Env:SPACK_ENV='%s'\n" % env.path else: if "color" in os.getenv("TERM", "") and prompt: prompt = colorize("@G{%s}" % prompt, color=True, enclose=True) @@ -82,7 +82,7 @@ def deactivate_header(shell): # TODO: despacktivate # TODO: prompt elif shell == "pwsh": - cmds += "Remove-Item Env:SPACK_ENV" + cmds += "Set-Item -Path Env:SPACK_ENV\n" else: cmds += "if [ ! -z ${SPACK_ENV+x} ]; then\n" cmds += "unset SPACK_ENV; export SPACK_ENV;\n" diff --git a/lib/spack/spack/test/util/environment.py b/lib/spack/spack/test/util/environment.py index 481a58db47..b797331b77 100644 --- a/lib/spack/spack/test/util/environment.py +++ b/lib/spack/spack/test/util/environment.py @@ -172,8 +172,8 @@ def test_escape_double_quotes_in_shell_modifications(): assert r'set "VAR=$PATH;$ANOTHER_PATH"' in cmds assert r'set "QUOTED_VAR="MY_VAL"' in cmds cmds = to_validate.shell_modifications(shell="pwsh") - assert r"$Env:VAR=$PATH;$ANOTHER_PATH" in cmds - assert r'$Env:QUOTED_VAR="MY_VAL"' in cmds + assert "$Env:VAR='$PATH;$ANOTHER_PATH'" in cmds + assert "$Env:QUOTED_VAR='\"MY_VAL\"'" in cmds else: cmds = to_validate.shell_modifications() assert 'export VAR="$PATH:$ANOTHER_PATH"' in cmds diff --git a/lib/spack/spack/util/environment.py b/lib/spack/spack/util/environment.py index 30322c320a..c18be76cc6 100644 --- a/lib/spack/spack/util/environment.py +++ b/lib/spack/spack/util/environment.py @@ -47,7 +47,7 @@ _SHELL_SET_STRINGS = { "csh": "setenv {0} {1};\n", "fish": "set -gx {0} {1};\n", "bat": 'set "{0}={1}"\n', - "pwsh": "$Env:{0}={1}\n", + "pwsh": "$Env:{0}='{1}'\n", } @@ -56,7 +56,7 @@ _SHELL_UNSET_STRINGS = { "csh": "unsetenv {0};\n", "fish": "set -e {0};\n", "bat": 'set "{0}="\n', - "pwsh": "Remove-Item Env:{0}\n", + "pwsh": "Set-Item -Path Env:{0}\n", } @@ -429,7 +429,7 @@ class RemovePath(NameValueModifier): def execute(self, env: MutableMapping[str, str]): tty.debug(f"RemovePath: {self.name}-{str(self.value)}", level=3) environment_value = env.get(self.name, "") - directories = environment_value.split(self.separator) if environment_value else [] + directories = environment_value.split(self.separator) directories = [ path_to_os_path(os.path.normpath(x)).pop() for x in directories @@ -724,11 +724,10 @@ class EnvironmentModifications: cmds += _SHELL_UNSET_STRINGS[shell].format(name) else: if sys.platform != "win32": - cmd = _SHELL_SET_STRINGS[shell].format( - name, double_quote_escape(new_env[name]) - ) + new_env_name = double_quote_escape(new_env[name]) else: - cmd = _SHELL_SET_STRINGS[shell].format(name, new_env[name]) + new_env_name = new_env[name] + cmd = _SHELL_SET_STRINGS[shell].format(name, new_env_name) cmds += cmd return cmds -- cgit v1.2.3-60-g2f50