golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
14.9k stars 1.35k forks source link

Using `--fix` with both `gofumpt` and `gocritic` linters deletes comment lines #3230

Closed psaikko closed 4 months ago

psaikko commented 1 year ago

Welcome

Description of the problem

Using golangci-lint with --fix flag and both gofumpt and gocritic linters enabled deletes comment lines. This is the case with at least comment lines missing a space after //.

In minimal example running with golangci-lint run --fix --disable-all --enable gocritic,gofumpt with no .golangci.yml config file.

Using either gofumpt or gocritic alone has the expected behavior (e.g. adding whitespace after //)

Version of golangci-lint

```console $ golangci-lint --version golangci-lint has version 1.49.0 built from cc2d97f3 on 2022-08-24T10:24:37Z ```

Configuration file

```console $ cat .golangci.yml cat: .golangci.yml: No such file or directory ```

Go environment

```console $ go version && go env go version go1.19 linux/amd64 GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/home/psaikko/.cache/go-build" GOENV="/home/psaikko/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/psaikko/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/psaikko/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/dev/null" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4702641=/tmp/go-build -gno-record-gcc-switches" ```

Verbose output of running

```console $ golangci-lint cache clean $ golangci-lint run -v --fix --disable-all --enable gocritic,gofumpt main.go INFO [config_reader] Config search paths: [./ /home/psaikko/lintbug /home/psaikko /home /] INFO [lintersdb] Active 2 linters: [gocritic gofumpt] INFO [loader] Go packages loading at mode 575 (types_sizes|compiled_files|deps|exports_file|imports|files|name) took 132.829788ms INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 172.936µs INFO [linters context/goanalysis] analyzers took 5.195754ms with top 10 stages: gocritic: 4.979514ms, gofumpt: 216.24µs INFO [runner] Processors filtering stat (out/in): filename_unadjuster: 2/2, exclude: 2/2, max_from_linter: 2/2, source_code: 2/2, cgo: 2/2, skip_dirs: 2/2, autogenerated_exclude: 2/2, max_per_file_from_linter: 2/2, path_prefixer: 2/2, path_prettifier: 2/2, skip_files: 2/2, identifier_marker: 2/2, diff: 2/2, sort_results: 2/2, exclude-rules: 2/2, nolint: 2/2, uniq_by_line: 2/2, max_same_issues: 2/2, path_shortener: 2/2, severity-rules: 2/2 INFO [runner] processing took 230.733µs with stages: exclude-rules: 68.396µs, identifier_marker: 59.956µs, nolint: 32.522µs, autogenerated_exclude: 21.874µs, path_prettifier: 17.159µs, source_code: 12.761µs, skip_dirs: 9.658µs, path_shortener: 1.352µs, cgo: 1.236µs, max_same_issues: 1.06µs, filename_unadjuster: 904ns, uniq_by_line: 647ns, max_per_file_from_linter: 632ns, skip_files: 631ns, max_from_linter: 569ns, severity-rules: 341ns, exclude: 312ns, diff: 310ns, sort_results: 232ns, path_prefixer: 181ns INFO [runner] linters took 540.551663ms with stages: goanalysis_metalinter: 540.238954ms INFO Line 6 has multiple issues but at least one of them isn't inline: []result.Issue{result.Issue{FromLinter:"gocritic", Text:"commentFormatting: put a space between `//` and comment text", Severity:"", SourceLines:[]string{"\t//standard greeting"}, Replacement:(*result.Replacement)(0xc0010f4030), Pkg:(*packages.Package)(0xc000b1ec00), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"main.go", Offset:43, Line:6, Column:2}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}, result.Issue{FromLinter:"gofumpt", Text:"File is not `gofumpt`-ed", Severity:"", SourceLines:[]string{"\t//standard greeting"}, Replacement:(*result.Replacement)(0xc000f96610), Pkg:(*packages.Package)(0xc000b1ec00), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"main.go", Offset:0, Line:6, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""}} INFO Fix issue &result.Issue{FromLinter:"gocritic", Text:"commentFormatting: put a space between `//` and comment text", Severity:"", SourceLines:[]string{"\t//standard greeting"}, Replacement:(*result.Replacement)(0xc0010f4030), Pkg:(*packages.Package)(0xc000b1ec00), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"main.go", Offset:43, Line:6, Column:2}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""} with range {6 6} INFO fixer took 206.198µs with stages: all: 206.198µs INFO File cache stats: 1 entries of total size 93B INFO Memory: 8 samples, avg is 45.1MB, max is 64.8MB INFO Execution took 681.67783ms ```

Code example or link to a public repository

```go package main import "fmt" func main() { //standard greeting fmt.Println("hello world") } ```
boring-cyborg[bot] commented 1 year ago

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

henriksjostrom commented 1 year ago

I ran into this as well. It was fairly hard to add a comment to the code with this

DryHumour commented 1 year ago

I wonder if the issue might be triggered in general by two or more fixers being applied to the same line?

ldemailly commented 1 year ago

so per the closed dup issue, not just comments but also code

ldez commented 4 months ago

Fixed by #3230

Antonboom commented 3 months ago

@ldez above you attached link to this issue itself 🙂

bombsimon commented 3 months ago

Should be #3316