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

Incorrect typecheck "imported but not used" finding #3921

Closed
4 tasks done
djm-google opened this issue Jun 20, 2023 · 3 comments
Closed
4 tasks done

Incorrect typecheck "imported but not used" finding #3921

djm-google opened this issue Jun 20, 2023 · 3 comments
Labels
question Further information is requested

Comments

@djm-google
Copy link

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

The typecheck linter reports an unused import in the google/go-attestation package, but the import is definitely used in the file that has the report:

$ git clone --quiet https://github.com/google/go-attestation
$ cd go-attestation/
$ golangci-lint run
attest/attest-tool/attest-tool.go:6:2: "crypto/ecdsa" imported but not used (typecheck)
	"crypto/ecdsa"
	^
$ grep -1 -n ecdsa attest/attest-tool/attest-tool.go
5-	"bytes"
6:	"crypto/ecdsa"
7-	"crypto/rand"
--
246-	switch pub := ek.Public.(type) {
247:	case *ecdsa.PublicKey:
248-		data, err := x509.MarshalPKIXPublicKey(pub)

There are build tags used elsewhere in this module, but not in this file (referencing issue #3891). Running go mod tidy doesn't help (referencing issue #2133).

I see this in the Github action (example) and when running locally.

Any suggestions?

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.53.3 built with go1.20.5 from 2dcd82f3 on 2023-06-15T10:50:11Z

Configuration file

$ cat .golangci.yml
cat: .golangci.yml: No such file or directory
$ cat ~/.golangci.yml
cat: /home/djm/.golangci.yml: No such file or directory

Go environment

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

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /home/djm/go-attestation /home/djm /home /] 
INFO [config_reader] Used config file .golangci.yaml 
INFO [lintersdb] Active 7 linters: [gofmt gosimple govet ineffassign staticcheck typecheck unused] 
INFO [loader] Go packages loading at mode 575 (imports|types_sizes|compiled_files|deps|files|exports_file|name) took 276.470587ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 20.677462ms 
INFO [linters_context/goanalysis] analyzers took 9.196922321s with top 10 stages: buildir: 7.305555419s, inspect: 610.145462ms, ctrlflow: 215.382642ms, printf: 189.421728ms, fact_purity: 149.638463ms, fact_deprecated: 141.105395ms, SA5012: 133.237886ms, nilness: 99.744016ms, typedness: 69.333115ms, S1039: 32.049188ms 
INFO [runner] Issues before processing: 142, after processing: 1 
INFO [runner] Processors filtering stat (out/in): severity-rules: 1/1, filename_unadjuster: 142/142, path_prettifier: 142/142, nolint: 141/141, max_from_linter: 1/1, sort_results: 1/1, cgo: 142/142, exclude: 142/142, max_same_issues: 1/1, fixer: 1/1, path_shortener: 1/1, path_prefixer: 1/1, autogenerated_exclude: 142/142, exclude-rules: 141/142, uniq_by_line: 1/141, source_code: 1/1, max_per_file_from_linter: 1/1, skip_files: 142/142, skip_dirs: 142/142, identifier_marker: 142/142, diff: 1/1 
INFO [runner] processing took 6.049552ms with stages: identifier_marker: 2.433569ms, exclude-rules: 2.243738ms, nolint: 808.425µs, autogenerated_exclude: 184.451µs, path_prettifier: 164.861µs, skip_dirs: 119.881µs, source_code: 50.662µs, cgo: 12.523µs, uniq_by_line: 11.257µs, filename_unadjuster: 10.052µs, max_same_issues: 2.551µs, max_from_linter: 1.99µs, path_shortener: 1.756µs, skip_files: 1.023µs, max_per_file_from_linter: 746ns, severity-rules: 555ns, exclude: 447ns, sort_results: 342ns, fixer: 314ns, diff: 284ns, path_prefixer: 125ns 
INFO [runner] linters took 4.132309074s with stages: goanalysis_metalinter: 4.126110896s 
attest/attest-tool/attest-tool.go:6:2: "crypto/ecdsa" imported and not used (typecheck)
	"crypto/ecdsa"
	^
INFO File cache stats: 1 entries of total size 7.8KiB 
INFO Memory: 46 samples, avg is 183.0MB, max is 248.4MB 
INFO Execution took 4.439548611s     

Code example or link to a public repository

https://github.com/google/go-attestation

@djm-google djm-google added the bug Something isn't working label Jun 20, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Jun 20, 2023

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

@ldez ldez added question Further information is requested and removed bug Something isn't working labels Jun 20, 2023
@ldez
Copy link
Member

ldez commented Jun 20, 2023

Hello,

You are using go1.19 locally and in your CI but the project sets go1.20 in its go.mod.

go1.20
$ docker run --rm -it golang:1.20-alpine sh
/go # apk add git curl
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz
(1/7) Installing brotli-libs (1.0.9-r9)
(2/7) Installing nghttp2-libs (1.51.0-r0)
(3/7) Installing libcurl (8.1.2-r0)
(4/7) Installing curl (8.1.2-r0)
(5/7) Installing libexpat (2.5.0-r0)
(6/7) Installing pcre2 (10.42-r0)
(7/7) Installing git (2.38.5-r0)
Executing busybox-1.35.0-r29.trigger
OK: 16 MiB in 23 packages
/go # curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.3
golangci/golangci-lint info checking GitHub for tag 'v1.53.3'
golangci/golangci-lint info found version: 1.53.3 for v1.53.3/linux/amd64
golangci/golangci-lint info installed /go/bin/golangci-lint
/go # git clone --quiet https://github.com/google/go-attestation
/go # cd go-attestation/
/go/go-attestation # golangci-lint run -v
INFO [config_reader] Config search paths: [./ /go/go-attestation /go / /root]
INFO [config_reader] Used config file .golangci.yaml
INFO [lintersdb] Active 7 linters: [gofmt gosimple govet ineffassign staticcheck typecheck unused]
INFO [loader] Go packages loading at mode 575 (exports_file|imports|types_sizes|compiled_files|deps|files|name) took 3.400675401s
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 6.68849ms
INFO [linters_context/goanalysis] analyzers took 3.129663811s with top 10 stages: buildir: 2.084247687s, inspect: 106.394397ms, printf: 58.111377ms, ctrlflow: 56.16251ms, gofmt: 51.700404ms, fact_deprecated: 49.937577ms, fact_purity: 47.73155ms, SA5012: 43.385057ms, S1038: 42.139872ms, nilness: 39.672569ms
INFO [runner] Issues before processing: 1, after processing: 0
INFO [runner] Processors filtering stat (out/in): autogenerated_exclude: 1/1, exclude: 1/1, exclude-rules: 0/1, identifier_marker: 1/1, skip_files: 1/1, cgo: 1/1, filename_unadjuster: 1/1, path_prettifier: 1/1, skip_dirs: 1/1
INFO [runner] processing took 96.535µs with stages: autogenerated_exclude: 28.105µs, identifier_marker: 24.048µs, exclude-rules: 13.604µs, skip_dirs: 13.56µs, path_prettifier: 12.16µs, cgo: 976ns, nolint: 565ns, max_same_issues: 492ns, diff: 474ns, filename_unadjuster: 422ns, skip_files: 328ns, fixer: 307ns, source_code: 281ns, uniq_by_line: 263ns, exclude: 191ns, severity-rules: 190ns, path_shortener: 170ns, max_from_linter: 137ns, sort_results: 109ns, max_per_file_from_linter: 82ns, path_prefixer: 71ns
INFO [runner] linters took 990.624893ms with stages: goanalysis_metalinter: 990.483942ms
INFO File cache stats: 0 entries of total size 0B
INFO Memory: 46 samples, avg is 76.8MB, max is 269.5MB
INFO Execution took 4.401110727s                  
/go/go-attestation # go build ./...
/go/go-attestation #
go1.19
$ docker run --rm -it golang:1.19-alpine sh
/go # apk add git curl
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/x86_64/APKINDEX.tar.gz
(1/7) Installing brotli-libs (1.0.9-r6)
(2/7) Installing nghttp2-libs (1.47.0-r0)
(3/7) Installing libcurl (8.1.2-r0)
(4/7) Installing curl (8.1.2-r0)
(5/7) Installing expat (2.5.0-r0)
(6/7) Installing pcre2 (10.40-r0)
(7/7) Installing git (2.36.6-r0)
Executing busybox-1.35.0-r17.trigger
OK: 20 MiB in 22 packages
/go # curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.3
golangci/golangci-lint info checking GitHub for tag 'v1.53.3'
golangci/golangci-lint info found version: 1.53.3 for v1.53.3/linux/amd64
golangci/golangci-lint info installed /go/bin/golangci-lint
/go # git clone --quiet https://github.com/google/go-attestation
/go # cd go-attestation/
/go/go-attestation # golangci-lint run -v
INFO [config_reader] Config search paths: [./ /go/go-attestation /go / /root] 
INFO [config_reader] Used config file .golangci.yaml 
INFO [lintersdb] Active 7 linters: [gofmt gosimple govet ineffassign staticcheck typecheck unused] 
INFO [loader] Go packages loading at mode 575 (types_sizes|compiled_files|files|name|deps|exports_file|imports) took 1.801918445s 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 7.154419ms 
INFO [linters_context/goanalysis] analyzers took 1.980360834s with top 10 stages: buildir: 1.427504661s, inspect: 86.960384ms, ctrlflow: 51.102549ms, fact_purity: 47.180505ms, printf: 46.705053ms, fact_deprecated: 44.659417ms, SA5012: 42.670369ms, nilness: 36.978ms, typedness: 34.93439ms, gofmt: 4.282696ms 
INFO [runner] Issues before processing: 291, after processing: 3 
INFO [runner] Processors filtering stat (out/in): uniq_by_line: 3/291, max_per_file_from_linter: 3/3, severity-rules: 3/3, filename_unadjuster: 291/291, path_prettifier: 291/291, autogenerated_exclude: 291/291, path_prefixer: 3/3, skip_dirs: 291/291, source_code: 3/3, path_shortener: 3/3, max_same_issues: 3/3, max_from_linter: 3/3, fixer: 3/3, identifier_marker: 291/291, nolint: 291/291, diff: 3/3, exclude-rules: 291/291, sort_results: 3/3, cgo: 291/291, skip_files: 291/291, exclude: 291/291 
INFO [runner] processing took 5.267985ms with stages: identifier_marker: 2.299534ms, exclude-rules: 2.282836ms, nolint: 496.967µs, path_prettifier: 93.415µs, skip_dirs: 34.231µs, source_code: 29.079µs, cgo: 7.812µs, uniq_by_line: 7.523µs, filename_unadjuster: 5.816µs, autogenerated_exclude: 5.78µs, max_same_issues: 1.888µs, path_shortener: 827ns, max_from_linter: 547ns, max_per_file_from_linter: 469ns, skip_files: 272ns, exclude: 266ns, fixer: 193ns, severity-rules: 177ns, diff: 177ns, sort_results: 100ns, path_prefixer: 76ns 
INFO [runner] linters took 824.960401ms with stages: goanalysis_metalinter: 819.641047ms 
attest/attest-tool/attest-tool.go:6:2: "crypto/ecdsa" imported and not used (typecheck)
        "crypto/ecdsa"
        ^
attest/challenge_test.go:6:2: "crypto/rsa" imported and not used (typecheck)
        "crypto/rsa"
        ^
../../usr/local/go/src/runtime/cgo/cgo.go:31:8: could not import C (cgo preprocessing failed) (typecheck)
import "C"
       ^
INFO File cache stats: 3 entries of total size 11.9KiB 
INFO Memory: 28 samples, avg is 90.2MB, max is 265.5MB 
INFO Execution took 2.636876381s                  
/go/go-attestation # 
/go/go-attestation # go build ./...
../pkg/mod/github.com/google/go-tpm@v0.3.4-0.20230613064043-511507721cb1/legacy/tpm2/credactivation/credential_activation.go:23:2: package crypto/ecdh is not in GOROOT (/usr/local/go/src/crypto/ecdh)

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

To see your problem you can run go build ./...

@ldez ldez closed this as completed Jun 20, 2023
@ldez
Copy link
Member

ldez commented Jun 20, 2023

google/go-attestation#336

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

2 participants