diff --git a/pkg/cmd/shell/shell.go b/pkg/cmd/shell/shell.go index 22db1ac7..90fbb79a 100644 --- a/pkg/cmd/shell/shell.go +++ b/pkg/cmd/shell/shell.go @@ -120,7 +120,7 @@ func runShellCommand(t *terminal.Terminal, sstore ShellStore, workspaceNameOrID // legacy environments wont support this and cause errrors, // but we don't want to block the user from using the shell _ = writeconnectionevent.WriteWCEOnEnv(sstore, workspace.DNS) - err = runSSH(sshName) + err = runSSH(sshName, host) if err != nil { return breverrors.WrapAndTrace(err) } @@ -144,9 +144,15 @@ func runShellCommand(t *terminal.Terminal, sstore ShellStore, workspaceNameOrID return nil } -func runSSH(sshAlias string) error { +func runSSH(sshAlias string, host bool) error { sshAgentEval := "eval $(ssh-agent -s)" - cmd := fmt.Sprintf("%s && ssh %s", sshAgentEval, sshAlias) + var cmd string + if host { + cmd = fmt.Sprintf("%s && ssh %s", sshAgentEval, sshAlias) + } else { + // SSH into VM and respect container WORKDIR if containerized, otherwise use default directory + cmd = fmt.Sprintf("%s && ssh -t %s 'DIR=$(readlink -f /proc/1/cwd 2>/dev/null || pwd); cd \"$DIR\" || echo \"Warning: Could not access container directory\" >&2; exec -l ${SHELL:-/bin/sh}'", sshAgentEval, sshAlias) + } sshCmd := exec.Command("bash", "-c", cmd) //nolint:gosec //cmd is user input sshCmd.Stderr = os.Stderr