From af3566d3af789ad1defa4fcf02c8b4afa748ed20 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 7 Oct 2024 17:43:20 +0200 Subject: [PATCH] fix: environment variables not being passed to MAPDL process (#3461) * fix: envvars ignoring current environment * fix: envvars ignoring current environment * feat: getting env vars in `launch_mapdl` * ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci * chore: adding changelog file 3461.fixed.md * fix: test --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> --- doc/changelog.d/3461.fixed.md | 1 + src/ansys/mapdl/core/launcher.py | 23 ++++++++++++++++------- tests/test_launcher.py | 3 ++- 3 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 doc/changelog.d/3461.fixed.md diff --git a/doc/changelog.d/3461.fixed.md b/doc/changelog.d/3461.fixed.md new file mode 100644 index 0000000000..76f0aad061 --- /dev/null +++ b/doc/changelog.d/3461.fixed.md @@ -0,0 +1 @@ +fix: environment variables not being passed to MAPDL process \ No newline at end of file diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 349602c667..ceafbbf806 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -602,7 +602,7 @@ def launch_grpc( env_vars = update_env_vars(add_env_vars, replace_env_vars) LOG.info( - f"Running a local instance at port {port} the following command: '{command}'" + f"Running a local instance in {run_location} at port {port} the following command: '{command}'" ) LOG.debug("MAPDL starting in background.") @@ -681,6 +681,7 @@ def _check_server_is_alive(stdout_queue, run_location, timeout): empty_i = 0 terminal_output = "" + LOG.debug(f"Checking if MAPDL server is alive") while time.time() < (t0 + timeout): terminal_output += "\n".join(_get_std_output(std_queue=stdout_queue)).strip() @@ -700,6 +701,9 @@ def _check_server_is_alive(stdout_queue, run_location, timeout): break else: + LOG.debug( + f"MAPDL gRPC server didn't print any valid output:\n{terminal_output}" + ) raise MapdlDidNotStart("MAPDL failed to start the gRPC server") @@ -1768,6 +1772,9 @@ def launch_mapdl( f"The machine has {machine_cores} cores. PyMAPDL is asking for {nproc} cores." ) + # Setting env vars + env_vars = update_env_vars(add_env_vars, replace_env_vars) + start_parm.update( { "exec_file": exec_file, @@ -1811,8 +1818,7 @@ def launch_mapdl( port, actual_run_location, process = launch_grpc( port=port, - add_env_vars=add_env_vars, - replace_env_vars=replace_env_vars, + replace_env_vars=env_vars, **start_parm, ) @@ -1917,7 +1923,7 @@ def check_mode(mode, version): return mode -def update_env_vars(add_env_vars, replace_env_vars): +def update_env_vars(add_env_vars: dict, replace_env_vars: dict) -> dict: """ Update environment variables for the MAPDL process. @@ -1939,6 +1945,8 @@ def update_env_vars(add_env_vars, replace_env_vars): """ # Expanding/replacing env variables for the process. + envvars = os.environ.copy() + if add_env_vars and replace_env_vars: raise ValueError( "'add_env_vars' and 'replace_env_vars' are incompatible. Please provide only one." @@ -1950,9 +1958,8 @@ def update_env_vars(add_env_vars, replace_env_vars): "The variable 'add_env_vars' should be a dict with env vars." ) - add_env_vars.update(os.environ) + envvars.update(add_env_vars) LOG.debug(f"Updating environment variables with: {add_env_vars}") - return add_env_vars elif replace_env_vars: if not isinstance(replace_env_vars, dict): @@ -1960,7 +1967,9 @@ def update_env_vars(add_env_vars, replace_env_vars): "The variable 'replace_env_vars' should be a dict with env vars." ) LOG.debug(f"Replacing environment variables with: {replace_env_vars}") - return replace_env_vars + envvars = replace_env_vars + + return envvars def _check_license_argument(license_type, additional_switches): diff --git a/tests/test_launcher.py b/tests/test_launcher.py index 5be0570c31..0e4bfd396b 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -256,7 +256,8 @@ def test_remove_temp_files_fail(tmpdir, mapdl): def test_env_injection(): - assert update_env_vars(None, None) is None + no_inject = update_env_vars(None, None) + assert no_inject == os.environ.copy() # return os.environ assert "myenvvar" in update_env_vars({"myenvvar": "True"}, None)