From acda6a9ad6d16cad5300b5e36b9aaf6cd47f8496 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 14 Nov 2022 10:59:49 +0000 Subject: [PATCH] fix: Avoid running shell twice in coder agent The users login shell would be executed as: /bin/bash -c '/bin/bash -l' This simplifies the command for login shells so that the executed command is: /bin/bash -l --- agent/agent.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 1709eb9718bf9..e2508b658b5fe 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -561,25 +561,26 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri return nil, xerrors.Errorf("metadata is the wrong type: %T", metadata) } + // OpenSSH executes all commands with the users current shell. + // We replicate that behavior for IDE support. + caller := "-c" + if runtime.GOOS == "windows" { + caller = "/c" + } + args := []string{caller, rawCommand} + // gliderlabs/ssh returns a command slice of zero // when a shell is requested. - command := rawCommand - if len(command) == 0 { - command = shell + if len(rawCommand) == 0 { + args = []string{} if runtime.GOOS != "windows" { // On Linux and macOS, we should start a login // shell to consume juicy environment variables! - command += " -l" + args = append(args, "-l") } } - // OpenSSH executes all commands with the users current shell. - // We replicate that behavior for IDE support. - caller := "-c" - if runtime.GOOS == "windows" { - caller = "/c" - } - cmd := exec.CommandContext(ctx, shell, caller, command) + cmd := exec.CommandContext(ctx, shell, args...) cmd.Dir = metadata.Directory if cmd.Dir == "" { // Default to $HOME if a directory is not set!