golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
15.61k stars 1.39k forks source link

typecheck: support Go 1.20 #3420

Closed codefromthecrypt closed 1 year ago

codefromthecrypt commented 1 year ago

Your feature request related to a problem? Please describe.

I tried to run linters that pass now, on go 1.20rc1, but some are failing suspiciously in typecheck.

For example, certain exported symbols in the same package show up as undeclared.

imports/go/gojs.go:30:32: undeclared name: `RoundTripperKey` (typecheck)
    return context.WithValue(ctx, RoundTripperKey{}, rt)
                                  ^

Another example, is it seems you have to add return after a panic now?

        panic(fmt.Sprintf("TODO: fetchPromise.%s", method))
+       return nil, nil

I stopped here

Describe the solution you'd like.

I would like to be able to test release candidates

Describe alternatives you've considered.

waiting

Additional context.

I installed go using gimme 1.20rc1 and ran make lint on https://github.com/tetratelabs/wazero

boring-cyborg[bot] commented 1 year ago

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

aarongable commented 1 year ago

@ldez Can you provide some insight into what causes these typecheck failures when running golangci-lint on a brand-new version of Go? Nothing about generics, interfaces, or exported symbols changed in this release. Why does the Go version bump cause problems for typecheck? If we can figure that out, then maybe we can avoid having to do this "can't run golangci-lint on Go release candidates" dance every time a new version comes out.

bombsimon commented 1 year ago

We have some documentation work to do because this is a very common question.

In short typechec is not a real linter but a way for golangci-lint to report compilation errors. In the majority of cases when you get typecheck issues your code does not compile when doing go build ./... on the same machine.

If something breaks in a version bump it's most likely because the code uses some kind of feature that's not supported in the go version golangci-lint was compiled with.

aarongable commented 1 year ago

Apologies for the ping, you're totally right.

My confusion arises because my project consumes golang-ci lint by building it ourselves. We produce a container image with a specific version of Go that we want to test on, then use go install to get golangci-lint, then run it against our codebase. In the same container image, all of the project code compiles and tests just fine.

bombsimon commented 1 year ago

Yeah I can undertand the confusion. Thanks for the extra info. I did a quick local test with go1.20, building golangcilintfrom master and also tried from thev1.50.1` tag, and it seem to be working fine.

$ go1.20rc1 version
go version go1.20rc1 darwin/arm64
$ go1.20rc1 build -o /tmp/golangci-lint ./cmd/golangci-lint/
$ /tmp/golangci-lint run ./...
test/akamai-test-srv/main.go:95:15: G114: Use of net/http serve function that has no support for setting timeouts (gosec)
        go log.Fatal(http.ListenAndServe(*listenAddr, nil))
                     ^
test/s3-test-srv/main.go:96:15: G114: Use of net/http serve function that has no support for setting timeouts (gosec)
        go log.Fatal(http.ListenAndServe(*listenAddr, nil))
                     ^
test/mail-test-srv/main.go:227:10: G114: Use of net/http serve function that has no support for setting timeouts (gosec)
                err := http.ListenAndServe(*listenAPI, http.DefaultServeMux)
                       ^

Are you able to reproduce this locally yourself?

aarongable commented 1 year ago

Hmm, yep, I can repeat those steps and get those results (with no errors, because I've fixed those three gosec findings) when running locally:

$ where golangci-lint
/home/aaron/.local/share/go/bin/golangci-lint

$ strings /home/aaron/.local/share/go/bin/golangci-lint | grep 'go1\.20rc1' | head -n1
go1.20rc1

$ golangci-lint run ./...

But doing the same exact thing inside the container results in an error related to the netip package:

$ BOULDER_TOOLS_TAG=go1.20rc1_2022-12-14 docker-compose run boulder which golangci-lint
<snip>
/usr/local/bin/golangci-lint

$ BOULDER_TOOLS_TAG=go1.20rc1_2022-12-14 docker-compose run boulder strings /usr/local/bin/golangci-lint | grep 'go1\.20rc1'
<snip>
go1.20rc1

$ BOULDER_TOOLS_TAG=go1.20rc1_2022-12-14 docker-compose run boulder /usr/local/bin/golangci-lint run ./...
<snip>
ERRO [runner] Panic: buildir: package "netip" (isInitialPkg: false, needAnalyzeSource: true): in net/netip.AddrFromSlice: cannot convert Load <[]byte> t0 ([]byte) to [4]byte: goroutine 8525 [running]:
runtime/debug.Stack()
    /usr/local/go/src/runtime/debug/stack.go:24 +0x65
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()
    /root/go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_action.go:102 +0x155
panic({0x14fe300, 0xc00141e810})
    /usr/local/go/src/runtime/panic.go:884 +0x213
honnef.co/go/tools/go/ir.emitConv(0xc0030b0b40, {0x19396c8, 0xc004068a80}, {0x192cbb0?, 0xc0064fc720}, {0x192c2a0, 0xc00ade4140})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/emit.go:293 +0xd29
honnef.co/go/tools/go/ir.(*builder).expr0(0xc00364da28, 0xc0030b0b40, {0x192ff70?, 0xc00ade4140?}, {0x7, {0x192cbb0, 0xc0064fc720}, {0x0, 0x0}})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:610 +0x5d7
honnef.co/go/tools/go/ir.(*builder).expr(0x192cd40?, 0xc0030b0b40, {0x192ff70, 0xc00ade4140})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:566 +0x1fa
honnef.co/go/tools/go/ir.(*builder).emitCallArgs(0x192cd40?, 0xc0030b0b40, 0xc00a090000, 0xc00ade4180, {0x0?, 0x0, 0x8b80c7?})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:975 +0x135
honnef.co/go/tools/go/ir.(*builder).setCall(0x1567e20?, 0xc0030b0b40, 0xc00ade4180, 0xc003797c68)
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:1037 +0x8e
honnef.co/go/tools/go/ir.(*builder).expr0(0xc00364da28, 0xc0030b0b40, {0x192ff70?, 0xc00ade4180?}, {0x7, {0x192cca0, 0xc00928fe30}, {0x0, 0x0}})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:623 +0x218e
honnef.co/go/tools/go/ir.(*builder).expr(0xc00364ce78?, 0xc0030b0b40, {0x192ff70, 0xc00ade4180})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:566 +0x1fa
honnef.co/go/tools/go/ir.(*builder).stmt(0xc0030b0b40?, 0xc0030b0b40, {0x19304b0?, 0xc0060273a0?})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2291 +0x194c
honnef.co/go/tools/go/ir.(*builder).stmtList(...)
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:855
honnef.co/go/tools/go/ir.(*builder).switchStmt(0xc00364d308?, 0xc0030b0b40, 0xc00b0a77d0, 0x0)
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:1366 +0x19a5
honnef.co/go/tools/go/ir.(*builder).stmt(0x20?, 0xc0030b0b40, {0x1930600?, 0xc00b0a77d0?})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2365 +0x121c
honnef.co/go/tools/go/ir.(*builder).stmtList(0x8bea45?, 0xc00364d4f8?, {0xc006027560?, 0x2, 0x8c0650?})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:855 +0x45
honnef.co/go/tools/go/ir.(*builder).stmt(0xc0030b0b40?, 0xc0030b0b40, {0x192ff10?, 0xc00b0a7800?})
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2338 +0x9b9
honnef.co/go/tools/go/ir.(*builder).buildFunction(0xc00364da28, 0xc0030b0b40)
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2450 +0x3d7
honnef.co/go/tools/go/ir.(*builder).buildFuncDecl(0x0?, 0xc00584d4d0, 0xc00b0a7830)
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2488 +0x19b
honnef.co/go/tools/go/ir.(*Package).build(0xc00584d4d0)
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2594 +0xc16
sync.(*Once).doSlow(0xc0040aa000?, 0xc005d40730?)
    /usr/local/go/src/sync/once.go:74 +0xc2
sync.(*Once).Do(...)
    /usr/local/go/src/sync/once.go:65
honnef.co/go/tools/go/ir.(*Package).Build(...)
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2512
honnef.co/go/tools/internal/passes/buildir.run(0xc00f231860)
    /root/go/pkg/mod/honnef.co/go/tools@v0.3.3/internal/passes/buildir/buildir.go:86 +0x368
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0xc0050fd300)
    /root/go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_action.go:188 +0x9d6
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2()
    /root/go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_action.go:106 +0x1d
github.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc002932ff0, {0x16c9b40, 0x7}, 0xc0032ef748)
    /root/go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/timeutils/stopwatch.go:111 +0x4a
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0xc00097e780?)
    /root/go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_action.go:105 +0x85
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0xc0050fd300)
    /root/go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0xb4
created by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze
    /root/go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x1eb
WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: buildir: package "netip" (isInitialPkg: false, needAnalyzeSource: true): in net/netip.AddrFromSlice: cannot convert Load <[]byte> t0 ([]byte) to [4]byte
ERRO Running error: 1 error occurred:
    * can't run linter goanalysis_metalinter: goanalysis_metalinter: buildir: package "netip" (isInitialPkg: false, needAnalyzeSource: true): in net/netip.AddrFromSlice: cannot convert Load <[]byte> t0 ([]byte) to [4]byte

Anyway, it seems likely this isn't strictly a golangci-lint issue at this point, it's likely more closely related to our container environment. Thanks for your help, and let me know if you've got any ideas!

atc0005 commented 1 year ago

Are you able to reproduce this locally yourself?

Additional notes (in case they provide any additional details):

cd $HOME/Desktop
git clone https://github.com/rs/zerolog
cd zerolog
docker image pull golang:1.20rc1
docker container run -it --rm -v $PWD:$PWD -w $PWD golang:1.20rc1
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1
golangci-lint --version
golangci-lint run ./...

Output from the last command:

root@8243eca418db:/home/ubuntu/Desktop/zerolog# golangci-lint run ./...
ERRO [runner] Panic: buildir: package "netip" (isInitialPkg: false, needAnalyzeSource: true): in net/netip.AddrFromSlice: cannot convert Load <[]byte> t0 ([]byte) to [4]byte: goroutine 1673 [running]:
runtime/debug.Stack()
    /usr/local/go/src/runtime/debug/stack.go:24 +0x65
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func1()
    /go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_action.go:102 +0x155
panic({0x14fd300, 0xc003570ee0})
    /usr/local/go/src/runtime/panic.go:884 +0x213
honnef.co/go/tools/go/ir.emitConv(0xc0024db7c0, {0x19386c8, 0xc0028df620}, {0x192bbb0?, 0xc00b142c30}, {0x192b2a0, 0xc011ba8180})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/emit.go:293 +0xd29
honnef.co/go/tools/go/ir.(*builder).expr0(0xc000023a28, 0xc0024db7c0, {0x192ef70?, 0xc011ba8180?}, {0x7, {0x192bbb0, 0xc00b142c30}, {0x0, 0x0}})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:610 +0x5d7
honnef.co/go/tools/go/ir.(*builder).expr(0x192bd40?, 0xc0024db7c0, {0x192ef70, 0xc011ba8180})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:566 +0x1fa
honnef.co/go/tools/go/ir.(*builder).emitCallArgs(0x192bd40?, 0xc0024db7c0, 0xc0014d8000, 0xc011ba81c0, {0x0?, 0x0, 0x8b80a7?})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:975 +0x135
honnef.co/go/tools/go/ir.(*builder).setCall(0x1566e20?, 0xc0024db7c0, 0xc011ba81c0, 0xc0028d1ee8)
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:1037 +0x8e
honnef.co/go/tools/go/ir.(*builder).expr0(0xc000023a28, 0xc0024db7c0, {0x192ef70?, 0xc011ba81c0?}, {0x7, {0x192bca0, 0xc00aac91f0}, {0x0, 0x0}})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:623 +0x218e
honnef.co/go/tools/go/ir.(*builder).expr(0xc000022e78?, 0xc0024db7c0, {0x192ef70, 0xc011ba81c0})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:566 +0x1fa
honnef.co/go/tools/go/ir.(*builder).stmt(0xc0024db7c0?, 0xc0024db7c0, {0x192f4b0?, 0xc0081a3d80?})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2291 +0x194c
honnef.co/go/tools/go/ir.(*builder).stmtList(...)
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:855
honnef.co/go/tools/go/ir.(*builder).switchStmt(0x22770a0?, 0xc0024db7c0, 0xc0018025a0, 0x0)
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:1366 +0x19a5
honnef.co/go/tools/go/ir.(*builder).stmt(0x20?, 0xc0024db7c0, {0x192f600?, 0xc0018025a0?})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2365 +0x121c
honnef.co/go/tools/go/ir.(*builder).stmtList(0x8bea25?, 0xc0000234f8?, {0xc0081a3fc0?, 0x2, 0x8c0630?})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:855 +0x45
honnef.co/go/tools/go/ir.(*builder).stmt(0xc0024db7c0?, 0xc0024db7c0, {0x192ef10?, 0xc0018025d0?})
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2338 +0x9b9
honnef.co/go/tools/go/ir.(*builder).buildFunction(0xc000023a28, 0xc0024db7c0)
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2450 +0x3d7
honnef.co/go/tools/go/ir.(*builder).buildFuncDecl(0x0?, 0xc002414480, 0xc001802600)
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2488 +0x19b
honnef.co/go/tools/go/ir.(*Package).build(0xc002414480)
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2594 +0xc16
sync.(*Once).doSlow(0xc0039dc9a0?, 0xc004e60af0?)
    /usr/local/go/src/sync/once.go:74 +0xc2
sync.(*Once).Do(...)
    /usr/local/go/src/sync/once.go:65
honnef.co/go/tools/go/ir.(*Package).Build(...)
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/go/ir/builder.go:2512
honnef.co/go/tools/internal/passes/buildir.run(0xc002afef70)
    /go/pkg/mod/honnef.co/go/tools@v0.3.3/internal/passes/buildir/buildir.go:86 +0x368
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyze(0xc0014dfc40)
    /go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_action.go:188 +0x9d6
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe.func2()
    /go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_action.go:106 +0x1d
github.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc0011d09b0, {0x16c8b40, 0x7}, 0xc0018dc748)
    /go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/timeutils/stopwatch.go:111 +0x4a
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*action).analyzeSafe(0xc0010a47e0?)
    /go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_action.go:105 +0x85
github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze.func2(0xc0014dfc40)
    /go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_loadingpackage.go:80 +0xb4
created by github.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*loadingPackage).analyze
    /go/pkg/mod/github.com/golangci/golangci-lint@v1.50.1/pkg/golinters/goanalysis/runner_loadingpackage.go:75 +0x1eb 
WARN [runner] Can't run linter goanalysis_metalinter: goanalysis_metalinter: buildir: package "netip" (isInitialPkg: false, needAnalyzeSource: true): in net/netip.AddrFromSlice: cannot convert Load <[]byte> t0 ([]byte) to [4]byte 
ERRO Running error: 1 error occurred:
    * can't run linter goanalysis_metalinter: goanalysis_metalinter: buildir: package "netip" (isInitialPkg: false, needAnalyzeSource: true): in net/netip.AddrFromSlice: cannot convert Load <[]byte> t0 ([]byte) to [4]byte
shuLhan commented 1 year ago

@atc0005 I believe this is an issue that has been reported go-tools module and fixed [1].

[1] https://github.com/dominikh/go-tools/issues/1335

codefromthecrypt commented 1 year ago

I think this issue will start getting spam levels of interest now that go 1.20 is out

codefromthecrypt commented 1 year ago

thanks tons @ldez