Skip to content

Commit 5a6a3df

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 5a6a3df

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,8 +1300,15 @@ func disallowInternal(srcDir string, importer *Package, importerPath string, p *
13001300

13011301
// Internal is present, and srcDir is outside parent's tree. Not allowed.
13021302
perr := *p
1303+
1304+
// The stack in the error should point to the importing package,
1305+
// so pop off the last stack frame.
1306+
importStack := stk.Copy()
1307+
if len(importStack) > 0 {
1308+
importStack = importStack[:len(importStack)-1]
1309+
}
13031310
perr.Error = &PackageError{
1304-
ImportStack: stk.Copy(),
1311+
ImportStack: importStack,
13051312
Err: ImportErrorf(p.ImportPath, "use of internal package "+p.ImportPath+" not allowed"),
13061313
}
13071314
perr.Incomplete = true

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ 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+
isDirect := false
59+
60+
// The package with the error has the highest index in the stack.
61+
if len(perr.ImportStack) >= 1 {
62+
importer := strings.TrimSuffix(perr.ImportStack[len(perr.ImportStack)-1], " (test)")
63+
isDirect = importer == p1.ImportPath
64+
}
65+
66+
if !isDirect {
67+
// Remove the position information, so that we'll
68+
// print the full import stack to the error.
69+
perr.Pos = ""
70+
}
5971
err = perr
6072
break
6173
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
! go test testdep/p1
22
stderr 'package testdep/p1 \(test\)\n\timports testdep/p2\n\timports testdep/p3: build constraints exclude all Go files ' # check for full import stack
33

4+
! go vet testdep/p1
5+
stderr 'package testdep/p1 \(test\)\n\timports testdep/p2\n\timports testdep/p3: build constraints exclude all Go files ' # check for full import stack
6+
47
-- testdep/p1/p1.go --
58
package p1
69
-- testdep/p1/p1_test.go --
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
env GO111MODULE=off
2+
3+
# Issue 36173. Verify that "go vet" prints line numbers on load errors.
4+
5+
! go vet a/a.go
6+
stderr 'a.go:5:3: use of internal package'
7+
8+
! go vet a/a_test.go
9+
stderr 'a_test.go:4:3: use of internal package'
10+
11+
! go vet a
12+
stderr 'a.go:5:3: use of internal package'
13+
14+
go vet b/b.go
15+
! stderr 'use of internal package'
16+
17+
! go vet b/b_test.go
18+
stderr 'b_test.go:4:3: use of internal package'
19+
20+
! go vet depends-on-a/depends-on-a.go
21+
stderr 'a.go:5:3: use of internal package'
22+
23+
! go vet depends-on-a/depends-on-a_test.go
24+
stderr 'package command-line-arguments \(test\)\n\timports a: use of internal package a/x/internal/y not allowed'
25+
26+
! go vet depends-on-a
27+
stderr 'a.go:5:3: use of internal package'
28+
29+
-- a/a.go --
30+
// A package with bad imports in both src and test
31+
package a
32+
33+
import (
34+
_ "a/x/internal/y"
35+
)
36+
37+
-- a/a_test.go --
38+
package a
39+
40+
import (
41+
_ "a/x/internal/y"
42+
)
43+
44+
-- b/b.go --
45+
// A package with a bad import in test only
46+
package b
47+
48+
-- b/b_test.go --
49+
package b
50+
51+
import (
52+
_ "a/x/internal/y"
53+
)
54+
55+
-- depends-on-a/depends-on-a.go --
56+
// A package that depends on a package with a bad import
57+
package depends
58+
59+
import (
60+
_ "a"
61+
)
62+
63+
-- depends-on-a/depends-on-a_test.go --
64+
package depends
65+
66+
import (
67+
_ "a"
68+
)
69+
70+
-- a/x/internal/y/y.go --
71+
package y

0 commit comments

Comments
 (0)