golangci/golangci-lint

can't lint go.mod file unless there are other go source files in the same directory

Closed this issue · 6 comments

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?

Via editor/IDE

Description of the problem

golangci-lint provides linters specifically for the go.mod file so I expect to be able to lint it.

There are not always go source files in the same directory as the go.mod file.

Action:

I try to lint only this part of the source using golangci-lint run /path/to/dir/with/gomod.

Expected behaviour:

golangci-lint lints my go.mod file

Actual behavior:

golangci-lint returns an error since the directory did not contain any go source files

ERRO Running error: context loading failed: failed to load packages: failed to load packages: package <path>: no go files to analyze

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 2.4.0 built with go1.25.0 from 43d03392 on 2025-08-13T23:36:29Zj

Configuration

version: "2"
formatters:
  enable:
    - gofumpt
    - goimports
  exclusions:
    paths:
      - vendor
linters:
  enable:
    - gochecknoglobals
    - gochecknoinits
    - lll
    - misspell
    - godot
    - noctx
    - contextcheck
    - containedctx
    - revive
    - testableexamples
    - nilnil
    - ireturn
    - nonamedreturns
    - nakedret
    - varnamelen
    - errorlint
    - loggercheck
    - forbidigo
    - staticcheck
  exclusions:
    paths:
      - vendor

Go environment

$ go version && go env
go version go1.25.1 darwin/arm64
AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/gfleming/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/gfleming/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/1d/_wdt4bm96_5c915q5m2sh6sc0000gn/T/g
o-build1664847679=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/gfleming/Projects/Personal/grpcstuff/go.mod'
GOMODCACHE='/Users/gfleming/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/gfleming/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.25.1/libexec'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/gfleming/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.25.1/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.25.1'
GOWORK=''
PKG_CONFIG='pkg-config'

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
# paste output here

A minimal reproducible example or link to a public repository

// add your code here

Validation

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

Supporter

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

ldez commented

Hello,

This is expected: golangci-lint must be run inside the module you want to analyze.

Also, the linters that analyze go.mod are a kind of hack of the static analysis system: golangci-lint needs code to make the analysis work.

It may be expected, but is this really the desired behaviour here?

For example many tools will invoke linters from the path of an opened file in an IDE. Since golangci-lint handles go.mod files it makes sense that it would be invoked on opening the go.mod file, which then in this case results in an error.

It feels like this could be handled more gracefully. I’m happy to give this some thought and suggest an MR if you are receptive to the idea?

ldez commented

For now, we don't plan to support that.

So you wouldn’t entertain an MR if I created one?

ldez commented

Yes, I wouldn't.