Skip to content

Commit

Permalink
cmd/go/internal/vet: print line numbers appropriately on list errors
Browse files Browse the repository at this point in the history
Fixes golang#36173

For reasons that are unclear to me, this commit:
golang@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.
  • Loading branch information
nicks committed Feb 11, 2020
1 parent 753d56d commit 6f31565
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/cmd/go/internal/load/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,27 @@ func TestPackagesFor(p *Package, cover *TestCover) (pmain, ptest, pxtest *Packag
}
if len(p1.DepsErrors) > 0 {
perr := p1.DepsErrors[0]
perr.Pos = "" // show full import stack
isDirect := false

// If this is an indirect error in a package we depend on,
// we want to print the full import stack.
// We do this by removing the position information from the error.
if len(perr.ImportStack) >= 1 {
errPath := perr.ImportStack[len(perr.ImportStack)-1]
if _, ok := perr.Err.(ImportPathError); ok {
// ImportErrors are special: their stack trace includes the imported package,
// but the error should be associated with the importing package.
errPath = perr.ImportStack[len(perr.ImportStack)-2]
}

errPath = strings.TrimSuffix(errPath, " (test)")

isDirect = errPath == p1.ImportPath
}

if !isDirect {
perr.Pos = ""
}
err = perr
break
}
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/go/testdata/script/test_import_error_stack.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
! go test testdep/p1
stderr 'package testdep/p1 \(test\)\n\timports testdep/p2\n\timports testdep/p3: build constraints exclude all Go files ' # check for full import stack

! go vet testdep/p1
stderr 'package testdep/p1 \(test\)\n\timports testdep/p2\n\timports testdep/p3: build constraints exclude all Go files ' # check for full import stack

-- testdep/p1/p1.go --
package p1
-- testdep/p1/p1_test.go --
Expand Down
71 changes: 71 additions & 0 deletions src/cmd/go/testdata/script/vet_internal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
env GO111MODULE=off

# Issue 36173. Verify that "go vet" prints line numbers on load errors.

! go vet a/a.go
stderr 'a.go:5:3: use of internal package'

! go vet a/a_test.go
stderr 'a_test.go:4:3: use of internal package'

! go vet a
stderr 'a.go:5:3: use of internal package'

go vet b/b.go
! stderr 'use of internal package'

! go vet b/b_test.go
stderr 'b_test.go:4:3: use of internal package'

! go vet depends-on-a/depends-on-a.go
stderr 'a.go:5:3: use of internal package'

! go vet depends-on-a/depends-on-a_test.go
stderr 'package command-line-arguments \(test\)\n\timports a: use of internal package a/x/internal/y not allowed'

! go vet depends-on-a
stderr 'a.go:5:3: use of internal package'

-- a/a.go --
// A package with bad imports in both src and test
package a

import (
_ "a/x/internal/y"
)

-- a/a_test.go --
package a

import (
_ "a/x/internal/y"
)

-- b/b.go --
// A package with a bad import in test only
package b

-- b/b_test.go --
package b

import (
_ "a/x/internal/y"
)

-- depends-on-a/depends-on-a.go --
// A package that depends on a package with a bad import
package depends

import (
_ "a"
)

-- depends-on-a/depends-on-a_test.go --
package depends

import (
_ "a"
)

-- a/x/internal/y/y.go --
package y

0 comments on commit 6f31565

Please sign in to comment.