Skip to content

Commit 7e6e502

Browse files
committed
cmd/go: fix 'go vet' of package with external tests
For example, fixes 'go vet syscall', which has source files in package syscall_test. Fixes #8511. LGTM=r R=golang-codereviews, r CC=golang-codereviews, iant https://golang.org/cl/152220044
1 parent 9a5b055 commit 7e6e502

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

src/cmd/go/test.bash

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,20 @@ fi
10851085
unset GOPATH
10861086
rm -rf $d
10871087

1088+
TEST go vet with external tests
1089+
d=$(mktemp -d -t testgoXXX)
1090+
export GOPATH=$(pwd)/testdata
1091+
if ./testgo vet vetpkg >$d/err 2>&1; then
1092+
echo "go vet vetpkg passes incorrectly"
1093+
ok=false
1094+
elif ! grep -q 'missing argument for Printf' $d/err; then
1095+
echo "go vet vetpkg did not find missing argument for Printf"
1096+
cat $d/err
1097+
ok=false
1098+
fi
1099+
unset GOPATH
1100+
rm -rf $d
1101+
10881102
# clean up
10891103
if $started; then stop; fi
10901104
rm -rf testdata/bin testdata/bin1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package p_test

src/cmd/go/testdata/src/vetpkg/b.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package p
2+
3+
import "fmt"
4+
5+
func f() {
6+
fmt.Printf("%d")
7+
}

src/cmd/go/vet.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package main
66

7+
import "path/filepath"
8+
79
func init() {
810
addBuildFlagsNX(cmdVet)
911
}
@@ -28,10 +30,21 @@ See also: go fmt, go fix.
2830
}
2931

3032
func runVet(cmd *Command, args []string) {
31-
for _, pkg := range packages(args) {
32-
// Use pkg.gofiles instead of pkg.Dir so that
33-
// the command only applies to this package,
34-
// not to packages in subdirectories.
35-
run(tool("vet"), relPaths(stringList(pkg.gofiles, pkg.sfiles)))
33+
for _, p := range packages(args) {
34+
// Vet expects to be given a set of files all from the same package.
35+
// Run once for package p and once for package p_test.
36+
if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles) > 0 {
37+
runVetFiles(p, stringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.SFiles))
38+
}
39+
if len(p.XTestGoFiles) > 0 {
40+
runVetFiles(p, stringList(p.XTestGoFiles))
41+
}
42+
}
43+
}
44+
45+
func runVetFiles(p *Package, files []string) {
46+
for i := range files {
47+
files[i] = filepath.Join(p.Dir, files[i])
3648
}
49+
run(tool("vet"), relPaths(files))
3750
}

0 commit comments

Comments
 (0)