Skip to content

Commit 6480aa8

Browse files
authored
Merge pull request #89 from golangci/support/fix-mockgen-detecting
Write debug logs for autogen excluding for #86
2 parents 6e12ea5 + f9027f7 commit 6480aa8

File tree

12 files changed

+93
-53
lines changed

12 files changed

+93
-53
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test:
2-
go install -i ./cmd/... # needed for govet and golint
2+
go install ./cmd/... # needed for govet and golint
33
golangci-lint run -v
44
golangci-lint run --fast --no-config -v
55
golangci-lint run --no-config -v

README.md

+11-9
Original file line numberDiff line numberDiff line change
@@ -371,18 +371,18 @@ nolint comment, diff, regexps; prettify paths, etc.
371371
We use `cobra` for command-line options.
372372

373373
# FAQ
374-
**Q: How do you add a custom linter?**
374+
**How do you add a custom linter?**
375375

376-
A: You can integrate it yourself, see this [wiki page](https://github.com/golangci/golangci-lint/wiki/How-to-add-a-custom-linter) with documentation. Or you can create a [GitHub Issue](https://github.com/golangci/golangci-lint/issues/new) and we will integrate when time permits.
376+
You can integrate it yourself, see this [wiki page](https://github.com/golangci/golangci-lint/wiki/How-to-add-a-custom-linter) with documentation. Or you can create a [GitHub Issue](https://github.com/golangci/golangci-lint/issues/new) and we will integrate when time permits.
377377

378-
**Q: It's cool to use `golangci-lint` when starting a project, but what about existing projects with large codebase? It will take days to fix all found issues**
378+
**It's cool to use `golangci-lint` when starting a project, but what about existing projects with large codebase? It will take days to fix all found issues**
379379

380-
A: We are sure that every project can easily integrate `golangci-lint`, even the large one. The idea is to not fix all existing issues. Fix only newly added issue: issues in new code. To do this setup CI (or better use [GolangCI](https://golangci.com) to run `golangci-lint` with option `--new-from-rev=origin/master`. Also, take a look at option `-n`.
380+
We are sure that every project can easily integrate `golangci-lint`, even the large one. The idea is to not fix all existing issues. Fix only newly added issue: issues in new code. To do this setup CI (or better use [GolangCI](https://golangci.com) to run `golangci-lint` with option `--new-from-rev=origin/master`. Also, take a look at option `-n`.
381381
By doing this you won't create new issues in your code and can choose fix existing issues (or not).
382382

383-
**Q: How to use `golangci-lint` in CI (Continuous Integration)?**
383+
**How to use `golangci-lint` in CI (Continuous Integration)?**
384384

385-
A: You have 2 choices:
385+
You have 2 choices:
386386
1. Use [GolangCI](https://golangci.com): this service is highly integrated with GitHub (issues are commented in the pull request) and uses a `golangci-lint` tool. For configuration use `.golangci.yml` (or toml/json).
387387
2. Use custom CI: just run `golangci-lint` in CI and check the exit code. If it's non-zero - fail the build. The main disadvantage is that you can't see issues in pull request code and would need to view the build log, then open the referenced source file to see the context.
388388
If you'd like to vendor `golangci-lint` in your repo, run:
@@ -403,12 +403,14 @@ go install ./vendor/github.com/golangci/golangci-lint/cmd/golangci-lint/`
403403
```
404404
Vendoring `golangci-lint` saves a network request, potentially making your CI system a little more reliable.
405405

406-
**Q: `govet` or `golint` reports false-positives or false-negatives**
406+
**`govet` or `golint` reports false-positives or false-negatives**
407+
407408
These linters obtain type information from installed package files.
408409
The same issue you will encounter if will run `govet` or `golint` without `golangci-lint`.
409-
Try `go install -i ./...` (or `go install -i ./cmd/...`: depends on the project) first.
410+
Try `go install ./...` (or `go install ./cmd/...`: depends on the project) first.
411+
412+
**`golangci-lint` doesn't work**
410413

411-
**Q: `golangci-lint` doesn't work**
412414
1. Update it: `go get -u github.com/golangci/golangci-lint/cmd/golangci-lint`
413415
2. Run it with `-v` option and check the output.
414416
3. If it doesn't help create a [GitHub issue](https://github.com/golangci/golangci-lint/issues/new) with the output from the error and #2 above.

README.md.tmpl

+11-9
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,18 @@ nolint comment, diff, regexps; prettify paths, etc.
243243
We use `cobra` for command-line options.
244244

245245
# FAQ
246-
**Q: How do you add a custom linter?**
246+
**How do you add a custom linter?**
247247

248-
A: You can integrate it yourself, see this [wiki page](https://github.com/golangci/golangci-lint/wiki/How-to-add-a-custom-linter) with documentation. Or you can create a [GitHub Issue](https://github.com/golangci/golangci-lint/issues/new) and we will integrate when time permits.
248+
You can integrate it yourself, see this [wiki page](https://github.com/golangci/golangci-lint/wiki/How-to-add-a-custom-linter) with documentation. Or you can create a [GitHub Issue](https://github.com/golangci/golangci-lint/issues/new) and we will integrate when time permits.
249249

250-
**Q: It's cool to use `golangci-lint` when starting a project, but what about existing projects with large codebase? It will take days to fix all found issues**
250+
**It's cool to use `golangci-lint` when starting a project, but what about existing projects with large codebase? It will take days to fix all found issues**
251251

252-
A: We are sure that every project can easily integrate `golangci-lint`, even the large one. The idea is to not fix all existing issues. Fix only newly added issue: issues in new code. To do this setup CI (or better use [GolangCI](https://golangci.com) to run `golangci-lint` with option `--new-from-rev=origin/master`. Also, take a look at option `-n`.
252+
We are sure that every project can easily integrate `golangci-lint`, even the large one. The idea is to not fix all existing issues. Fix only newly added issue: issues in new code. To do this setup CI (or better use [GolangCI](https://golangci.com) to run `golangci-lint` with option `--new-from-rev=origin/master`. Also, take a look at option `-n`.
253253
By doing this you won't create new issues in your code and can choose fix existing issues (or not).
254254

255-
**Q: How to use `golangci-lint` in CI (Continuous Integration)?**
255+
**How to use `golangci-lint` in CI (Continuous Integration)?**
256256

257-
A: You have 2 choices:
257+
You have 2 choices:
258258
1. Use [GolangCI](https://golangci.com): this service is highly integrated with GitHub (issues are commented in the pull request) and uses a `golangci-lint` tool. For configuration use `.golangci.yml` (or toml/json).
259259
2. Use custom CI: just run `golangci-lint` in CI and check the exit code. If it's non-zero - fail the build. The main disadvantage is that you can't see issues in pull request code and would need to view the build log, then open the referenced source file to see the context.
260260
If you'd like to vendor `golangci-lint` in your repo, run:
@@ -275,12 +275,14 @@ go install ./vendor/github.com/golangci/golangci-lint/cmd/golangci-lint/`
275275
```
276276
Vendoring `golangci-lint` saves a network request, potentially making your CI system a little more reliable.
277277

278-
**Q: `govet` or `golint` reports false-positives or false-negatives**
278+
**`govet` or `golint` reports false-positives or false-negatives**
279+
279280
These linters obtain type information from installed package files.
280281
The same issue you will encounter if will run `govet` or `golint` without `golangci-lint`.
281-
Try `go install -i ./...` (or `go install -i ./cmd/...`: depends on the project) first.
282+
Try `go install ./...` (or `go install ./cmd/...`: depends on the project) first.
283+
284+
**`golangci-lint` doesn't work**
282285

283-
**Q: `golangci-lint` doesn't work**
284286
1. Update it: `go get -u github.com/golangci/golangci-lint/cmd/golangci-lint`
285287
2. Run it with `-v` option and check the output.
286288
3. If it doesn't help create a [GitHub issue](https://github.com/golangci/golangci-lint/issues/new) with the output from the error and #2 above.

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
}

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+
}

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
}

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.

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.

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.

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.

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)