Skip to content

Commit c4cdc33

Browse files
committed
cmd/go/internal/vet: print line numbers appropriately on list errors
Fixes #36173 For reasons that are unclear to me, this commit: f1d5ce0 introduces a TestPackagesFor function that strips line numbers from error messages. This commit introduces a new version of that function for 'go vet' that always keeps the line numbers.
1 parent 753d56d commit c4cdc33

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ func (e *importError) ImportPath() string {
395395
// An ImportStack is a stack of import paths, possibly with the suffix " (test)" appended.
396396
// The import path of a test package is the import path of the corresponding
397397
// non-test package with the suffix "_test" added.
398+
// Sometimes the first entry will be "command-line-arguments" to indicate
399+
// an import stack relative to the command-line working directory.
398400
type ImportStack []string
399401

400402
func (s *ImportStack) Push(p string) {

src/cmd/go/internal/load/test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ func TestPackagesFor(p *Package, cover *TestCover) (pmain, ptest, pxtest *Packag
5555
}
5656
if len(p1.DepsErrors) > 0 {
5757
perr := p1.DepsErrors[0]
58-
perr.Pos = "" // show full import stack
58+
// A typical ImportStack looks like
59+
// [ "command-line-arguments (test)", "pkg/with/error" ]
60+
// or
61+
// [ "command-line-arguments (test)", "parent/pkg", "pkg/with/error" ]
62+
// so the package with the error has the highest index. So we want
63+
// to check the second highest index to see if we should print the full stack.
64+
direct := len(perr.ImportStack) < 2 ||
65+
(perr.ImportStack[len(perr.ImportStack)-2] == p1.ImportPath ||
66+
strings.HasPrefix(perr.ImportStack[len(perr.ImportStack)-2], "command-line-arguments"))
67+
if !direct {
68+
perr.Pos = "" // show full import stack
69+
}
5970
err = perr
6071
break
6172
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
env GO111MODULE=off
2+
3+
# Issue 36173. Verify that "go vet" prints line numbers on load errors.
4+
5+
! go vet a/a_test.go
6+
stderr 'a_test.go:4:3: use of internal package'
7+
8+
-- a/a.go --
9+
package a
10+
11+
-- a/a_test.go --
12+
package a
13+
14+
import (
15+
_ "a/x/internal/y"
16+
)
17+
18+
-- a/x/internal/y/y.go --
19+
package y

0 commit comments

Comments
 (0)