Skip to content

Commit 084dec8

Browse files
committed
Expose job working dir via environment variable
* Expose the job's working dir via `DSTACK_WORKING_DIR` environment variable * Automatically `cd` to `DSTACK_WORKING_DIR` on login Closes: #3045
1 parent a178834 commit 084dec8

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

  • runner/internal/executor
  • src/dstack/_internal/server/services/jobs/configurators

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
]

0 commit comments

Comments
 (0)