Skip to content

golangci-lint can't find source files when run from outside the module directory in a go.work environment #4219

@intinig

Description

@intinig

Welcome

Description of the problem

I have a project that uses go.work with several modules. golangci-lint fails at running the linter from anywhere but the module's directory for one of these, with Running error: context loading failed: no go files to analyze.

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.55.2 built with go1.21.3 from e3c2265f on 2023-11-03T12:59:25Z

Configuration

linters:
  enable:
  # Enable specific linter
  # https://golangci-lint.run/usage/linters
    - asciicheck
    - dupl
    - durationcheck
    - errorlint
    - gocyclo
    - gosec
    - maintidx
    - whitespace
    - unparam
    - unconvert
    - tparallel
    - thelper
    - reassign
    - prealloc
    - paralleltest
    - nilnil
    - nilerr
    - nestif
    - importas
    - gocritic
    - exportloopref
issues:
  exclude-rules:
    # Exclude some linters from running on tests files.
    - path: _test\.go
      linters:
        - gocyclo
        - errcheck
        - dupl
        - gosec
        - maintidx
        - errorlint

This doesn't work

# C:\s\bin\golangci-lint.exe run -v --allow-parallel-runners -j 4 --issues-exit-code 1 --max-issues-per-linter 0 --max-same-issues 0 -c C:\s\nk-modules\.golangci.yaml .\src\common

If I cd src\common, this works

# C:\s\bin\golangci-lint.exe run -v --allow-parallel-runners -j 4 --issues-exit-code 1 --max-issues-per-linter 0 --max-same-issues 0 -c C:\s\nk-modules\.golangci.yaml .

Go environment

$ go version && go env
go version go1.21.4 windows/amd64
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\gintini\AppData\Local\go-build
set GOENV=C:\Users\gintini\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\gintini\go\pkg\mod
set GONOPROXY=github.com/vela-games,buf.build/gen/go
set GONOSUMDB=github.com/vela-games,buf.build/gen/go
set GOOS=windows
set GOPATH=C:\Users\gintini\go
set GOPRIVATE=github.com/vela-games,buf.build/gen/go
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\gintini\AppData\Local\Temp\go-build1716184392=/tmp/go-build -gno-record-gcc-switches

Verbose output of running

$ golangci-lint cache clean
$ C:\s\bin\golangci-lint.exe run -v --allow-parallel-runners -j 4 --issues-exit-code 1 --max-issues-per-linter 0 --max-same-issues 0 -c C:\s\nk-modules\.golangci.yaml ./src/common
level=info msg="[config_reader] Used config file .golangci.yaml"
level=info msg="[lintersdb] Active 27 linters: [asciicheck dupl durationcheck errcheck errorlint exportloopref gocritic gocyclo gosec gosimple govet importas ineffassign maintidx nestif nilerr nilnil paralleltest prealloc reassign staticcheck thelper tparallel unconvert unparam unused whitespace]"
level=info msg="[loader] Go packages loading at mode 575 (deps|files|name|types_sizes|compiled_files|exports_file|imports) took 152.6283ms"
level=error msg="Running error: context loading failed: no go files to analyze"
level=info msg="Memory: 4 samples, avg is 27.1MB, max is 27.1MB"
level=info msg="Execution took 223.5321ms"

This is run from src/common:

$ golangci-lint cache clean
$ C:\s\bin\golangci-lint.exe run -v --allow-parallel-runners -j 4 --issues-exit-code 1 --max-issues-per-linter 0 --max-same-issues 0 -c C:\s\nk-modules\.golangci.yaml .
level=info msg="[config_reader] Used config file ..\\..\\.golangci.yaml"
level=info msg="[lintersdb] Active 27 linters: [asciicheck dupl durationcheck errcheck errorlint exportloopref gocritic gocyclo gosec gosimple govet importas ineffassign maintidx nestif nilerr nilnil paralleltest prealloc reassign staticcheck thelper tparallel unconvert unparam unused whitespace]"
level=info msg="[loader] Go packages loading at mode 575 (exports_file|types_sizes|deps|files|imports|name|compiled_files) took 674.5274ms"
level=info msg="[runner/filename_unadjuster] Pre-built 0 adjustments in 0s"
level=info msg="[linters_context] importas settings found, but no aliases listed. List aliases under alias: key."
level=info msg="[linters_context/goanalysis] analyzers took 11.9829ms with top 10 stages: gocritic: 2.5999ms, printf: 1.0462ms, S1003: 1.0462ms, SA9002: 1.0462ms, ctrlflow: 1.0462ms, fact_purity: 1.0341ms, nilness: 1.0341ms, SA5012: 1.0341ms, typedness: 530.1µs, fact_deprecated: 523.4µs"
level=info msg="[runner] processing took 0s with stages: sort_results: 0s, filename_unadjuster: 0s, path_prettifier: 0s, identifier_marker: 0s, max_per_file_from_linter: 0s, max_from_linter: 0s, uniq_by_line: 0s, path_shortener: 0s, severity-rules: 0s, cgo: 0s, skip_files: 0s, skip_dirs: 0s, autogenerated_exclude: 0s, nolint: 0s, diff: 0s, max_same_issues: 0s, source_code: 0s, fixer: 0s, exclude: 0s, exclude-rules: 0s, path_prefixer: 0s"
level=info msg="[runner] linters took 576.4503ms with stages: goanalysis_metalinter: 576.4503ms"
level=info msg="File cache stats: 0 entries of total size 0B"
level=info msg="Memory: 15 samples, avg is 38.8MB, max is 57.6MB"
level=info msg="Execution took 1.3244328s"

A minimal reproducible example or link to a public repository

I can't repro this.

Validation

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions