golangci / golangci-lint

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

govet: `printf` unstable behavior with imported types #4423

Open gabolaev opened 8 months ago

gabolaev commented 8 months ago

Welcome

Description of the problem

Hi, we've encountered some strange behavior with the govet printf linter.

If we edit the previously reported file without fixing the issue, it stops reporting the issue. However, if we clean the cache, it gets back to normal, but I'm not sure we're supposed to clean the cache before every run.

asciicast

There is one interesting detail that I think may be a clue though. It stops being unstable if we change the code so that it doesn't import logger, but initializes log in place:

 package somepackage

-import "reproduction/logger"
+import "github.com/sirupsen/logrus"

-var log = logger.NewLogger()
+var log = logrus.New()

 func SomeFunc() {
        log.Warnf("SOME OTHER TEXT %v") // this line is supposed to be reported by govet printf linter
 }

Version of golangci-lint

```console $ golangci-lint --version golangci-lint has version 1.56.2 built with go1.22.0 from 58a724a on 2024-02-15T12:52:06Z ```

Configuration

```console linters: disable-all: true enable: - govet ```

Go environment

```console $ go version && go env go version go1.22.0 darwin/arm64 GO111MODULE='' GOARCH='arm64' GOBIN='' GOCACHE='/Users/gabolaev/Library/Caches/go-build' GOENV='/Users/gabolaev/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/gabolaev/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPATH='/Users/gabolaev/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/opt/homebrew/Cellar/go/1.22.0/libexec' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.0/libexec/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.22.0' GCCGO='gccgo' AR='ar' CC='cc' CXX='c++' CGO_ENABLED='1' GOMOD='/Users/gabolaev/d/reproduction/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/k1/pd5ymz911jl6gdx0zgj85cym0000gq/T/go-build3537487068=/tmp/go-build -gno-record-gcc-switches -fno-common' ```

Verbose output of running

```console $ # golangci-lint cache clean $ golangci-lint run -v INFO [config_reader] Config search paths: [./ /Users/gabolaev/d/reproduction /Users/gabolaev/d /Users/gabolaev /Users /] INFO [lintersdb] Active 6 linters: [errcheck gosimple govet ineffassign staticcheck unused] INFO [loader] Go packages loading at mode 575 (files|compiled_files|deps|exports_file|imports|name|types_sizes) took 80.6465ms INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 671.333µs INFO [linters_context/goanalysis] analyzers took 22.21121ms with top 10 stages: buildir: 4.840833ms, fact_deprecated: 1.047916ms, ctrlflow: 868.047µs, errcheck: 710.334µs, SA9002: 663.334µs, printf: 639.212µs, SA4020: 559.833µs, SA4019: 559.667µs, unused: 544.792µs, ineffassign: 540.792µs INFO [runner] processing took 998ns with stages: max_same_issues: 333ns, skip_dirs: 167ns, nolint: 125ns, sort_results: 125ns, max_from_linter: 42ns, diff: 42ns, exclude-rules: 41ns, max_per_file_from_linter: 41ns, severity-rules: 41ns, autogenerated_exclude: 41ns, path_prettifier: 0s, skip_files: 0s, path_shortener: 0s, path_prefixer: 0s, uniq_by_line: 0s, exclude: 0s, filename_unadjuster: 0s, source_code: 0s, fixer: 0s, cgo: 0s, identifier_marker: 0s INFO [runner] linters took 95.207125ms with stages: goanalysis_metalinter: 94.983417ms INFO File cache stats: 0 entries of total size 0B INFO Memory: 3 samples, avg is 32.2MB, max is 39.6MB INFO Execution took 184.939709ms ```

A minimal reproducible example or link to a public repository

https://github.com/gabolaev/gl-issue-reproduction

Validation

boring-cyborg[bot] commented 8 months ago

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

ldez commented 8 months ago

Hello,

the problem is related to 2 things: the cache, and the relative position between reports and the code.

  1. The first run fills the cache with reports
    • the report targets the file somefile.go inside the package somepackage but the report is related to a function of another package (logger).
  2. The second run uses the cache and invalids the cache for the package somepackage
    • only the "invalided" package will be analyzed
    • but the root of the report is not inside somepackage but logger (and it's not a dependency).
    • so when the package is analyzed there is no report.

For now, I have no solution, I need to investigate more.

ldez commented 8 months ago

I found a solution.