feat(BREV-2866): open shell in expected container working directory#308
feat(BREV-2866): open shell in expected container working directory#308nisolanki1209 wants to merge 2 commits intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the brev shell command to respect the container's WORKDIR when opening an interactive shell session in a custom container. Previously, users were placed in the default SSH login directory (/root) regardless of the container's intended working directory.
Changes:
- The
runSSHfunction signature is updated to accept ahost boolparameter, and the call site is updated accordingly. - When
host=false(the container path), the SSH command now reads/proc/1/cwdviareadlink(withpwdas a fallback) to determine the container's working directory, thencds into it before executing the shell. - The
host=truepath retains the original simplesshcommand behavior without modifications.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
pkg/cmd/shell/shell.go
Outdated
| 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 ${SHELL:-/bin/sh}'", sshAgentEval, sshAlias) |
There was a problem hiding this comment.
The shell is launched via exec ${SHELL:-/bin/sh} without the login flag (-l). This means the shell doesn't source login initialization files (such as /etc/profile, ~/.profile, or ~/.bash_profile). As a result, environment variables such as PATH and other shell customizations set in those files may not be available, which could break user tools and workflows. The original ssh alias command would spawn a login shell by default. To preserve login shell behavior, use exec -l ${SHELL:-/bin/sh} (POSIX) or exec ${SHELL:-/bin/sh} -l.
| 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 ${SHELL:-/bin/sh}'", sshAgentEval, sshAlias) | |
| 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) |
Problem
When launching a custom container, brev shell did not take the container’s
WORKDIRinto account. It opened a standard shell session, which defaults to the root user’s home directory, instead of the container’sWORKDIR.As a result, users were placed in
/rootrather than the container’s intendedWORKDIR.Solution
Updated the runSSH function to detect and respect the container’s
WORKDIRwhen launching brev shell.WORKDIRby reading/proc/1/cwd(PID 1 working directory).pwdif/proc/1/cwdis unavailable.${SHELL}with/bin/shas a fallback for minimal containers.