From d147ef231f40445b5815f1e3c434f70cfb37bc7a Mon Sep 17 00:00:00 2001 From: "John W. Parent" <45471568+johnwparent@users.noreply.github.com> Date: Thu, 25 May 2023 20:08:15 -0400 Subject: Windows: fix "spack build-env" (#37923) "spack build-env" was not generating proper environment variable definitions on Windows; this commit updates the generated commands to succeed with batch/PowerShell. --- lib/spack/spack/test/cmd/build_env.py | 6 +++++- lib/spack/spack/test/util/environment.py | 5 ++++- lib/spack/spack/util/environment.py | 13 ++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/spack/spack/test/cmd/build_env.py b/lib/spack/spack/test/cmd/build_env.py index 0e10a629ed..a03d9760bf 100644 --- a/lib/spack/spack/test/cmd/build_env.py +++ b/lib/spack/spack/test/cmd/build_env.py @@ -3,6 +3,7 @@ # # SPDX-License-Identifier: (Apache-2.0 OR MIT) import pickle +import sys import pytest @@ -39,7 +40,10 @@ def test_dump(tmpdir): with tmpdir.as_cwd(): build_env("--dump", _out_file, "zlib") with open(_out_file) as f: - assert any(line.startswith("PATH=") for line in f.readlines()) + if sys.platform == "win32": + assert any(line.startswith('set "PATH=') for line in f.readlines()) + else: + assert any(line.startswith("PATH=") for line in f.readlines()) @pytest.mark.usefixtures("config", "mock_packages", "working_env") diff --git a/lib/spack/spack/test/util/environment.py b/lib/spack/spack/test/util/environment.py index eff07cbee9..801c2d19f4 100644 --- a/lib/spack/spack/test/util/environment.py +++ b/lib/spack/spack/test/util/environment.py @@ -119,7 +119,10 @@ def test_dump_environment(prepare_environment_for_tests, tmpdir): dumpfile_path = str(tmpdir.join("envdump.txt")) envutil.dump_environment(dumpfile_path) with open(dumpfile_path, "r") as dumpfile: - assert "TEST_ENV_VAR={0}; export TEST_ENV_VAR\n".format(test_paths) in list(dumpfile) + if sys.platform == "win32": + assert 'set "TEST_ENV_VAR={}"\n'.format(test_paths) in list(dumpfile) + else: + assert "TEST_ENV_VAR={0}; export TEST_ENV_VAR\n".format(test_paths) in list(dumpfile) def test_reverse_environment_modifications(working_env): diff --git a/lib/spack/spack/util/environment.py b/lib/spack/spack/util/environment.py index 0eed2726b4..ded31dcf39 100644 --- a/lib/spack/spack/util/environment.py +++ b/lib/spack/spack/util/environment.py @@ -171,7 +171,11 @@ def path_put_first(var_name: str, directories: List[Path]): BASH_FUNCTION_FINDER = re.compile(r"BASH_FUNC_(.*?)\(\)") -def _env_var_to_source_line(var: str, val: str) -> str: +def _win_env_var_to_set_line(var: str, val: str) -> str: + return f'set "{var}={val}"' + + +def _nix_env_var_to_source_line(var: str, val: str) -> str: if var.startswith("BASH_FUNC"): source_line = "function {fname}{decl}; export -f {fname}".format( fname=BASH_FUNCTION_FINDER.sub(r"\1", var), decl=val @@ -181,6 +185,13 @@ def _env_var_to_source_line(var: str, val: str) -> str: return source_line +def _env_var_to_source_line(var: str, val: str) -> str: + if sys.platform == "win32": + return _win_env_var_to_set_line(var, val) + else: + return _nix_env_var_to_source_line(var, val) + + @system_path_filter(arg_slice=slice(1)) def dump_environment(path: Path, environment: Optional[MutableMapping[str, str]] = None): """Dump an environment dictionary to a source-able file. -- cgit v1.2.3-70-g09d2