Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: environment variables not being passed to MAPDL process #3461

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
break

else:
LOG.debug(

Check warning on line 704 in src/ansys/mapdl/core/launcher.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/launcher.py#L704

Added line #L704 was not covered by tests
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 @@
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 @@

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 @@
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 @@
"""

# 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 @@
"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
Loading