Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undeclared name: false positive (typecheck) #3185

Closed
4 tasks done
Integralist opened this issue Sep 2, 2022 · 11 comments
Closed
4 tasks done

undeclared name: false positive (typecheck) #3185

Integralist opened this issue Sep 2, 2022 · 11 comments
Labels
question Further information is requested

Comments

@Integralist
Copy link

Integralist commented Sep 2, 2022

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. (https://golangci-lint.run/usage/linters/)

Description of the problem

I have a package called compute which is split across multiple files.

  • pkg/commands/compute/init.go
  • pkg/commands/compute/language.go

The init.go file reports:

undeclared name: `NewLanguages` typecheck [150, 15]

But the NewLanguages type is a declared function in the same package (it's defined inside language.go).

Version of golangci-lint

I installed the binary using go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest.

$ golangci-lint --version
golangci-lint has version v1.48.0 built from (unknown, mod sum: "h1:hRiBNk9iRqdAKMa06ntfEiLyza1/3IE9rHLNJaek4a8=") on (unknown)

Configuration file

$ cat ~/.golangci.yml
# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
# https://golangci-lint.run/usage/linters/#enabled-by-default-linters
linters:
  enable:
    - deadcode
    - errcheck
    - gosimple
    - gofumpt
    - goimports
    - govet
    - ineffassign
    - revive
    - staticcheck
    - structcheck
    - typecheck
    - unused
    - varcheck

# https://github.com/mgechev/revive#configuration
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
#
linters-settings:
  revive:
    enableAllRules: true
    rules:
      - name: "add-constant"
        severity: "warning"
        arguments: [{ maxLitCount = "10", allowInts = "0,1,2,3,4,5" }] # e.g. avoid matching `if len(...) > 0` and other simple assignments.
        disabled: true # most of the time it's fine, and the performance isn't a concern
      - name: "argument-limit"
        severity: "warning"
        arguments: [5]
      - name: "atomic"
        severity: "warning"
      - name: "bare-return"
        severity: "warning"
      - name: "bool-literal-in-expr"
        severity: "warning"
      - name: "confusing-naming"
        severity: "warning"
      - name: "confusing-results"
        severity: "warning"
      - name: "constant-logical-expr"
        severity: "error"
      - name: "context-as-argument"
        severity: "error"
      - name: "context-keys-type"
        severity: "error"
      # - name: "datarace"
      #   severity: "error"
      - name: "deep-exit"
        severity: "warning"
      - name: "defer"
        severity: "warning"
      - name: "early-return"
        severity: "warning"
      - name: "empty-block"
        severity: "error"
      - name: "empty-lines"
        severity: "warning"
      - name: "error-naming"
        severity: "error"
      - name: "error-return"
        severity: "error"
      - name: "error-strings"
        severity: "error"
      - name: "errorf"
        severity: "warning"
      - name: "exported"
        severity: "error"
      - name: "flag-parameter"
        severity: "warning"
      - name: "function-result-limit"
        severity: "warning"
        arguments: [3]
      - name: "function-length"
        severity: "warning"
        arguments: [40,0]
      - name: "get-return"
        severity: "error"
      - name: "identical-branches"
        severity: "error"
      - name: "if-return"
        severity: "warning"
      - name: "increment-decrement"
        severity: "error"
      - name: "indent-error-flow"
        severity: "warning"
      - name: "import-shadowing"
        severity: "warning"
      - name: "modifies-parameter"
        severity: "warning"
      - name: "modifies-value-receiver"
        severity: "warning"
      - name: "nested-structs"
        severity: "warning"
      - name: "optimize-operands-order"
        severity: "warning"
      - name: "package-comments"
        severity: "warning"
      - name: "range"
        severity: "warning"
      - name: "range-val-in-closure"
        severity: "warning"
      - name: "range-val-address"
        severity: "warning"
      - name: "receiver-naming"
        severity: "warning"
      - name: "redefines-builtin-id"
        severity: "error"
      - name: "string-of-int"
        severity: "warning"
      - name: "struct-tag"
        severity: "warning"
      - name: "superfluous-else"
        severity: "warning"
      - name: "time-equal"
        severity: "warning"
      - name: "time-naming"
        severity: "warning"
      - name: "var-declaration"
        severity: "warning"
      - name: "var-naming"
        severity: "warning"
      - name: "unconditional-recursion"
        severity: "error"
      - name: "unexported-naming"
        severity: "warning"
      - name: "unexported-return"
        severity: "error"
      - name: "unhandled-error"
        severity: "warning"
      - name: "unnecessary-stmt"
        severity: "warning"
      - name: "unreachable-code"
        severity: "warning"
      - name: "unused-parameter"
        severity: "warning"
      - name: "unused-receiver"
        severity: "warning"
        disabled: true
      - name: "use-any"
        severity: "warning"
      - name: "useless-break"
        severity: "warning"
      - name: "waitgroup-by-value"
        severity: "warning"

Go environment

$ go version && go env
go version go1.19 darwin/arm64
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/integralist/Library/Caches/go-build"
GOENV="/Users/integralist/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/integralist/go/1.19.0/pkg/mod"
GONOPROXY="github.com/fastly"
GONOSUMDB="github.com/fastly"
GOOS="darwin"
GOPATH="/Users/integralist/go/1.19.0"
GOPRIVATE="github.com/fastly"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/integralist/.goenv/versions/1.19.0"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/integralist/.goenv/versions/1.19.0/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/integralist/Code/fastly/cli/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/lj/y1tlk7nd5tgcz5z00fycytjr0000gp/T/go-build864079446=/tmp/go-build -gno-record-gcc-switches -fno-common"

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /Users/integralist/Code/fastly/cli /Users/integralist/Code/fastly /Users/integralist/Code /Users/integralist /Users /]
INFO [config_reader] Used config file ../../../.golangci.yml
INFO [lintersdb] Active 13 linters: [deadcode errcheck gofumpt goimports gosimple govet ineffassign revive staticcheck structcheck typecheck unused varcheck]
INFO [loader] Go packages loading at mode 575 (compiled_files|deps|name|types_sizes|exports_file|files|imports) took 371.32775ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 17.900917ms
INFO [linters context/goanalysis] analyzers took 44.607480547s with top 10 stages: buildir: 11.512498869s, gofumpt: 1.6799765s, goimports: 1.378397379s, printf: 858.572835ms, directives: 851.93433ms, ctrlflow: 840.534696ms, S1038: 682.600328ms, unused: 625.604705ms, fact_purity: 592.380655ms, inspect: 569.932465ms
ERRO [runner] Panic: the_only_name: package "api" (isInitialPkg: true, needAnalyzeSource: true): interface conversion: interface {} is int, not string: goroutine 13707 [running]:
runtime/debug.Stack()
        /usr/local/go/src/runtime/debug/stack.go:24 +0x68
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/goanalysis/runner_action.go:101 +0x104
panic({0x104fde440, 0x1400ee53fb0})
        /usr/local/go/src/runtime/panic.go:838 +0x204
github.com/golangci/golangci-lint/pkg/golinters.safeTomlSlice(...)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/revive.go:243
github.com/golangci/golangci-lint/pkg/golinters.createConfigMap(0x14002138820)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/revive.go:218 +0x7a4
github.com/golangci/golangci-lint/pkg/golinters.getReviveConfig(0x1401f97c4e0?)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/revive.go:170 +0x60
github.com/golangci/golangci-lint/pkg/golinters.runRevive(0x140001e0fc0, 0x1401f97c4e0?, 0x14002138820)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/revive.go:80 +0x64
github.com/golangci/golangci-lint/pkg/golinters.NewRevive.func1.1(0x104fbe540?)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/revive.go:57 +0x40
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0x1400796bc70)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/goanalysis/runner_action.go:187 +0x96c
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2()
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/goanalysis/runner_action.go:105 +0x24
github.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0x140040ecf00, {0x104ceb946, 0xd}, 0x140033f0730)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/timeutils/stopwatch.go:111 +0x48
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0x1048246d0?)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/goanalysis/runner_action.go:104 +0x78
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0x1400796bc70)
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0xb4
created by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze
        /Users/integralist/go/pkg/mod/github.com/golangci/[email protected]/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x16c
WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: the_only_name: package "api" (isInitialPkg: true, needAnalyzeSource: true): interface conversion: interface {} is int, not string
WARN [linters context] structcheck is disabled because of generics. You can track the evolution of the generics support by following the https://github.com/golangci/golangci-lint/issues/2649.
INFO [runner] processing took 5.914µs with stages: max_same_issues: 2µs, nolint: 792ns, skip_dirs: 583ns, max_from_linter: 541ns, path_prefixer: 500ns, path_prettifier: 375ns, filename_unadjuster: 292ns, cgo: 208ns, skip_files: 125ns, identifier_marker: 83ns, severity-rules: 83ns, diff: 42ns, uniq_by_line: 42ns, autogenerated_exclude: 42ns, max_per_file_from_linter: 42ns, source_code: 41ns, exclude: 41ns, path_shortener: 41ns, exclude-rules: 41ns, sort_results: 0s
INFO [runner] linters took 3.930609875s with stages: goanalysis_metalinter: 3.9303975s, structcheck: 9.625µs
ERRO Running error: 1 error occurred:
        * can't run linter goanalysis_metalinter: goanalysis_metalinter: the_only_name: package "api" (isInitialPkg: true, needAnalyzeSource: true): interface conversion: interface {} is int, not string
 
INFO Memory: 45 samples, avg is 745.2MB, max is 1229.9MB
INFO Execution took 4.334657417s

Code example or link to a public repository

https://github.com/fastly/cli/blob/main/pkg/commands/compute/init.go#L150

@Integralist Integralist added the bug Something isn't working label Sep 2, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 2, 2022

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

@ldez
Copy link
Member

ldez commented Sep 2, 2022

Hello,

typecheck is not a real linter it's just a way to parse/display "compilation" and linters errors (linter reports are not errors).

@ldez
Copy link
Member

ldez commented Sep 2, 2022

The logs seem not related to the title and the description of the issue.

In the log, there is a panic from revive.

@ldez ldez added question Further information is requested and removed bug Something isn't working labels Sep 2, 2022
@ldez
Copy link
Member

ldez commented Sep 2, 2022

The revive panic comes because add-constant configuration is invalid:

I think that the typecheck issues are related to a problem with //go:embed config.toml, you have to run your target make config.

@ldez ldez closed this as completed Sep 2, 2022
@Integralist
Copy link
Author

In the log, there is a panic from revive.

Even if I remove revive, and re-run to see various messages from errcheck binary from within my editor (Neovim configured with the below) it will still report the undeclared name: NewLanguages typecheck [150, 15] warning which is actually quite distracting.

NOTE: After re-running golangci-lint run -v and seeing it not panic due to revive, I then put back my revive config into the ~/.golangci.yml and again re-ran golangci-lint run -v and it was the same output as when I had removed it. So not sure why that caused a panic earlier.

local lint = require("lint")

lint.linters_by_ft = {
  go = { "golangcilint" },
}

local golangcilint = require("lint.linters.golangcilint")
golangcilint.append_fname = true
golangcilint.args = {
  "run",
  "--out-format",
  "json",
}

@ldez
Copy link
Member

ldez commented Sep 2, 2022

I'm sure of the root cause of the panic, because I'm able to reproduce it with your configuration.

invalid configuration (panic)
      - name: "add-constant"
        severity: "warning"
        arguments: [{ maxLitCount = "10", allowInts = "0,1,2,3,4,5" }] # <--HERE (TOML syntax)
        disabled: true
valid configuration
      - name: add-constant
        severity: warning
        disabled: false
        arguments:
          - maxLitCount: "10"
            allowInts: "0,1,2,3,4,5"

I don't know how neovim work, but if you are running golangci-lint to only one file it will not work because the code must be compilable to be analyzed.

I'm still thinking that the typecheck issues are related to a problem with //go:embed config.toml, you have to run your target make config.

@ldez
Copy link
Member

ldez commented Sep 2, 2022

I recommend using v1.49.0 (support go1.19) instead of v1.48.0

@Integralist
Copy link
Author

Thanks I'll be sure to update versions.

re:

I don't know how neovim work, but if you are running golangci-lint to only one file it will not work because the code must be compilable to be analyzed.

I'm using the Neovim plugin https://github.com/mfussenegger/nvim-lint the configuration I'm using (shown earlier) looks to be passing the file name to golangci-lint.

So maybe that's why the typecheck linter is failing to pick up references across files (even if they're part of the same package).

The revive tool has a similar issue with it not working correctly on single files.

@Integralist
Copy link
Author

Interesting.

go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest does nothing 🤔 it seems like it doesn't see that there is a 1.49.0 release?

@ldez
Copy link
Member

ldez commented Sep 5, 2022

I don't recommend using latest, because latest is a moving reference.

go install -u github.com/golangci/golangci-lint/cmd/golangci-lint@latest

@wafaking

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants