Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/20260422120133.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:sparkles: Add support for specifying the directory in subprocess commands
8 changes: 6 additions & 2 deletions utils/subprocess/command_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type command struct {
loggers logs.Loggers
cmdWrapper cmdWrapper
io ICommandIO
dir string
}

func (c *command) createCommand(cmdCtx context.Context) *exec.Cmd {
Expand All @@ -140,6 +141,7 @@ func (c *command) createCommand(cmdCtx context.Context) *exec.Cmd {
cmd.Stdin, cmd.Stdout, cmd.Stderr = c.io.Register(cmdCtx)
cmd.Env = cmd.Environ()
cmd.Env = append(cmd.Env, c.env...)
cmd.Dir = c.dir

// for any of our wait checks to work we need to set the group ID to the pid, otherwise the
// group ID will be the code that launched it (i.e. the code that calls exec.Cmd.Start). This
Expand Down Expand Up @@ -178,7 +180,7 @@ func (c *command) Check() (err error) {
return
}

func newCommand(loggers logs.Loggers, as *commandUtils.CommandAsDifferentUser, env []string, cmd string, args ...string) (osCmd *command) {
func newCommand(loggers logs.Loggers, as *commandUtils.CommandAsDifferentUser, env []string, dir string, cmd string, args ...string) (osCmd *command) {
osCmd = &command{
cmd: cmd,
args: args,
Expand All @@ -187,11 +189,12 @@ func newCommand(loggers logs.Loggers, as *commandUtils.CommandAsDifferentUser, e
loggers: loggers,
cmdWrapper: cmdWrapper{},
io: NewIOFromLoggers(loggers),
dir: dir,
}
return
}

func newCommandWithCustomIO(loggers logs.Loggers, io ICommandIO, as *commandUtils.CommandAsDifferentUser, env []string, cmd string, args ...string) (osCmd *command) {
func newCommandWithCustomIO(loggers logs.Loggers, io ICommandIO, as *commandUtils.CommandAsDifferentUser, env []string, dir string, cmd string, args ...string) (osCmd *command) {
osCmd = &command{
cmd: cmd,
args: args,
Expand All @@ -200,6 +203,7 @@ func newCommandWithCustomIO(loggers logs.Loggers, io ICommandIO, as *commandUtil
loggers: loggers,
cmdWrapper: cmdWrapper{},
io: io,
dir: dir,
}
return
}
Expand Down
10 changes: 5 additions & 5 deletions utils/subprocess/command_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func TestCmdRun(t *testing.T) {
loggers, err := logs.NewLogrLogger(logstest.NewTestLogger(t), "test")
require.NoError(t, err)
if platform.IsWindows() {
cmd = newCommand(loggers, commandUtils.Me(), nil, test.cmdWindows, test.argWindows...)
cmd = newCommand(loggers, commandUtils.Me(), nil, "", test.cmdWindows, test.argWindows...)
} else {
cmd = newCommand(loggers, commandUtils.Me(), nil, test.cmdOther, test.argOther...)
cmd = newCommand(loggers, commandUtils.Me(), nil, "", test.cmdOther, test.argOther...)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestCmdRunWithEnv(t *testing.T) {
if platform.IsWindows() {
cmd = newCommand(loggers, commandUtils.Me(), envTest.envVars, "powershell", "-Command", envTest.cmdWindows)
} else {
cmd = newCommand(loggers, commandUtils.Me(), envTest.envVars, envTest.cmdOther)
cmd = newCommand(loggers, commandUtils.Me(), envTest.envVars, "", envTest.cmdOther)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand Down Expand Up @@ -146,9 +146,9 @@ func TestCmdStartStop(t *testing.T) {
require.NoError(t, err)

if platform.IsWindows() {
cmd = newCommand(loggers, commandUtils.Me(), nil, test.cmdWindows, test.argWindows...)
cmd = newCommand(loggers, commandUtils.Me(), nil, "", test.cmdWindows, test.argWindows...)
} else {
cmd = newCommand(loggers, commandUtils.Me(), nil, test.cmdOther, test.argOther...)
cmd = newCommand(loggers, commandUtils.Me(), nil, "", test.cmdOther, test.argOther...)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand Down
Loading
Loading