Skip to content

typecheck false positives on module versions in import statements #3718

Closed
@Ullaakut

Description

@Ullaakut

Welcome

  • Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've included all information below (version, config, etc.).
  • Yes, I've tried with the standalone linter if available (e.g., gocritic, go vet, etc.). (https://golangci-lint.run/usage/linters/)

Description of the problem

When running the latest version of golangci-lint on my project, I get false positives for modules with matching versions. The code does compile and run without any issues.

Example:

	"github.com/hamba/cmd/v2"
	"github.com/hamba/logger/v2"
	"github.com/hamba/statter/v2"
	"github.com/urfave/cli/v2"

Here, those four imports trigger the following linter warnings:

cmd/cmd.go:8:2: `v2` redeclared in this block (typecheck)
        "github.com/hamba/logger/v2"
        ^
cmd/cmd.go:7:2: other declaration of v2 (typecheck)
        "github.com/hamba/cmd/v2"
        ^
cmd/cmd.go:10:2: `v2` redeclared in this block (typecheck)
        "github.com/hamba/statter/v2"
        ^

Then, additional warnings occur whenever those packages are used within that file, because the linter seems unaware of the actual package name (which is not v2).

cmd/cmd.go:20:19: undefined: cli (typecheck)
func NewLogger(c *cli.Context, svc string) (*logger.Logger, func(), error) {
                  ^
cmd/cmd.go:35:20: undefined: cli (typecheck)
func NewStatter(c *cli.Context, log *logger.Logger, svc string) (*statter.Statter, func(), error) {
                   ^
cmd/cmd.go:50:19: undefined: cli (typecheck)
func NewTracer(c *cli.Context, log *logger.Logger, svc, version string) (*trace.TracerProvider, func(), error) {
                  ^
cmd/cmd.go:23:14: undefined: cmd (typecheck)
        log, err := cmd.NewLogger(c)
                    ^
cmd/cmd.go:36:16: undefined: cmd (typecheck)
        stats, err := cmd.NewStatter(c, log)
                      ^
cmd/cmd.go:51:17: undefined: cmd (typecheck)
        tracer, err := cmd.NewTracer(c, log,

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.52.1 built with go1.20.2 from d92b38cc on 2023-03-21T19:48:38Z

Configuration file

$ cat .golangci.yml
run:
  tests: false
  timeout: 5m

linters-settings:
  cyclop:
    max-complexity: 15
    skip-tests: true
  lll:
    line-length: 130
  gofumpt:
    extra-rules: true
  gosec:
    excludes:
      - G402

linters:
  enable-all: true
  disable:
    - interfacer # deprecated
    - scopelint # deprecated
    - maligned # deprecated
    - golint # deprecated
    - gocyclo # duplicate of cyclop
    - structcheck # deprecated
    - ifshort # deprecated
    - varcheck # deprecated
    - deadcode # deprecated
    - nosnakecase # deprecated
    - exhaustivestruct # deprecated
    - exhaustive
    - exhaustruct
    - forcetypeassert
    - funlen
    - gochecknoglobals
    - gochecknoinits
    - godox
    - goerr113
    - gomnd
    - ireturn
    - nlreturn
    - nonamedreturns
    - varnamelen
    - wrapcheck
    - wsl

issues:
  exclude-use-default: false
  exclude:
    - "ST1000: at least one file in a package should have a package comment"
    - "package-comments: should have a package comment"
  exclude-rules: []

Go environment

$ go version && go env
go version go1.20.2 linux/amd64
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ullaakut/.cache/go-build"
GOENV="/home/ullaakut/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ullaakut/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ullaakut/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.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/mnt/e/Work/x/y/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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4085354824=/tmp/go-build -gno-record-gcc-switches"

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /mnt/e/Work/x/y /mnt/e/Work/x /mnt/e/Work /mnt/e /mnt / /home/ullaakut]
INFO [config_reader] Used config file .golangci.yml
INFO [lintersdb] Active 77 linters: [asasalint asciicheck bidichk bodyclose containedctx contextcheck cyclop decorder depguard dogsled dupl dupword durationcheck errcheck errchkjson errname errorlint execinquery exportloopref forbidigo gci ginkgolinter gocheckcompilerdirectives gocognit goconst gocritic godot gofmt gofumpt goheader goimports gomoddirectives gomodguard goprintffuncname gosec gosimple govet grouper importas ineffassign interfacebloat lll loggercheck maintidx makezero misspell musttag nakedret nestif nilerr nilnil noctx nolintlint nosprintfhostport paralleltest prealloc predeclared promlinter reassign revive rowserrcheck sqlclosecheck staticcheck stylecheck tagliatelle tenv testableexamples testpackage thelper tparallel typecheck unconvert unparam unused usestdlibvars wastedassign whitespace]
INFO [loader] Go packages loading at mode 575 (types_sizes|deps|name|imports|compiled_files|exports_file|files) took 447.3815ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 7.2091ms
INFO [linters_context] importas settings found, but no aliases listed. List aliases under alias: key.
INFO [linters_context/goanalysis] analyzers took 8.5834ms with top 10 stages: the_only_name: 696.5µs, ST1012: 204.6µs, gosec: 176.8µs, asciicheck: 164.5µs, errcheck: 164.1µs, framepointer: 154.2µs, ST1000: 154.2µs, predeclared: 147.4µs, inspect: 145.4µs, unconvert: 145µs
INFO [runner/max_same_issues] 8/11 issues with text "undefined: cli" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 2/5 issues with text "undefined: logger" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 2/5 issues with text "undefined: cmd" were hidden, use --max-same-issues
INFO [runner/max_same_issues] 2/5 issues with text "`v2` redeclared in this block" were hidden, use --max-same-issues
INFO [runner] Issues before processing: 9618, after processing: 17
INFO [runner] Processors filtering stat (out/in): filename_unadjuster: 9618/9618, autogenerated_exclude: 9618/9618, exclude-rules: 9618/9618, max_same_issues: 17/31, source_code: 17/17, path_shortener: 17/17, severity-rules: 17/17, fixer: 17/17, path_prefixer: 17/17, exclude: 9618/9618, nolint: 9618/9618, diff: 31/31, cgo: 9618/9618, path_prettifier: 9618/9618, skip_files: 9618/9618, uniq_by_line: 31/9618, max_per_file_from_linter: 31/31, sort_results: 17/17, skip_dirs: 9618/9618, identifier_marker: 9618/9618, max_from_linter: 17/17
INFO [runner] processing took 115.8071ms with stages: identifier_marker: 49.5033ms, path_prettifier: 29.5258ms, nolint: 22.3821ms, source_code: 7.8135ms, skip_dirs: 3.4153ms, filename_unadjuster: 823.6µs, uniq_by_line: 753.5µs, autogenerated_exclude: 596.1µs, exclude: 547.4µs, cgo: 344.1µs, max_same_issues: 88.6µs, max_per_file_from_linter: 6.6µs, max_from_linter: 3.3µs, path_shortener: 2.3µs, fixer: 400ns, sort_results: 300ns, severity-rules: 200ns, exclude-rules: 200ns, diff: 200ns, path_prefixer: 200ns, skip_files: 100ns
INFO [runner] linters took 999.0607ms with stages: goanalysis_metalinter: 883.1756ms
internal/observe/observer.go:8:2: `v2` redeclared in this block (typecheck)
        "github.com/hamba/statter/v2"
        ^
internal/observe/observer.go:7:2: other declaration of v2 (typecheck)
        "github.com/hamba/logger/v2"
        ^
internal/observe/observer.go:15:13: undefined: logger (typecheck)
        Log       *logger.Logger
                   ^
internal/observe/observer.go:16:13: undefined: statter (typecheck)
        Stats     *statter.Statter
                   ^
internal/observe/observer.go:23:15: undefined: logger (typecheck)
func New(log *logger.Logger, stats *statter.Statter, traceProv trace.TracerProvider, closeFns ...func()) *Observer {
              ^
internal/observe/observer.go:51:9: undefined: logger (typecheck)
        log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Error)
               ^
internal/observe/observer.go:52:11: undefined: statter (typecheck)
        stats := statter.New(statter.DiscardReporter, time.Minute)
                 ^
cmd/cmd.go:8:2: `v2` redeclared in this block (typecheck)
        "github.com/hamba/logger/v2"
        ^
cmd/cmd.go:7:2: other declaration of v2 (typecheck)
        "github.com/hamba/cmd/v2"
        ^
cmd/cmd.go:10:2: `v2` redeclared in this block (typecheck)
        "github.com/hamba/statter/v2"
        ^
cmd/cmd.go:20:19: undefined: cli (typecheck)
func NewLogger(c *cli.Context, svc string) (*logger.Logger, func(), error) {
                  ^
cmd/cmd.go:35:20: undefined: cli (typecheck)
func NewStatter(c *cli.Context, log *logger.Logger, svc string) (*statter.Statter, func(), error) {
                   ^
cmd/cmd.go:50:19: undefined: cli (typecheck)
func NewTracer(c *cli.Context, log *logger.Logger, svc, version string) (*trace.TracerProvider, func(), error) {
                  ^
cmd/cmd.go:23:14: undefined: cmd (typecheck)
        log, err := cmd.NewLogger(c)
                    ^
cmd/cmd.go:36:16: undefined: cmd (typecheck)
        stats, err := cmd.NewStatter(c, log)
                      ^
cmd/cmd.go:51:17: undefined: cmd (typecheck)
        tracer, err := cmd.NewTracer(c, log,
                       ^
cmd/allocgen/main.go:15:2: other declaration of v2 (typecheck)
        "github.com/hamba/cmd/v2"
        ^
INFO File cache stats: 3 entries of total size 5.5KiB
INFO Memory: 16 samples, avg is 96.1MB, max is 201.5MB
INFO Execution took 1.4855319s

Code example or link to a public repository

package cmd

import (
	"context"

	"github.com/hamba/cmd/v2"
	"github.com/hamba/logger/v2"
	lctx "github.com/hamba/logger/v2/ctx"
	"github.com/hamba/statter/v2"
	"github.com/hamba/statter/v2/runtime"
	"github.com/hamba/statter/v2/tags"
	"github.com/urfave/cli/v2"
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions