Repository: skypilot-org/skypilot

Issue:
Allow setting the `workdir` using environment variables
For situations when the working directory is not known in advance (e.g., on remote machines), it would be helpful to be able to dynamically populate the `workdir` value using an environment variable using the following syntax:

```yaml
envs:
  MY_WORKDIR: "some/path/"

workfir: ${MY_WORKDIR}
```

Discussion:
BabyChouSr: > Thanks @BabyChouSr! I tested manually and it works. Can we add some tests for this functionality?

Yup! just added a simple one in `test_yaml_parser.py`.

Patch:
diff --git a/sky/task.py b/sky/task.py
index cf26e137..cebc616d 100644
--- a/sky/task.py
+++ b/sky/task.py
@@ -393,6 +393,11 @@ class Task:
             config['service'] = _fill_in_env_vars(config['service'],
                                                   config.get('envs', {}))
 
+        # Fill in any Task.envs into workdir
+        if config.get('workdir') is not None:
+            config['workdir'] = _fill_in_env_vars(config['workdir'],
+                                                  config.get('envs', {}))
+
         task = Task(
             config.pop('name', None),
             run=config.pop('run', None),


Test patch:
diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py
index 1453cfe1..7d304b60 100644
--- a/tests/test_yaml_parser.py
+++ b/tests/test_yaml_parser.py
@@ -146,3 +146,14 @@ def test_invalid_empty_envs(tmp_path):
     with pytest.raises(ValueError) as e:
         Task.from_yaml(config_path)
     assert 'Environment variable \'env_key2\' is None.' in e.value.args[0]
+
+
+def test_replace_envs_in_workdir(tmpdir, tmp_path):
+    config_path = _create_config_file(
+        textwrap.dedent(f"""\
+            envs:
+                env_key1: {tmpdir}
+            workdir: $env_key1
+            """), tmp_path)
+    task = Task.from_yaml(config_path)
+    assert task.workdir == tmpdir