From 89ca4c823602fdcd108bbf3ac2a443c789f7cc35 Mon Sep 17 00:00:00 2001 From: Michele Cardone Date: Fri, 26 Apr 2024 19:40:21 +0200 Subject: [PATCH] Change `golangci-lint` lint mode to `project` 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 #1574`](https://github.com/golangci/golangci-lint/issues/1574) by an user who configured his editor to run the linter on single files. As commented by a core maintainer [here](https://github.com/golangci/golangci-lint/issues/1574#issuecomment-804500358) `typecheck` is not included in the "fast" linters list and, therefore, cannot be run on single files. --- .automation/test/golang/bad/go.mod | 3 +++ .automation/test/golang/{ => bad}/golang_bad_01.go | 0 .automation/test/golang/{ => bad}/golang_bad_02.go | 0 .automation/test/golang/fix/go.mod | 3 +++ .automation/test/golang/{ => fix}/golang_fix_01.go | 0 .automation/test/golang/{ => fix}/golang_fix_02.go | 0 .automation/test/golang/good/go.mod | 3 +++ .automation/test/golang/{ => good}/golang_good_01.go | 0 .automation/test/golang/{ => good}/golang_good_02.go | 0 CHANGELOG.md | 1 + megalinter/descriptors/go.megalinter-descriptor.yml | 2 +- 11 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .automation/test/golang/bad/go.mod rename .automation/test/golang/{ => bad}/golang_bad_01.go (100%) rename .automation/test/golang/{ => bad}/golang_bad_02.go (100%) create mode 100644 .automation/test/golang/fix/go.mod rename .automation/test/golang/{ => fix}/golang_fix_01.go (100%) rename .automation/test/golang/{ => fix}/golang_fix_02.go (100%) create mode 100644 .automation/test/golang/good/go.mod rename .automation/test/golang/{ => good}/golang_good_01.go (100%) rename .automation/test/golang/{ => good}/golang_good_02.go (100%) diff --git a/.automation/test/golang/bad/go.mod b/.automation/test/golang/bad/go.mod new file mode 100644 index 00000000000..d94ed42c3a0 --- /dev/null +++ b/.automation/test/golang/bad/go.mod @@ -0,0 +1,3 @@ +module megalinter.io/test-bad + +go 1.22 diff --git a/.automation/test/golang/golang_bad_01.go b/.automation/test/golang/bad/golang_bad_01.go similarity index 100% rename from .automation/test/golang/golang_bad_01.go rename to .automation/test/golang/bad/golang_bad_01.go diff --git a/.automation/test/golang/golang_bad_02.go b/.automation/test/golang/bad/golang_bad_02.go similarity index 100% rename from .automation/test/golang/golang_bad_02.go rename to .automation/test/golang/bad/golang_bad_02.go diff --git a/.automation/test/golang/fix/go.mod b/.automation/test/golang/fix/go.mod new file mode 100644 index 00000000000..8bd378cfc86 --- /dev/null +++ b/.automation/test/golang/fix/go.mod @@ -0,0 +1,3 @@ +module megalinter.io/test-fix + +go 1.22 diff --git a/.automation/test/golang/golang_fix_01.go b/.automation/test/golang/fix/golang_fix_01.go similarity index 100% rename from .automation/test/golang/golang_fix_01.go rename to .automation/test/golang/fix/golang_fix_01.go diff --git a/.automation/test/golang/golang_fix_02.go b/.automation/test/golang/fix/golang_fix_02.go similarity index 100% rename from .automation/test/golang/golang_fix_02.go rename to .automation/test/golang/fix/golang_fix_02.go diff --git a/.automation/test/golang/good/go.mod b/.automation/test/golang/good/go.mod new file mode 100644 index 00000000000..47e39d859c1 --- /dev/null +++ b/.automation/test/golang/good/go.mod @@ -0,0 +1,3 @@ +module megalinter.io/test-good + +go 1.22 diff --git a/.automation/test/golang/golang_good_01.go b/.automation/test/golang/good/golang_good_01.go similarity index 100% rename from .automation/test/golang/golang_good_01.go rename to .automation/test/golang/good/golang_good_01.go diff --git a/.automation/test/golang/golang_good_02.go b/.automation/test/golang/good/golang_good_02.go similarity index 100% rename from .automation/test/golang/golang_good_02.go rename to .automation/test/golang/good/golang_good_02.go diff --git a/CHANGELOG.md b/CHANGELOG.md index cbf4b5469f2..8a6282b4f54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/megalinter/descriptors/go.megalinter-descriptor.yml b/megalinter/descriptors/go.megalinter-descriptor.yml index 9556dd683d8..ad7fa7338ee 100644 --- a/megalinter/descriptors/go.megalinter-descriptor.yml +++ b/megalinter/descriptors/go.megalinter-descriptor.yml @@ -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