Skip to content

Commit

Permalink
Change golangci-lint lint mode to project
Browse files Browse the repository at this point in the history
It seems `list_of_files` is not the right choice as default lint
mode for the `golangci-lint` linter, for several reasons.

In such mode, the changed `.go` files are sent in a single call to the
linter.

The first issue with that is that, as the linter
[Quick Start](https://golangci-lint.run/welcome/quick-start/) guide
explicitly states, files must come from the same package.

The second issue is that the linter tries to compile each file via
`types.Checker` and, unless the source file is self-contained and has no
references to things declared in other source files of the codebase, the
compilation will fail, even if the references are towards the same
package.

Such compilation errors are reported by `golangci-lint` and labeled as
`typecheck` (which is not a linter) (see
[why do you have `typecheck` errors?](https://golangci-lint.run/welcome/faq/#why-do-you-have-typecheck-errors)
section of the documentation).

The same problem has also been reported in
[`golangci-lint oxsecurity#1574`](golangci/golangci-lint#1574)
by an user who configured his editor to run the linter on single files.
As commented by a core maintainer
[here](golangci/golangci-lint#1574 (comment))
`typecheck` is not included in the "fast" linters list and, therefore,
cannot be run on single files.
  • Loading branch information
wandering-tales committed Apr 26, 2024
1 parent ced5636 commit 841070a
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .automation/test/golang/bad/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module megalinter.io/test-bad

go 1.22
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions .automation/test/golang/fix/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module megalinter.io/test-fix

go 1.22
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions .automation/test/golang/good/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module megalinter.io/test-good

go 1.22
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This is a package comment
package main
package good

import "fmt"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This is a package comment
package main
package good

import "fmt"

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
- Reporters

- Fixes
- Change `golangci-lint` lint mode to `project`

- Doc

Expand Down
2 changes: 1 addition & 1 deletion megalinter/descriptors/go.megalinter-descriptor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ linters:
linter_rules_configuration_url: https://golangci-lint.run/usage/configuration/#config-file
linter_rules_inline_disable_url: https://golangci-lint.run/usage/false-positives/#nolint
config_file_name: .golangci.yml
cli_lint_mode: list_of_files
cli_lint_mode: project
cli_lint_extra_args:
- "run"
cli_lint_fix_arg_name: --fix
Expand Down

0 comments on commit 841070a

Please sign in to comment.