Skip to content

Commit 5514c43

Browse files
committed
Fix #17, #87: govet becomes SLOW linter by default
1. Allow govet to work in 2 modes: fast and slow. Default is slow. In fast mode golangci-lint runs `go install -i` and `go test -i` for analyzed packages. But it's fast only when: - go >= 1.10 - it's repeated run or $GOPATH/pkg or `go env GOCACHE` is cached between CI builds In slow mode we load program from source code like for another linters and do it only once for all linters. 3. Patch govet code to warn about any troubles with the type information. Default behaviour of govet was to hide such warnings. Fail analysis if there are any troubles with type loading: it will prevent false-positives and false-negatives from govet. 4. Describe almost all options in .golangci.example.yml and include it into README. Describe when to use slow or fast mode of govet. 5. Speed up govet: reuse AST parsing: it's already parsed once by golangci-lint. For "slow" runs (when we run at least one slow linter) speedup by not loading type information second time. 6. Improve logging, debug logging 7. Fix crash in logging of AST cache warnings (#118)
1 parent f239b80 commit 5514c43

File tree

22 files changed

+721
-159
lines changed

22 files changed

+721
-159
lines changed

.golangci.example.yml

+88-6
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,98 @@
1+
# This file contains all available configuration options
2+
# with their default values.
3+
4+
# options for analysis running
15
run:
6+
# default concurrency is a available CPU number
27
concurrency: 4
8+
9+
# timeout for analysis, e.g. 30s, 5m, default is 1m
310
deadline: 1m
11+
12+
# exit code when at least one issue was found, default is 1
413
issues-exit-code: 1
14+
15+
# include test files or not, default is true
516
tests: true
17+
18+
# list of build tags, all linters use it. Default is empty list.
619
build-tags:
720
- mytag
21+
22+
# which dirs to skip: they won't be analyzed;
23+
# can use regexp here: generated.*, regexp is applied on full path;
24+
# default value is empty list, but next dirs are always skipped independently
25+
# from this option's value:
26+
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
827
skip-dirs:
928
- src/external_libs
1029
- autogenerated_by_my_lib
30+
31+
# which files to skip: they will be analyzed, but issues from them
32+
# won't be reported. Default value is empty list, but there is
33+
# no need to include all autogenerated files, we confidently recognize
34+
# autogenerated files. If it's not please let us know.
1135
skip-files:
12-
- ".*\\.pb\\.go$"
36+
- ".*\\.my\\.go$"
1337
- lib/bad.go
1438

39+
# output configuration options
1540
output:
41+
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
1642
format: colored-line-number
43+
44+
# print lines of code with issue, default is true
1745
print-issued-lines: true
46+
47+
# print linter name in the end of issue text, default is true
1848
print-linter-name: true
1949

50+
# all available settings of specific linters
2051
linters-settings:
2152
errcheck:
53+
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
54+
# default is false: such cases aren't reported by default.
2255
check-type-assertions: false
56+
57+
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
58+
# default is false: such cases aren't reported by default.
2359
check-blank: false
2460
govet:
61+
# report about shadowed variables
2562
check-shadowing: true
63+
64+
# Obtain type information from installed (to $GOPATH/pkg) package files:
65+
# golangci-lint will execute `go install -i` and `go test -i` for analyzed packages
66+
# before analyzing them.
67+
# By default this option is disabled and govet gets type information by loader from source code.
68+
# Loading from source code is slow, but it's done only once for all linters.
69+
# Go-installing of packages first time is much slower than loading them from source code,
70+
# therefore this option is disabled by default.
71+
# But repeated installation is fast in go >= 1.10 because of build caching.
72+
# Enable this option only if all conditions are met:
73+
# 1. you use only "fast" linters (--fast e.g.): no program loading occurs
74+
# 2. you use go >= 1.10
75+
# 3. you do repeated runs (false for CI) or cache $GOPATH/pkg or `go env GOCACHE` dir in CI.
76+
use-installed-packages: false
2677
golint:
78+
# minimal confidence for issues, default is 0.8
2779
min-confidence: 0.8
2880
gofmt:
81+
# simplify code: gofmt with `-s` option, true by default
2982
simplify: true
3083
gocyclo:
84+
# minimal code complexity to report, 30 by default (but we recommend 10-20)
3185
min-complexity: 10
3286
maligned:
87+
# print struct with more effective memory layout or not, false by default
3388
suggest-new: true
3489
dupl:
35-
threshold: 50
90+
# tokens count to trigger issue, 150 by default
91+
threshold: 100
3692
goconst:
93+
# minimal length of string constant, 3 by default
3794
min-len: 3
95+
# minimal occurrences count to trigger, 3 by default
3896
min-occurrences: 3
3997
depguard:
4098
list-type: blacklist
@@ -45,8 +103,8 @@ linters-settings:
45103
linters:
46104
enable:
47105
- megacheck
48-
- vet
49-
enable-all: true
106+
- govet
107+
enable-all: false
50108
disable:
51109
maligned
52110
disable-all: false
@@ -56,11 +114,35 @@ linters:
56114
fast: false
57115

58116
issues:
117+
# List of regexps of issue texts to exclude, empty list by default.
118+
# But independently from this option we use default exclude patterns,
119+
# it can be disabled by `exclude-use-default: false`. To list all
120+
# excluded by default patterns execute `golangci-lint run --help`
59121
exclude:
60122
- abcdef
123+
124+
# Independently from option `exclude` we use default exclude patterns,
125+
# it can be disabled by this option. To list all
126+
# excluded by default patterns execute `golangci-lint run --help`.
127+
# Default value for this option is false.
61128
exclude-use-default: true
129+
130+
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
62131
max-per-linter: 0
132+
133+
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
63134
max-same: 0
135+
136+
# Show only new issues: if there are unstaged changes or untracked files,
137+
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
138+
# It's a super-useful option for integration of golangci-lint into existing
139+
# large codebase. It's not practical to fix all existing issues at the moment
140+
# of integration: much better don't allow issues in new code.
141+
# Default is false.
64142
new: false
65-
new-from-rev: ""
66-
new-from-patch: ""
143+
144+
# Show only new issues created after git revision `REV`
145+
new-from-rev: REV
146+
147+
# Show only new issues created in git patch with set file path.
148+
new-from-patch: path/to/patch/file

Gopkg.lock

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

Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
test:
2-
go install ./cmd/... # needed for govet and golint if go < 1.10
32
GL_TEST_RUN=1 golangci-lint run -v
43
GL_TEST_RUN=1 golangci-lint run --fast --no-config -v
54
GL_TEST_RUN=1 golangci-lint run --no-config -v

README.md

+158-7
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ GolangCI-Lint can be used with zero configuration. By default the following lint
8484
```
8585
$ golangci-lint linters
8686
Enabled by default linters:
87-
govet: Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: true]
87+
govet: Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false]
8888
errcheck: Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases [fast: false]
8989
staticcheck: Staticcheck is a go vet on steroids, applying a ton of static analysis checks [fast: false]
9090
unused: Checks Go code for unused constants, variables, functions and types [fast: false]
@@ -311,10 +311,161 @@ To see which config file is being used and where it was sourced from run golangc
311311
Config options inside the file are identical to command-line options.
312312
You can configure specific linters' options only within the config file (not the command-line).
313313

314-
There is a [`.golangci.yml`](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml) example config file with all supported options.
314+
There is a [`.golangci.yml`](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml) example
315+
config file with all supported options, their description and default value:
316+
```yaml
317+
# This file contains all available configuration options
318+
# with their default values.
319+
320+
# options for analysis running
321+
run:
322+
# default concurrency is a available CPU number
323+
concurrency: 4
324+
325+
# timeout for analysis, e.g. 30s, 5m, default is 1m
326+
deadline: 1m
327+
328+
# exit code when at least one issue was found, default is 1
329+
issues-exit-code: 1
330+
331+
# include test files or not, default is true
332+
tests: true
333+
334+
# list of build tags, all linters use it. Default is empty list.
335+
build-tags:
336+
- mytag
337+
338+
# which dirs to skip: they won't be analyzed;
339+
# can use regexp here: generated.*, regexp is applied on full path;
340+
# default value is empty list, but next dirs are always skipped independently
341+
# from this option's value:
342+
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
343+
skip-dirs:
344+
- src/external_libs
345+
- autogenerated_by_my_lib
346+
347+
# which files to skip: they will be analyzed, but issues from them
348+
# won't be reported. Default value is empty list, but there is
349+
# no need to include all autogenerated files, we confidently recognize
350+
# autogenerated files. If it's not please let us know.
351+
skip-files:
352+
- ".*\\.my\\.go$"
353+
- lib/bad.go
354+
355+
# output configuration options
356+
output:
357+
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
358+
format: colored-line-number
359+
360+
# print lines of code with issue, default is true
361+
print-issued-lines: true
362+
363+
# print linter name in the end of issue text, default is true
364+
print-linter-name: true
365+
366+
# all available settings of specific linters
367+
linters-settings:
368+
errcheck:
369+
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
370+
# default is false: such cases aren't reported by default.
371+
check-type-assertions: false
372+
373+
# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
374+
# default is false: such cases aren't reported by default.
375+
check-blank: false
376+
govet:
377+
# report about shadowed variables
378+
check-shadowing: true
379+
380+
# Obtain type information from installed (to $GOPATH/pkg) package files:
381+
# golangci-lint will execute `go install -i` and `go test -i` for analyzed packages
382+
# before analyzing them.
383+
# By default this option is disabled and govet gets type information by loader from source code.
384+
# Loading from source code is slow, but it's done only once for all linters.
385+
# Go-installing of packages first time is much slower than loading them from source code,
386+
# therefore this option is disabled by default.
387+
# But repeated installation is fast in go >= 1.10 because of build caching.
388+
# Enable this option only if all conditions are met:
389+
# 1. you use only "fast" linters (--fast e.g.): no program loading occurs
390+
# 2. you use go >= 1.10
391+
# 3. you do repeated runs (false for CI) or cache $GOPATH/pkg or `go env GOCACHE` dir in CI.
392+
use-installed-packages: false
393+
golint:
394+
# minimal confidence for issues, default is 0.8
395+
min-confidence: 0.8
396+
gofmt:
397+
# simplify code: gofmt with `-s` option, true by default
398+
simplify: true
399+
gocyclo:
400+
# minimal code complexity to report, 30 by default (but we recommend 10-20)
401+
min-complexity: 10
402+
maligned:
403+
# print struct with more effective memory layout or not, false by default
404+
suggest-new: true
405+
dupl:
406+
# tokens count to trigger issue, 150 by default
407+
threshold: 100
408+
goconst:
409+
# minimal length of string constant, 3 by default
410+
min-len: 3
411+
# minimal occurrences count to trigger, 3 by default
412+
min-occurrences: 3
413+
depguard:
414+
list-type: blacklist
415+
include-go-root: false
416+
packages:
417+
- github.com/davecgh/go-spew/spew
418+
419+
linters:
420+
enable:
421+
- megacheck
422+
- govet
423+
enable-all: false
424+
disable:
425+
maligned
426+
disable-all: false
427+
presets:
428+
- bugs
429+
- unused
430+
fast: false
431+
432+
issues:
433+
# List of regexps of issue texts to exclude, empty list by default.
434+
# But independently from this option we use default exclude patterns,
435+
# it can be disabled by `exclude-use-default: false`. To list all
436+
# excluded by default patterns execute `golangci-lint run --help`
437+
exclude:
438+
- abcdef
439+
440+
# Independently from option `exclude` we use default exclude patterns,
441+
# it can be disabled by this option. To list all
442+
# excluded by default patterns execute `golangci-lint run --help`.
443+
# Default value for this option is false.
444+
exclude-use-default: true
445+
446+
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
447+
max-per-linter: 0
448+
449+
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
450+
max-same: 0
451+
452+
# Show only new issues: if there are unstaged changes or untracked files,
453+
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
454+
# It's a super-useful option for integration of golangci-lint into existing
455+
# large codebase. It's not practical to fix all existing issues at the moment
456+
# of integration: much better don't allow issues in new code.
457+
# Default is false.
458+
new: false
459+
460+
# Show only new issues created after git revision `REV`
461+
new-from-rev: REV
462+
463+
# Show only new issues created in git patch with set file path.
464+
new-from-patch: path/to/patch/file
465+
```
315466
316467
It's a [.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml) config file of this repo: we enable more linters
317-
than the default and more strict settings:
468+
than the default and have more strict settings:
318469
```yaml
319470
linters-settings:
320471
govet:
@@ -410,11 +561,11 @@ go install ./vendor/github.com/golangci/golangci-lint/cmd/golangci-lint/
410561
```
411562
Vendoring `golangci-lint` saves a network request, potentially making your CI system a little more reliable.
412563

413-
**`govet` or `golint` reports false-positives or false-negatives**
564+
**Does I need to run `go install`?**
414565

415-
These linters obtain type information from installed package files.
416-
The same issue you will encounter if will run `govet` or `golint` without `golangci-lint`.
417-
Try `go install ./...` (or `go install ./cmd/...`: depends on the project) first.
566+
No, you don't need to do it anymore. We will run `go install -i` and `go test -i`
567+
for analyzed packages ourselves. We will run them only
568+
if option `govet.use-installed-packages` is `true`.
418569

419570
**`golangci-lint` doesn't work**
420571

README.md.tmpl

+10-6
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,14 @@ To see which config file is being used and where it was sourced from run golangc
200200
Config options inside the file are identical to command-line options.
201201
You can configure specific linters' options only within the config file (not the command-line).
202202

203-
There is a [`.golangci.yml`](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml) example config file with all supported options.
203+
There is a [`.golangci.yml`](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml) example
204+
config file with all supported options, their description and default value:
205+
```yaml
206+
{{.GolangciYamlExample}}
207+
```
204208

205209
It's a [.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml) config file of this repo: we enable more linters
206-
than the default and more strict settings:
210+
than the default and have more strict settings:
207211
```yaml
208212
{{.GolangciYaml}}
209213
```
@@ -275,11 +279,11 @@ go install ./vendor/github.com/golangci/golangci-lint/cmd/golangci-lint/
275279
```
276280
Vendoring `golangci-lint` saves a network request, potentially making your CI system a little more reliable.
277281

278-
**`govet` or `golint` reports false-positives or false-negatives**
282+
**Does I need to run `go install`?**
279283

280-
These linters obtain type information from installed package files.
281-
The same issue you will encounter if will run `govet` or `golint` without `golangci-lint`.
282-
Try `go install ./...` (or `go install ./cmd/...`: depends on the project) first.
284+
No, you don't need to do it anymore. We will run `go install -i` and `go test -i`
285+
for analyzed packages ourselves. We will run them only
286+
if option `govet.use-installed-packages` is `true`.
283287

284288
**`golangci-lint` doesn't work**
285289

pkg/config/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ type LintersSettings struct {
120120
CheckAssignToBlank bool `mapstructure:"check-blank"`
121121
}
122122
Govet struct {
123-
CheckShadowing bool `mapstructure:"check-shadowing"`
123+
CheckShadowing bool `mapstructure:"check-shadowing"`
124+
UseInstalledPackages bool `mapstructure:"use-installed-packages"`
124125
}
125126
Golint struct {
126127
MinConfidence float64 `mapstructure:"min-confidence"`

0 commit comments

Comments
 (0)