Skip to content

Commit 9d7595b

Browse files
committed
Use correct user when searching for vscode path
1 parent d5623b9 commit 9d7595b

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

internal/pkg/devcontainers/dockerutils.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,11 @@ func ExecInDevContainer(containerID string, workDir string, args []string) error
251251

252252
statusWriter.Printf("Getting container PATH")
253253
containerPath, err := getContainerEnvVar(containerID, "PATH")
254+
vscodeServerPath := ""
254255
if err == nil {
255256
// Got the PATH
256257
statusWriter.Printf("Getting code server path")
257-
vscodeServerPath, err := getVscodeServerPath(containerID)
258+
vscodeServerPath, err = getVscodeServerPath(containerID, userName)
258259
if err == nil {
259260
// Got the VS Code server location - add bin subfolder to PATH
260261
containerPath = strings.TrimSpace(containerPath)
@@ -271,6 +272,13 @@ func ExecInDevContainer(containerID string, workDir string, args []string) error
271272
fmt.Println("Continuing without overriding PATH...")
272273
}
273274

275+
browser := ""
276+
if vscodeServerPath == "" {
277+
fmt.Printf("Warning: VS Code Server location not found. Continuing without setting BROWSER...")
278+
} else {
279+
browser = fmt.Sprintf("%s/helpers/browser.sh", vscodeServerPath)
280+
}
281+
274282
statusWriter.Printf("Getting VSCODE_IPC_HOOK_CLI")
275283
vscodeIpcSock, err := getVscodeIpcSock(containerID)
276284
if err != nil {
@@ -349,6 +357,9 @@ func ExecInDevContainer(containerID string, workDir string, args []string) error
349357
if vscodeGitIpcSock != "" {
350358
dockerArgs = append(dockerArgs, "--env", "VSCODE_GIT_IPC_HANDLE="+vscodeGitIpcSock)
351359
}
360+
if browser != "" {
361+
dockerArgs = append(dockerArgs, "--env", "BROWSER="+browser)
362+
}
352363
dockerArgs = append(dockerArgs, containerID)
353364
dockerArgs = append(dockerArgs, args...)
354365

@@ -382,34 +393,38 @@ func getSshAuthSockValue(containerID string) (string, error) {
382393
// Host has SSH_AUTH_SOCK set, so expecting the dev container to have forwarding set up
383394
// Find the latest /tmp/vscode-ssh-auth-<...>.sock
384395

385-
return getLatestFileMatch(containerID, "\"${TMPDIR:-/tmp}\"/vscode-ssh-auth-*")
396+
return getLatestFileMatch(containerID, "", "\"${TMPDIR:-/tmp}\"/vscode-ssh-auth-*")
386397
}
387398

388-
func getVscodeServerPath(containerID string) (string, error) {
389-
path, err := getLatestFileMatch(containerID, "${HOME}/.vscode-server/bin/*")
399+
func getVscodeServerPath(containerID string, userName string) (string, error) {
400+
path, err := getLatestFileMatch(containerID, userName, "${HOME}/.vscode-server/bin/*")
390401
if err == nil {
391402
return path, err
392403
}
393-
path, err = getLatestFileMatch(containerID, "/vscode/vscode-server/bin/linux-x64/*")
404+
path, err = getLatestFileMatch(containerID, "", "/vscode/vscode-server/bin/linux-x64/*")
394405
if err == nil {
395406
return path, err
396407
}
397-
return getLatestFileMatch(containerID, "/vscode/vscode-server/bin/x64/*")
408+
return getLatestFileMatch(containerID, "", "/vscode/vscode-server/bin/x64/*")
398409
}
399410
func getVscodeIpcSock(containerID string) (string, error) {
400-
return getLatestFileMatch(containerID, "\"${TMPDIR:-/tmp}\"/vscode-ipc-*")
411+
return getLatestFileMatch(containerID, "", "\"${TMPDIR:-/tmp}\"/vscode-ipc-*")
401412
}
402413
func getRemoteContainersIpcSock(containerID string) (string, error) {
403-
return getLatestFileMatch(containerID, "\"${TMPDIR:-/tmp}\"/vscode-remote-containers-ipc-*")
414+
return getLatestFileMatch(containerID, "", "\"${TMPDIR:-/tmp}\"/vscode-remote-containers-ipc-*")
404415
}
405416
func getGitIpcSock(containerID string, userID string) (string, error) {
406-
return getLatestFileMatch(containerID, fmt.Sprintf("\"${TMPDIR:-/tmp}\"/user/%s/vscode-git-*", userID))
417+
return getLatestFileMatch(containerID, "", fmt.Sprintf("\"${TMPDIR:-/tmp}\"/user/%s/vscode-git-*", userID))
407418
}
408419

409420
// getLatestFileMatch lists files matching `pattern` in the container and returns the latest filename
410-
func getLatestFileMatch(containerID string, pattern string) (string, error) {
421+
func getLatestFileMatch(containerID string, userName string, pattern string) (string, error) {
411422

412-
dockerArgs := []string{"exec", containerID, "bash", "-c", fmt.Sprintf("ls -t -d -1 %s", pattern)}
423+
dockerArgs := []string{"exec"}
424+
if userName != "" {
425+
dockerArgs = append(dockerArgs, "--user", userName)
426+
}
427+
dockerArgs = append(dockerArgs, containerID, "bash", "-c", fmt.Sprintf("ls -t -d -1 %s", pattern))
413428
dockerCmd := exec.Command("docker", dockerArgs...)
414429
buf, err := dockerCmd.CombinedOutput()
415430
if err != nil {

0 commit comments

Comments
 (0)