dev_container: Start containers from the project directory

The initializeCommand lifecycle hook already runs with cwd set to the project directory, but the subsequent docker run / podman run inherited Zed's own working directory. Relative paths in runArgs such as --env-file .devcontainer/.env therefore failed to resolve.

Set current_dir on the spawned command so relative runArgs paths line up with where lifecycle scripts already run, matching the behavior of the upstream devcontainer CLI.

Add a get_current_dir accessor on util::command::Command so the regression test can verify the working directory is set.
This commit is contained in:
Kunall Banerjee 2026-05-19 04:48:04 -04:00
parent b8dce970fa
commit de7536536e
No known key found for this signature in database
3 changed files with 15 additions and 0 deletions

View file

@ -1841,6 +1841,7 @@ RUN sed -i -E 's/((^|\s)PATH=)([^\$]*)$/\1\${PATH:-\3}/g' /etc/profile || true
let docker_cli = self.docker_client.docker_cli();
let mut command = Command::new(&docker_cli);
command.current_dir(&self.local_project_directory);
command.arg("run");
@ -3100,6 +3101,12 @@ mod test {
let docker_run_command = docker_run_command.expect("ok");
assert_eq!(docker_run_command.get_program(), "docker");
assert_eq!(
docker_run_command.get_current_dir(),
Some(Path::new(TEST_PROJECT_PATH)),
"docker run must execute with cwd set to the project directory so \
relative paths in `runArgs` (e.g. `--env-file .devcontainer/.env`) resolve correctly"
);
let expected_config_file_label = PathBuf::from(TEST_PROJECT_PATH)
.join(".devcontainer")
.join("devcontainer.json");

View file

@ -72,6 +72,10 @@ impl Command {
self.0.get_args()
}
pub fn get_current_dir(&self) -> Option<&Path> {
self.0.get_current_dir()
}
pub fn env(&mut self, key: impl AsRef<OsStr>, val: impl AsRef<OsStr>) -> &mut Self {
self.0.env(key, val);
self

View file

@ -225,6 +225,10 @@ impl Command {
pub fn get_program(&self) -> &OsStr {
self.program.as_os_str()
}
pub fn get_current_dir(&self) -> Option<&Path> {
self.current_dir.as_deref()
}
}
#[derive(Debug)]