From b34838913da606087b0f3141891f7d0fb2254eef Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 20 Apr 2022 17:07:14 -0400 Subject: os/exec: set PWD implicitly if Dir is non-empty and Env is nil Fixes #50599. Change-Id: I4e5dbb3972cdf21ede049567bfb98f2c992c5849 Reviewed-on: https://go-review.googlesource.com/c/go/+/401340 Run-TryBot: Bryan Mills TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills Reviewed-by: Ian Lance Taylor --- src/os/exec/example_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/os/exec/example_test.go') diff --git a/src/os/exec/example_test.go b/src/os/exec/example_test.go index a66890be69..bb166ceaf4 100644 --- a/src/os/exec/example_test.go +++ b/src/os/exec/example_test.go @@ -144,6 +144,21 @@ func ExampleCmd_CombinedOutput() { fmt.Printf("%s\n", stdoutStderr) } +func ExampleCmd_Environ() { + cmd := exec.Command("pwd") + + // Set Dir before calling cmd.Environ so that it will include an + // updated PWD variable (on platforms where that is used). + cmd.Dir = ".." + cmd.Env = append(cmd.Environ(), "POSIXLY_CORRECT=1") + + out, err := cmd.Output() + if err != nil { + log.Fatal(err) + } + fmt.Printf("%s\n", out) +} + func ExampleCommandContext() { ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) defer cancel() -- cgit v1.3