Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go: place GOROOT/bin at the beginning of PATH in 'go generate' an…
…d 'go test' This causes tests and generators that execute 'go' as a subprocess to use the same go command as the parent 'go test' or 'go generate' command. For #51473. Change-Id: I003cf1d05d1c93a26c6a7fdfad25e86c11765f59 Reviewed-on: https://go-review.googlesource.com/c/go/+/404134 Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Russ Cox <rsc@golang.org>
- Loading branch information
Showing
5 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# https://go.dev/issue/51473: to avoid the need for generators to rely on | ||
# runtime.GOROOT, 'go generate' should run the test with its own GOROOT/bin | ||
# at the beginning of $PATH. | ||
|
||
[short] skip | ||
|
||
[!plan9] env PATH= | ||
[plan9] env path= | ||
go generate . | ||
|
||
[!plan9] env PATH=$WORK${/}bin | ||
[plan9] env path=$WORK${/}bin | ||
go generate . | ||
|
||
-- go.mod -- | ||
module example | ||
|
||
go 1.19 | ||
-- main.go -- | ||
//go:generate go run . | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func main() { | ||
_, err := exec.LookPath("go") | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} | ||
-- $WORK/bin/README.txt -- | ||
This directory contains no executables. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# https://go.dev/issue/51473: to avoid the need for tests to rely on | ||
# runtime.GOROOT, 'go test' should run the test with its own GOROOT/bin | ||
# at the beginning of $PATH. | ||
|
||
[short] skip | ||
|
||
[!plan9] env PATH= | ||
[plan9] env path= | ||
go test . | ||
|
||
[!plan9] env PATH=$WORK${/}bin | ||
[plan9] env path=$WORK${/}bin | ||
go test . | ||
|
||
-- go.mod -- | ||
module example | ||
|
||
go 1.19 | ||
-- example_test.go -- | ||
package example | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestGoCommandExists(t *testing.T) { | ||
got, err := exec.LookPath("go") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := filepath.Join(os.Getenv("GOROOT"), "bin", "go" + os.Getenv("GOEXE")) | ||
if got != want { | ||
t.Fatalf(`exec.LookPath("go") = %q; want %q`, got, want) | ||
} | ||
} | ||
-- $WORK/bin/README.txt -- | ||
This directory contains no executables. |