diff options
author | Timothy Gu <timothygu99@gmail.com> | 2021-05-14 13:06:12 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-14 16:06:12 -0400 |
commit | 0582543d742132e9bd911473d88fb3db8ddbfa93 (patch) | |
tree | e5965eb4059dad42b9028fa458333eac7b46a1c6 | |
parent | 71042b949a84ae275660f45b8da9499a5877bb9c (diff) | |
download | bazelbuild-rules_go-0582543d742132e9bd911473d88fb3db8ddbfa93.tar.gz |
nogo: check adjusted filename for inclusion/exclusion (#2863)
Go files could include `//line` or `/*line` comments that indicate the
original location of the line. This functionality is used, among other
things, cgo's code rewrite step. This commit changes the
include/exclude_files logic to use the adjusted location in order to
better align with user expectations.
Fixes #2862
-rw-r--r-- | go/tools/builders/nogo_main.go | 6 | ||||
-rw-r--r-- | tests/core/nogo/custom/custom_test.go | 52 |
2 files changed, 52 insertions, 6 deletions
diff --git a/go/tools/builders/nogo_main.go b/go/tools/builders/nogo_main.go index c248282e..631cf0e5 100644 --- a/go/tools/builders/nogo_main.go +++ b/go/tools/builders/nogo_main.go @@ -394,10 +394,10 @@ func checkAnalysisResults(actions []*action, pkg *goPackage) string { for _, d := range act.diagnostics { // NOTE(golang.org/issue/31008): nilness does not set positions, // so don't assume the position is valid. - f := pkg.fset.File(d.Pos) + p := pkg.fset.Position(d.Pos) filename := "-" - if f != nil { - filename = f.Name() + if p.IsValid() { + filename = p.Filename } include := true if len(config.onlyFiles) > 0 { diff --git a/tests/core/nogo/custom/custom_test.go b/tests/core/nogo/custom/custom_test.go index 2221e508..3f8f077b 100644 --- a/tests/core/nogo/custom/custom_test.go +++ b/tests/core/nogo/custom/custom_test.go @@ -79,6 +79,13 @@ go_library( ) go_library( + name = "has_errors_linedirective", + srcs = ["has_errors_linedirective.go"], + importpath = "haserrors_linedirective", + deps = [":dep"], +) + +go_library( name = "no_errors", srcs = ["no_errors.go"], importpath = "noerrors", @@ -290,8 +297,25 @@ import ( ) func Foo() bool { // This should fail foofuncname - dep.D() // This should fail visibility - return true // This should fail boolreturn + dep.D() // This should fail visibility + return true +} + +-- has_errors_linedirective.go -- +//line linedirective.go:1 +package haserrors_linedirective + +import ( + /*line linedirective_importfmt.go:4*/ _ "fmt" // This should fail importfmt + + "dep" +) + +//line linedirective_foofuncname.go:9 +func Foo() bool { // This should fail foofuncname +//line linedirective_visibility.go:10 + dep.D() // This should fail visibility + return true } -- no_errors.go -- @@ -332,7 +356,17 @@ func Test(t *testing.T) { `has_errors.go:.*function D is not visible in this package \(visibility\)`, }, }, { + desc: "default_config_linedirective", + target: "//:has_errors_linedirective", + wantSuccess: false, + includes: []string{ + `linedirective_importfmt.go:.*package fmt must not be imported \(importfmt\)`, + `linedirective_foofuncname.go:.*function must not be named Foo \(foofuncname\)`, + `linedirective_visibility.go:.*function D is not visible in this package \(visibility\)`, + }, + }, { desc: "custom_config", + config: "config.json", target: "//:has_errors", wantSuccess: false, includes: []string{ @@ -340,7 +374,19 @@ func Test(t *testing.T) { `has_errors.go:.*function must not be named Foo \(foofuncname\)`, }, excludes: []string{ - "custom/has_errors.go:.*function D is not visible in this package", + `visib`, + }, + }, { + desc: "custom_config_linedirective", + config: "config.json", + target: "//:has_errors_linedirective", + wantSuccess: false, + includes: []string{ + `linedirective_foofuncname.go:.*function must not be named Foo \(foofuncname\)`, + `linedirective_visibility.go:.*function D is not visible in this package \(visibility\)`, + }, + excludes: []string{ + `importfmt`, }, }, { desc: "no_errors", |