Skip to content

Commit f9027f7

Browse files
committed
Write debug logs for autogen excluding for #86
1 parent b4bf038 commit f9027f7

File tree

9 files changed

+70
-34
lines changed

9 files changed

+70
-34
lines changed

Diff for: pkg/commands/root.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"runtime/pprof"
99

1010
"github.com/golangci/golangci-lint/pkg/config"
11+
"github.com/golangci/golangci-lint/pkg/logutils"
1112
"github.com/golangci/golangci-lint/pkg/printers"
1213
"github.com/sirupsen/logrus"
1314
"github.com/spf13/cobra"
@@ -16,7 +17,9 @@ import (
1617

1718
func setupLog(isVerbose bool) {
1819
log.SetFlags(0) // don't print time
19-
if isVerbose {
20+
if logutils.IsDebugEnabled() {
21+
logrus.SetLevel(logrus.DebugLevel)
22+
} else if isVerbose {
2023
logrus.SetLevel(logrus.InfoLevel)
2124
}
2225
}

Diff for: pkg/logutils/logutils.go

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package logutils
22

33
import (
44
"os"
5+
"strings"
56

67
"github.com/sirupsen/logrus"
78
)
@@ -15,3 +16,38 @@ func HiddenWarnf(format string, args ...interface{}) {
1516
logrus.Infof(format, args...)
1617
}
1718
}
19+
20+
func getEnabledDebugs() map[string]bool {
21+
ret := map[string]bool{}
22+
debugVar := os.Getenv("GL_DEBUG")
23+
if debugVar == "" {
24+
return ret
25+
}
26+
27+
for _, tag := range strings.Split(debugVar, ",") {
28+
ret[tag] = true
29+
}
30+
31+
return ret
32+
}
33+
34+
var enabledDebugs = getEnabledDebugs()
35+
36+
type DebugFunc func(format string, args ...interface{})
37+
38+
func nopDebugf(format string, args ...interface{}) {}
39+
40+
func Debug(tag string) DebugFunc {
41+
if !enabledDebugs[tag] {
42+
return nopDebugf
43+
}
44+
45+
return func(format string, args ...interface{}) {
46+
newArgs := append([]interface{}{tag}, args...)
47+
logrus.Debugf("%s: "+format, newArgs...)
48+
}
49+
}
50+
51+
func IsDebugEnabled() bool {
52+
return len(enabledDebugs) != 0
53+
}

Diff for: pkg/result/processors/autogenerated_exclude.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import (
77
"strings"
88

99
"github.com/golangci/golangci-lint/pkg/lint/astcache"
10+
"github.com/golangci/golangci-lint/pkg/logutils"
1011
"github.com/golangci/golangci-lint/pkg/result"
1112
)
1213

14+
var autogenDebugf = logutils.Debug("autogen_exclude")
15+
1316
type ageFileSummary struct {
1417
isGenerated bool
1518
}
@@ -62,10 +65,12 @@ func isGeneratedFileByComment(doc string) bool {
6265
doc = strings.ToLower(doc)
6366
for _, marker := range markers {
6467
if strings.Contains(doc, marker) {
68+
autogenDebugf("doc contains marker %q: file is generated", marker)
6569
return true
6670
}
6771
}
6872

73+
autogenDebugf("doc of len %d doesn't contain any of markers: %s", len(doc), markers)
6974
return false
7075
}
7176

@@ -83,27 +88,43 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
8388
return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err)
8489
}
8590

86-
doc := getDoc(f.F, f.Fset)
91+
autogenDebugf("file %q: astcache file is %+v", i.FilePath(), *f)
92+
93+
doc := getDoc(f.F, f.Fset, i.FilePath())
8794

8895
fs.isGenerated = isGeneratedFileByComment(doc)
96+
autogenDebugf("file %q is generated: %t", i.FilePath(), fs.isGenerated)
8997
return fs, nil
9098
}
9199

92-
func getDoc(f *ast.File, fset *token.FileSet) string {
100+
func getDoc(f *ast.File, fset *token.FileSet, filePath string) string {
93101
// don't use just f.Doc: e.g. mockgen leaves extra line between comment and package name
94102

95-
importPos := f.End()
103+
var importPos token.Pos
96104
if len(f.Imports) != 0 {
97105
importPos = f.Imports[0].Pos()
106+
autogenDebugf("file %q: search comments until first import pos %d (%s)", filePath, importPos, fset.Position(importPos))
107+
} else {
108+
importPos = f.End()
109+
autogenDebugf("file %q: search comments until EOF pos %d (%s)", filePath, importPos, fset.Position(importPos))
98110
}
99111

100112
var neededComments []string
101113
for _, g := range f.Comments {
102-
if g.Pos() < importPos && fset.Position(g.Pos()).Column == 1 {
103-
neededComments = append(neededComments, g.Text())
114+
pos := g.Pos()
115+
filePos := fset.Position(pos)
116+
text := g.Text()
117+
isAllowed := pos < importPos && filePos.Column == 1
118+
if isAllowed {
119+
autogenDebugf("file %q: pos=%d, filePos=%s: comment %q: it's allowed", filePath, pos, filePos, text)
120+
neededComments = append(neededComments, text)
121+
} else {
122+
autogenDebugf("file %q: pos=%d, filePos=%s: comment %q: it's NOT allowed", filePath, pos, filePos, text)
104123
}
105124
}
106125

126+
autogenDebugf("file %q: got %d allowed comments", filePath, len(neededComments))
127+
107128
if len(neededComments) == 0 {
108129
return ""
109130
}

Diff for: pkg/result/processors/autogenerated_exclude_test.go

-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/result/processors/testdata/autogenerated.go renamed to test/testdata/autogenerated/autogenerated.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

Diff for: pkg/result/processors/testdata/autogenerated_alt_hdr2.go renamed to test/testdata/autogenerated/go_bindata.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: test/testdata/autogenerated/mockgen.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/result/processors/testdata/autogenerated_alt_hdr.go renamed to test/testdata/autogenerated/protoc_gen_foo.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)