Skip to content

When GOPATH is unset, context loading failed: no go files to analyze #3828

Open
@daxelrod-rh

Description

@daxelrod-rh

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

Hello! I've encountered a situation where golanglint-ci runs correctly when the GOPATH environment variable is set to its default value (GOPATH=$HOME/go or even GOPATH=$(go env GOPATH), but when GOPATH is unset, golanglint-ci fails with Running error: context loading failed: no go files to analyze. I believe this is a different root cause than #825 because the code builds and tests successfully, but I'm happy to move this into #825 if you believe it should be there instead.

I saw this behavior on RHEL 8.7. I've included a Dockerfile reproducer, although I haven't yet attempted to narrow down the linter set or source file set that cause this behavior yet.

FROM registry.access.redhat.com/ubi8/ubi:8.7-1112

RUN mkdir -p /usr/local/src
WORKDIR /usr/local/src

RUN sed -e "s/^enabled=.*/enabled=0/" -i /etc/dnf/plugins/subscription-manager.conf && yum install -y git diffutils
RUN curl -OL https://go.dev/dl/go1.20.4.linux-amd64.tar.gz && tar -C $HOME -xzf go1.20.4.linux-amd64.tar.gz
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $HOME/go/bin v1.52.2

RUN git clone https://github.com/rh-mobb/ocm-operator
WORKDIR /usr/local/src/ocm-operator
RUN git checkout e26aad059f3772f608a9388522efdf314c5fd461

ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/go/bin

RUN golangci-lint version
RUN go version && go env
RUN golangci-lint cache clean
RUN golangci-lint run --verbose

Building this Dockerfile with docker build . or podman build . results in the "Verbose output of running" section below.

However, if the last line of the Dockerfile is changed to

RUN GOPATH=$HOME/go golangci-lint run --verbose

the output changes to

level=info msg="[config_reader] Used config file .golangci.yml"
level=info msg="[lintersdb] Active 54 linters: [asciicheck bodyclose cyclop depguard dogsled dupl durationcheck errcheck errorlint exhaustive exportloopref forbidigo forcetypeassert funlen gocognit goconst gocritic gocyclo godot goerr113 gofmt goheader goimports gomnd gomoddirectives gomodguard goprintffuncname gosec gosimple govet importas ineffassign lll makezero misspell nakedret nestif nilerr nlreturn nolintlint paralleltest prealloc predeclared staticcheck stylecheck testpackage thelper tparallel typecheck unconvert unparam unused whitespace wrapcheck]"
level=info msg="[loader] Go packages loading at mode 575 (imports|types_sizes|compiled_files|exports_file|name|deps|files) took 34.623234133s"
level=info msg="[runner/filename_unadjuster] Pre-built 0 adjustments in 2.220315ms"
level=info msg="[linters_context] importas settings found, but no aliases listed. List aliases under alias: key."
level=warning msg="[linters_context] running gomoddirectives failed: failed to get module file: unmarshaling error: invalid character 'w' looking for beginning of value: warning: GOPATH set to GOROOT (/root/go) has no effect\n{\n\t\"Path\": \"github.com/rh-mobb/ocm-operator\",\n\t\"Main\": true,\n\t\"Dir\": \"/usr/local/src/ocm-operator\",\n\t\"GoMod\": \"/usr/local/src/ocm-operator/go.mod\",\n\t\"GoVersion\": \"1.19\"\n}\n: if you are not using go modules it is suggested to disable this linter"
level=info msg="[linters_context/goanalysis] analyzers took 2m40.622655468s with top 10 stages: buildir: 1m24.140985783s, exhaustive: 23.9564232s, gocritic: 16.126824461s, goimports: 9.339361858s, buildssa: 3.595645207s, nilness: 3.52651082s, inspect: 2.978616952s, fact_deprecated: 2.370868105s, fact_purity: 2.365729781s, printf: 2.349924287s"
level=info msg="[runner] Issues before processing: 119, after processing: 0"
level=info msg="[runner] Processors filtering stat (out/in): cgo: 119/119, filename_unadjuster: 119/119, exclude-rules: 60/98, skip_dirs: 119/119, autogenerated_exclude: 98/119, identifier_marker: 98/98, exclude: 98/98, path_prettifier: 119/119, skip_files: 119/119, nolint: 0/60"
level=info msg="[runner] processing took 11.234608ms with stages: nolint: 4.127542ms, exclude-rules: 3.911522ms, identifier_marker: 1.853522ms, autogenerated_exclude: 687.477µs, path_prettifier: 510.265µs, skip_dirs: 128.543µs, cgo: 7.1µs, filename_unadjuster: 5.647µs, max_same_issues: 713ns, skip_files: 336ns, source_code: 288ns, fixer: 243ns, uniq_by_line: 227ns, severity-rules: 200ns, exclude: 186ns, max_from_linter: 158ns, diff: 151ns, path_shortener: 147ns, sort_results: 135ns, max_per_file_from_linter: 112ns, path_prefixer: 94ns"
level=info msg="[runner] linters took 23.901686177s with stages: goanalysis_metalinter: 23.890380382s"
level=info msg="File cache stats: 40 entries of total size 152.4KiB"
level=info msg="Memory: 581 samples, avg is 827.1MB, max is 2675.6MB"
level=info msg="Execution took 58.531925923s"

I have confirmed that when I introduce lint errors into the source code, they are correctly identified once GOPATH is set.

(The warning from gomoddirectives can be ignored, this appears to be a separate bug where gomoddirectives is attempting to parse output from go list -m -json but choking on a warning being output).

Please let me know if there's any additional information I can provide, or if you'd like me to try to narrow anything down further.

Thank you for all your work on golangci-lint!

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.52.2 built with go1.20.2 from da04413a on 2023-03-25T18:11:28Z

Configuration file

$ cat .golangci.yml
---
linters-settings:
  dupl:
    threshold: 200
  funlen:
    lines: 100
    statements: 50
  gci:
    local-prefixes: github.com/ocm-operator/ocm-operator
  goconst:
    min-len: 2
    min-occurrences: 2
  gocritic:
    enabled-tags:
      - diagnostic
      - experimental
      - opinionated
      - performance
      - style
    disabled-checks:
      - dupImport  # https://github.com/go-critic/go-critic/issues/845
      - ifElseChain
      - octalLiteral
      - whyNoLint
      - wrapperFunc
  gocyclo:
    min-complexity: 15
  goimports:
    local-prefixes: github.com/rh-mobb/ocm-operator
  gomnd:
    settings:
      mnd:
        # don't include the "operation" and "assign"
        checks:
          - argument
          - case
          - condition
          - return
  govet:
    check-shadowing: true
  lll:
    line-length: 140
  maligned:
    suggest-new: true
  misspell:
    locale: US
  nolintlint:
    allow-leading-space: true
    allow-unused: false
    require-explanation: false
    require-specific: false
  staticcheck:
    checks:
      - all

linters:
  # please, do not use `enable-all`: it's deprecated and will be removed soon.
  # inverted configuration with `enable-all` and `disable` is not scalable
  ##  during updates of golangci-lint
  disable-all: true
  enable:
    - asciicheck
    - bodyclose
    - cyclop
    - depguard
    - dogsled
    - dupl
    - durationcheck
    - errcheck
    - errorlint
    - exhaustive
    - exportloopref
    - forbidigo
    - forcetypeassert
    - funlen
    - gocognit
    - goconst
    - gocritic
    - gocyclo
    - godot
    - goerr113
    - goheader
    - goimports
    - gofmt
    - gomnd
    - gomoddirectives
    - gomodguard
    - goprintffuncname
    - gosec
    - gosimple
    - govet
    - importas
    - ineffassign
    - lll
    - makezero
    - misspell
    - nakedret
    - nestif
    - nilerr
    - nlreturn
    - nolintlint
    - paralleltest
    - prealloc
    - predeclared
    - staticcheck
    - stylecheck
    - testpackage
    - thelper
    - tparallel
    - typecheck
    - unconvert
    - unparam
    - unused
    - whitespace
    - wrapcheck

    # ISSUES: the following have global issues and are disabled.
    # - exhaustruct:      too restrictive on how to define structures
    # - gci:              many incorrect errors found different between CI pipeline and locally
    # - godox:            allow TODOs in code
    # - revive:           allow const type = value format for defining constants and variables
    # - gochecknoglobals: kubebuilder uses globals by default; be consistent
    # - gochecknoinits:   kubebuilder uses init by default; be consistent

    # OVERRIDDEN: the following are overriden due to other linters being enabled.
    # - rowserrcheck
    # - sqlclosecheck
    # - wastedassign
    # - wsl

issues:
  # excluding configuration per-path, per-linter, per-text and per-source
  exclude-rules:
    - path: _test\.go
      linters:
        - gomnd
        - funlen
        - scopelint
        - dupl
        - gochecknoglobals
        - testpackage
    - path: pkg/ocm/machine_pool\.go
      linters:
        - dupl
    - path: pkg/ocm/node_pool\.go
      linters:
        - dupl

run:
  skip-dirs: []

Go environment

$ go version && go env
go version go1.20.4 linux/amd64                                                                                                                                                                      
GO111MODULE=""                                                                                                                                                                                      
GOARCH="amd64"                                                                                                                                                                                      
GOBIN=""                                                                                                                                                                                            
GOCACHE="/root/.cache/go-build"                                                                                                                                                                     
GOENV="/root/.config/go/env"                                                                                                                                                                        
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"                                                                                                                                                                                     
GOINSECURE=""                                                                                                                                                                                        
GOMODCACHE="/root/go/pkg/mod"                                                                                                                                                                        
GONOPROXY=""                                                                                                                                                                                         
GONOSUMDB=""                                                                                                                                                                                         
GOOS="linux"                                                                                                                                                                                         
GOPATH="/root/go"                                                                                                                                                                                    
GOPRIVATE=""                                                                                                                                                                                         
GOPROXY="https://proxy.golang.org,direct"                                                                                                                                                            
GOROOT="/root/go"                                                                                                                                                                                    
GOSUMDB="sum.golang.org"                                                                                                                                                                             
GOTMPDIR=""                                                                                                                                                                                          
GOTOOLDIR="/root/go/pkg/tool/linux_amd64"                                                                                                                                                            
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/usr/local/src/ocm-operator/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 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build743681851=/tmp/go-build -gno-record-gcc-switches"      

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
level=info msg="[config_reader] Config search paths: [./ /usr/local/src/ocm-operator /usr/local/src /usr/local /usr / /root]"
level=info msg="[config_reader] Used config file .golangci.yml"
level=info msg="[lintersdb] Active 54 linters: [asciicheck bodyclose cyclop depguard dogsled dupl durationcheck errcheck errorlint exhaustive exportloopref forbidigo forcetypeassert funlen gocognit goconst gocritic gocyclo godot goerr113 gofmt goheader goimports gomnd gomoddirectives gomodguard goprintffuncname gosec gosimple govet importas ineffassign lll makezero misspell nakedret nestif nilerr nlreturn nolintlint paralleltest prealloc predeclared staticcheck stylecheck testpackage thelper tparallel typecheck unconvert unparam unused whitespace wrapcheck]"
level=info msg="[loader] Go packages loading at mode 575 (compiled_files|deps|files|name|types_sizes|exports_file|imports) took 184.750981ms"
level=error msg="Running error: context loading failed: no go files to analyze"
level=info msg="Memory: 3 samples, avg is 31.4MB, max is 31.4MB"
level=info msg="Execution took 188.214406ms"

Code example or link to a public repository

https://github.com/rh-mobb/ocm-operator/tree/e26aad059f3772f608a9388522efdf314c5fd461

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