From 620f802352eee7a3b787c49ad654944de117433f Mon Sep 17 00:00:00 2001 From: Ricky Xu Date: Sat, 22 Jul 2023 00:35:45 -0700 Subject: [PATCH] [core][autoscaler] Fix env variable overwrite not able to be used if command itself uses the env (#37675) --------- Signed-off-by: rickyyx Co-authored-by: scv119 Signed-off-by: Victor --- python/ray/autoscaler/_private/util.py | 6 ++++-- python/ray/tests/test_autoscaler_util.py | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/python/ray/autoscaler/_private/util.py b/python/ray/autoscaler/_private/util.py index 20fb5198610cc..9eeafd938252f 100644 --- a/python/ray/autoscaler/_private/util.py +++ b/python/ray/autoscaler/_private/util.py @@ -395,13 +395,15 @@ def with_envs(cmds: List[str], kv: Dict[str, str]) -> str: Example: with_envs(["echo $FOO"], {"FOO": "BAR"}) - -> ["FOO=BAR echo $FOO"] + -> ["export FOO=BAR; echo $FOO"] """ out_cmds = [] for cmd in cmds: kv_str = "" for k, v in kv.items(): - kv_str += f"{k}={v} " + # We will need to do export here so that it works correctly with + # shell if the cmd args uses the argument. + kv_str += f"export {k}={v}; " out_cmds.append(f"{kv_str}{cmd}") return out_cmds diff --git a/python/ray/tests/test_autoscaler_util.py b/python/ray/tests/test_autoscaler_util.py index af35fbc418adc..c8b05f130ad3c 100644 --- a/python/ray/tests/test_autoscaler_util.py +++ b/python/ray/tests/test_autoscaler_util.py @@ -10,22 +10,22 @@ def test_with_envs(): assert with_envs( ["echo $RAY_HEAD_IP", "ray start"], {"RAY_HEAD_IP": "127.0.0.0"} ) == [ - "RAY_HEAD_IP=127.0.0.0 echo $RAY_HEAD_IP", - "RAY_HEAD_IP=127.0.0.0 ray start", + "export RAY_HEAD_IP=127.0.0.0; echo $RAY_HEAD_IP", + "export RAY_HEAD_IP=127.0.0.0; ray start", ] assert with_head_node_ip(["echo $RAY_HEAD_IP"], "127.0.0.0") == [ - "RAY_HEAD_IP=127.0.0.0 echo $RAY_HEAD_IP" + "export RAY_HEAD_IP=127.0.0.0; echo $RAY_HEAD_IP" ] assert ( - "RAY_HEAD_IP=456" + "export RAY_HEAD_IP=456" in with_envs( ["echo $RAY_CLOUD_ID"], {"RAY_CLOUD_ID": "123", "RAY_HEAD_IP": "456"} )[0] ) assert ( - "RAY_CLOUD_ID=123" + "export RAY_CLOUD_ID=123" in with_envs( ["echo $RAY_CLOUD_ID"], {"RAY_CLOUD_ID": "123", "RAY_HEAD_IP": "456"} )[0]