Preserve child working directory in environment overlays

This commit is contained in:
2026-11-02 11:36:40 +00:00
parent 31f9e48219
commit a2d75c462c
4 changed files with 106 additions and 6 deletions

View File

@@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"sort"
"strings"
@@ -151,14 +150,15 @@ func newCommand(ctx context.Context, invocation Invocation) (*exec.Cmd, error) {
command.Dir = invocation.Dir
command.WaitDelay = time.Second
if len(invocation.Env) != 0 || len(invocation.UnsetEnv) != 0 {
command.Env = environmentEntries(invocation.Env, invocation.UnsetEnv)
// Dir contributes PWD on POSIX before explicit overrides are applied.
command.Env = environmentEntries(command.Environ(), invocation.Env, invocation.UnsetEnv)
}
return command, nil
}
func environmentEntries(overlay map[string]string, unset []string) []string {
environment := make(map[string]string, len(os.Environ())+len(overlay))
for _, entry := range os.Environ() {
func environmentEntries(base []string, overlay map[string]string, unset []string) []string {
environment := make(map[string]string, len(base)+len(overlay))
for _, entry := range base {
key, value, ok := strings.Cut(entry, "=")
if ok {
environment[key] = value