Skip to content

Commit 4fe324d

Browse files
committed
cmd/go: make TOOLEXEC_IMPORTPATH consistent with 'go list -f {{.ImportPath}}'
TOOLEXEC_IMPORTPATH is useful for the toolexec program to know what package is currently being built. This is otherwise tricky to figure out. Unfortunately, for test packages it was lacking. In the added test case, we have a total of four packages in 'go list -test': test/main test/main.test test/main [test/main.test] test/main_test [test/main.test] And, when running with -toolexec, one would get the following values: # test/main_test [test/main.test] compile TOOLEXEC_IMPORTPATH="test/main_test" # test/main [test/main.test] compile TOOLEXEC_IMPORTPATH="test/main" # test/main.test compile TOOLEXEC_IMPORTPATH="test/main.test" Note how the " [test/main.test]" suffixes are missing. Because of that, when one sees TOOLEXEC_IMPORTPATH="test/main", it is ambiguous whether the regular "test/main" package is meant, or its test variant, otherwise known as "test/main [test/main.test]" and including foo_test.go To fix this, we need unambiguous strings to identify the packages involved, just like one can do with "go list -test". "go list" already has such a field, ImportPath, which is also used when printing output from each build "action" as seen above. That string is not really an import path - internally, it's load.Package.Desc, and called a "description". However, it makes sense to be consistent with "go list -json", because it's the source of truth for practically all tools interacting with the Go toolchain. To keep cmd/go more consistent, "go list -f {{.ImportPath}}" now calls Package.Desc as well, instead of having its own copy of the string concatenation for ForTest. Fixes #44963. Change-Id: Ibce7fbb5549209dac50526043c0c7daa0beebc08 Reviewed-on: https://go-review.googlesource.com/c/go/+/313770 Reviewed-by: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Daniel Martí <[email protected]>
1 parent f68878f commit 4fe324d

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

src/cmd/go/alldocs.go

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/list/list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
628628
old := make(map[string]string)
629629
for _, p := range all {
630630
if p.ForTest != "" {
631-
new := p.ImportPath + " [" + p.ForTest + ".test]"
631+
new := p.Desc()
632632
old[new] = p.ImportPath
633633
p.ImportPath = new
634634
}

src/cmd/go/internal/work/build.go

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ and test commands:
152152
a program to use to invoke toolchain programs like vet and asm.
153153
For example, instead of running asm, the go command will run
154154
'cmd args /path/to/asm <arguments for asm>'.
155+
The TOOLEXEC_IMPORTPATH environment variable will be set,
156+
matching 'go list -f {{.ImportPath}}' for the package being built.
155157
156158
The -asmflags, -gccgoflags, -gcflags, and -ldflags flags accept a
157159
space-separated list of arguments to pass to an underlying tool

src/cmd/go/internal/work/exec.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -2071,8 +2071,11 @@ func (b *Builder) runOut(a *Action, dir string, env []string, cmdargs ...interfa
20712071

20722072
// Add the TOOLEXEC_IMPORTPATH environment variable for -toolexec tools.
20732073
// It doesn't really matter if -toolexec isn't being used.
2074+
// Note that a.Package.Desc is not really an import path,
2075+
// but this is consistent with 'go list -f {{.ImportPath}}'.
2076+
// Plus, it is useful to uniquely identify packages in 'go list -json'.
20742077
if a != nil && a.Package != nil {
2075-
cmd.Env = append(cmd.Env, "TOOLEXEC_IMPORTPATH="+a.Package.ImportPath)
2078+
cmd.Env = append(cmd.Env, "TOOLEXEC_IMPORTPATH="+a.Package.Desc())
20762079
}
20772080

20782081
cmd.Env = append(cmd.Env, env...)

src/cmd/go/testdata/script/toolexec.txt

+44-7
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,37 @@ go build ./cmd/mytool
1111
# Finally, note that asm and cgo are run twice.
1212

1313
go build -toolexec=$PWD/mytool
14-
[amd64] stderr -count=2 '^asm'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main/withasm$'
15-
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main/withasm$'
16-
[cgo] stderr -count=2 '^cgo'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main/withcgo$'
17-
[cgo] stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main/withcgo$'
18-
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main$'
19-
stderr -count=1 '^link'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main$'
14+
[amd64] stderr -count=2 '^asm'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main/withasm"$'
15+
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main/withasm"$'
16+
[cgo] stderr -count=2 '^cgo'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main/withcgo"$'
17+
[cgo] stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main/withcgo"$'
18+
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main"$'
19+
stderr -count=1 '^link'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main"$'
20+
21+
# Test packages are a little bit trickier.
22+
# We have four variants of test/main, as reported by 'go list -test':
23+
#
24+
# test/main - the regular non-test package
25+
# test/main.test - the generated test program
26+
# test/main [test/main.test] - the test package for foo_test.go
27+
# test/main_test [test/main.test] - the test package for foo_separate_test.go
28+
#
29+
# As such, TOOLEXEC_IMPORTPATH must see the same strings, to be able to uniquely
30+
# identify each package being built as reported by 'go list -f {{.ImportPath}}'.
31+
# Note that these are not really "import paths" anymore, but that naming is
32+
# consistent with 'go list -json' at least.
33+
34+
go test -toolexec=$PWD/mytool
35+
36+
stderr -count=2 '^# test/main\.test$'
37+
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main\.test"$'
38+
stderr -count=1 '^link'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main\.test"$'
39+
40+
stderr -count=1 '^# test/main \[test/main\.test\]$'
41+
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main \[test/main\.test\]"$'
42+
43+
stderr -count=1 '^# test/main_test \[test/main\.test\]$'
44+
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main_test \[test/main\.test\]"$'
2045

2146
-- go.mod --
2247
module test/main
@@ -32,6 +57,18 @@ import (
3257
)
3358

3459
func main() {}
60+
-- foo_test.go --
61+
package main
62+
63+
import "testing"
64+
65+
func TestFoo(t *testing.T) {}
66+
-- foo_separate_test.go --
67+
package main_test
68+
69+
import "testing"
70+
71+
func TestSeparateFoo(t *testing.T) {}
3572
-- withcgo/withcgo.go --
3673
package withcgo
3774

@@ -71,7 +108,7 @@ func main() {
71108
// We can't alter the version output.
72109
} else {
73110
// Print which tool we're running, and on what package.
74-
fmt.Fprintf(os.Stdout, "%s TOOLEXEC_IMPORTPATH=%s\n", toolName, os.Getenv("TOOLEXEC_IMPORTPATH"))
111+
fmt.Fprintf(os.Stdout, "%s TOOLEXEC_IMPORTPATH=%q\n", toolName, os.Getenv("TOOLEXEC_IMPORTPATH"))
75112
}
76113

77114
// Simply run the tool.

0 commit comments

Comments
 (0)