Skip to content

Commit e111ae6

Browse files
authored
Expose job working dir via environment variable (#3049)
* Expose the job's working dir via `DSTACK_WORKING_DIR` environment variable * Automatically `cd` to `DSTACK_WORKING_DIR` on login Closes: #3045
1 parent 740b2cc commit e111ae6

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

runner/internal/executor/executor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ func (ex *RunExecutor) execJob(ctx context.Context, jobLogFile io.Writer) error
394394
"DSTACK_RUN_NAME": ex.run.RunSpec.RunName,
395395
"DSTACK_REPO_ID": ex.run.RunSpec.RepoId,
396396
"DSTACK_REPO_DIR": ex.repoDir,
397+
"DSTACK_WORKING_DIR": ex.jobWorkingDir,
397398
"DSTACK_NODES_IPS": strings.Join(ex.clusterInfo.JobIPs, "\n"),
398399
"DSTACK_MASTER_NODE_IP": ex.clusterInfo.MasterJobIP,
399400
"DSTACK_NODE_RANK": strconv.Itoa(node_rank),
@@ -864,6 +865,9 @@ func writeDstackProfile(env map[string]string, pth string) error {
864865
return err
865866
}
866867
}
868+
if _, err = file.WriteString("cd \"$DSTACK_WORKING_DIR\"\n"); err != nil {
869+
return err
870+
}
867871
if err = os.Chmod(pth, 0o644); err != nil {
868872
return err
869873
}

src/dstack/_internal/server/services/jobs/configurators/base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,15 @@ def _dstack_image_commands(self) -> List[str]:
218218
):
219219
return []
220220
return [
221-
f"uv venv -q --prompt $DSTACK_RUN_NAME --seed -p {self._python()} {DSTACK_DIR}/venv",
221+
# `uv` may emit:
222+
# > warning: `VIRTUAL_ENV=/dstack/venv` does not match the project environment path
223+
# > `.venv` and will be ignored; use `--active` to target the active environment
224+
# > instead
225+
# Safe to ignore, reusing dstack's venv for `uv` is discouraged (it should only be
226+
# used for legacy `pip`-based configurations). `--no-active` suppresses the warning.
227+
# Alternatively, the user can call `deactivate` once before using `uv`.
228+
# If the user really wants to reuse dstack's venv, they must spefify `--active`.
229+
f"uv venv -q --prompt dstack -p {self._python()} --seed {DSTACK_DIR}/venv",
222230
f"echo '. {DSTACK_DIR}/venv/bin/activate' >> {DSTACK_PROFILE_PATH}",
223231
f". {DSTACK_DIR}/venv/bin/activate",
224232
]

src/tests/_internal/server/routers/test_runs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def get_dev_env_run_plan_dict(
110110
"-i",
111111
"-c",
112112
(
113-
"uv venv -q --prompt $DSTACK_RUN_NAME --seed -p 3.13 /dstack/venv"
113+
"uv venv -q --prompt dstack -p 3.13 --seed /dstack/venv"
114114
" && echo '. /dstack/venv/bin/activate' >> /dstack/profile"
115115
" && . /dstack/venv/bin/activate"
116116
" && (echo 'pip install ipykernel...'"
@@ -314,7 +314,7 @@ def get_dev_env_run_dict(
314314
"-i",
315315
"-c",
316316
(
317-
"uv venv -q --prompt $DSTACK_RUN_NAME --seed -p 3.13 /dstack/venv"
317+
"uv venv -q --prompt dstack -p 3.13 --seed /dstack/venv"
318318
" && echo '. /dstack/venv/bin/activate' >> /dstack/profile"
319319
" && . /dstack/venv/bin/activate"
320320
" && (echo 'pip install ipykernel...'"

src/tests/_internal/server/services/jobs/configurators/test_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def test_with_commands_no_image(self, shell: Optional[str], expected_shell
9898
expected_shell,
9999
"-i",
100100
"-c",
101-
"uv venv -q --prompt $DSTACK_RUN_NAME --seed -p 3.12 /dstack/venv"
101+
"uv venv -q --prompt dstack -p 3.12 --seed /dstack/venv"
102102
" && echo '. /dstack/venv/bin/activate' >> /dstack/profile"
103103
" && . /dstack/venv/bin/activate"
104104
" && sleep inf",

0 commit comments

Comments
 (0)