Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gomoddirectives: false-negative / false-positive #5736

Closed
@TheRoSS

Description

@TheRoSS

Welcome

  • Yes, I'm using a binary release within 2 latest releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've read the typecheck section of the FAQ.
  • Yes, I've tried with the standalone linter if available (e.g., gocritic, go vet, etc.).
  • I agree to follow this project's Code of Conduct

How did you install golangci-lint?

go install

Description of the problem

I had 'replace' directive in the go.mod file. Linter complained with gomoddirectives error on line 127. I removed this line. But linter continues to complain.

WARN [runner/source_code] Failed to get line 127 for file /home/alex/ws/rupor/video/go.mod: invalid file line index0 (126) >= len(fc) (126) 
go.mod:127:1: replacement are not allowed: gitlab-b.rutube.ru/cyber-rom/rupor/cmatch-scheduler (gomoddirectives)

But there is no line 127 at all in the go.mod file anymore.
If I switch to another git branch the error disappears. If I switch back to the original branch the error returns back too.

The problem can be solved with command

golangci-lint cache clean

From this I can conclude that linter takes go.mod file from cache even after its modification.

The problem arises not permanently. I saw it second time during 4 months.

P.S. I used jetbrains GoLand 2024.3.5 to edit go.mod file. Maybe it matters.

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version v1.64.8 built with go1.23.8 from (unknown, modified: ?, mod sum: "h1:y5TdeVidMtBGG32zgSC7ZXTFNHrsJkDnpO4ItB3Am+I=") on (unknown)

Configuration

run:
  concurrency: 4
  timeout: 5m
  tests: true
  modules-download-mode: readonly
  allow-parallel-runners: true
  allow-serial-runners: true
  go: '1.23'

issues:
  fix: true
  max-issues-per-linter: 0
  max-same-issues: 0
  exclude-rules:
    - path: ^(.*/)?(integration_)?test/
      linters:
        - gosec
        - goconst
        - mnd
    - path: _test\.go$
      linters:
        - gosec
        - goconst
        - mnd
  exclude-dirs:
    - \.vendor\.pb$
    - vendor$
    - mocks$
  exclude-files:
    - \.pb\.go$
    - \.pb\.gw\.go$
    - \.swagger\.go$
    - _mocks\.go$

  include:
    #gosec
    - EXC0006
    - EXC0007
    - EXC0008
    - EXC0009
    - EXC0010

linters-settings:
  errcheck:
    check-type-assertions: true

  goconst:
    min-len:         2
    min-occurrences: 2

  gocritic:
    settings:
      captLocal:
        paramsOnly: false

  goimports:
    local-prefixes: <some prefix>

  misspell:
    locale: US

  gosimple:
    checks:
      - "all"

  govet:
    enable-all: true
    disable:
      - fieldalignment # disable alignment of structures
      - shadow # disable all shadows declaration

linters:
  disable-all: true
  enable:
    - asciicheck # checks that your code does not contain non-ASCII identifiers
    - bidichk # checks for dangerous unicode character sequences
    - bodyclose # checks whether HTTP response body is closed successfully
    - copyloopvar # checks for pointers to enclosing loop variables
    - dogsled # checks assignments with too many blank identifiers (e.g. x, , , _, := f())
    - errcheck # errcheck is a program for checking for unchecked errors in Go code
    - errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
    - gocognit # computes and checks the cognitive complexity of functions
    - goconst # finds repeated strings that could be replaced by a constant
    - gocritic # provides diagnostics that check for bugs, performance and style issues
    - gocyclo # computes and checks the cyclomatic complexity of functions
    - gofmt # gofmt checks whether code was gofmt-ed
    - goimports # check import statements are formatted according to the 'goimport' command
    - gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
    - gosec # inspects source code for security problems
    - gosimple # linter for Go source code that specializes in simplifying code
    - govet # reports suspicious constructs
    - ineffassign # detects when assignments to existing variables are not used
    - misspell # finds commonly misspelled English words in comments
    - mnd # an analyzer to detect magic numbers
    - nilerr # finds the code that returns nil even if it checks that
    - noctx # finds sending http request without context
    - prealloc # finds slice declarations that could potentially be pre-allocated
    - predeclared # finds code that shadows one of Go's predeclared
    - revive # configurable linter for Go, provides a framework for development of custom rules.
    - staticcheck # https://staticcheck.io/docs/
    - stylecheck # stylecheck is a replacement for golint
    - unconvert # remove unnecessary type conversions
    - unparam # reports unused function parameters
    - unused # checks Go code for unused constants, variables, functions and types
    - whitespace # tool for detection of leading and trailing whitespace

Go environment

go version go1.23.8 linux/amd64
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/alex/.cache/go-build'
GOENV='/home/alex/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/alex/.gvm/pkgsets/go1.23.8/global/pkg/mod'
GONOPROXY='gitlab.cyrm.ru/*,gitlab-b.rutube.ru/cyber-rom/rupor/*,gitlab.rutube.ru/*'
GONOSUMDB='gitlab.cyrm.ru/*,gitlab-b.rutube.ru/cyber-rom/rupor/*,gitlab.rutube.ru/*'
GOOS='linux'
GOPATH='/home/alex/.gvm/pkgsets/go1.23.8/global'
GOPRIVATE='gitlab.cyrm.ru/*,gitlab-b.rutube.ru/cyber-rom/rupor/*,gitlab.rutube.ru/*'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/alex/.gvm/gos/go1.23.8'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/alex/.gvm/gos/go1.23.8/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.8'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/alex/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/alex/ws/rupor/video/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 -ffile-prefix-map=/tmp/go-build3323948570=/tmp/go-build -gno-record-gcc-switches'

Verbose output of running

$ golangci-lint cache clean
!!! This fixes the problem
$ golangci-lint run -v
INFO golangci-lint has version v1.64.8 built with go1.23.8 from (unknown, modified: ?, mod sum: "h1:y5TdeVidMtBGG32zgSC7ZXTFNHrsJkDnpO4ItB3Am+I=") on (unknown) 
INFO [config_reader] Config search paths: [...] 
INFO [config_reader] Used config file ./.golangci.yml
INFO [goenv] Read go env for 7.515976ms: map[string]string{"GOCACHE":"/home/.../.cache/go-build", "GOROOT":"/home/.../.gvm/gos/go1.23.8"} 
INFO [lintersdb] Active 31 linters: [asciicheck bidichk bodyclose copyloopvar dogsled errcheck errname gocognit goconst gocritic gocyclo gofmt goimports gomoddirectives gosec gosimple govet ineffassign misspell mnd nilerr noctx prealloc predeclared revive staticcheck stylecheck unconvert unparam unused whitespace] 
INFO [loader] Go packages loading at mode 8767 (compiled_files|files|name|types_sizes|deps|exports_file|imports) took 1.048974991s 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 236.333271ms 
INFO [linters_context/goanalysis] analyzers took 0s with no stages 
INFO [runner] Applying suggested fixes            
INFO [runner] fixer took 470ns with stages: all: 470ns 
INFO [runner/exclusion_rules] Skipped 5 issues by rules: [Path: "_test\\.go$", Linters: "gosec, goconst, mnd"] 
INFO [runner/exclusion_rules] Skipped 45 issues by rules: [Path: "^(.*/)?(integration_)?test/", Linters: "gosec, goconst, mnd"] 
INFO [runner] Issues before processing: 3728, after processing: 0 
INFO [runner] Processors filtering stat (in/out): skip_dirs: 2653/2653, identifier_marker: 2266/2266, exclusion_rules: 2266/74, path_absoluter: 3728/3728, skip_files: 3728/2653, invalid_issue: 3728/3728, exclusion_paths: 3728/3728, cgo: 3728/3728, filename_unadjuster: 3728/3728, path_relativity: 3728/3728, nolint_filter: 74/0, generated_file_filter: 2653/2266 
INFO [runner] processing took 70.062434ms with stages: nolint_filter: 28.973133ms, exclusion_rules: 17.699448ms, generated_file_filter: 11.38701ms, path_relativity: 4.214477ms, skip_dirs: 3.822557ms, skip_files: 1.300504ms, invalid_issue: 927.637µs, filename_unadjuster: 909.179µs, identifier_marker: 409.35µs, cgo: 216.513µs, path_absoluter: 167.033µs, fixer: 32.391µs, max_same_issues: 535ns, max_from_linter: 469ns, exclusion_paths: 450ns, sort_results: 336ns, uniq_by_line: 292ns, diff: 290ns, source_code: 290ns, path_shortener: 221ns, max_per_file_from_linter: 114ns, severity-rules: 103ns, path_prettifier: 102ns 
INFO [runner] linters took 927.928914ms with stages: goanalysis_metalinter: 857.6949ms 
INFO File cache stats: 0 entries of total size 0B 
INFO Memory: 24 samples, avg is 66.5MB, max is 104.5MB 
INFO Execution took 2.218360717s                  

A minimal reproducible example or link to a public repository

go.mod:
...
127: replace ... => ...

This line deleted

Validation

  • Yes, I've included all information above (version, config, etc.).

Supporter

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions