Skip to content

Commit

Permalink
fix: environment variables not being passed to MAPDL process (#3461)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Oct 7, 2024
1 parent 24b3a13 commit af3566d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3461.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: environment variables not being passed to MAPDL process
23 changes: 16 additions & 7 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -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()

Expand All @@ -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")


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)

Expand Down Expand Up @@ -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.
Expand All @@ -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."
Expand All @@ -1950,17 +1958,18 @@ 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):
raise TypeError(
"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):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit af3566d

Please sign in to comment.