セキュリティ系の勉強・その他開発メモとか雑談. Twitter, ブログカテゴリ一覧
本ブログはあくまでセキュリティに関する情報共有の一環として作成したものであり,公開されているシステム等に許可なく実行するなど、違法な行為を助長するものではありません.

go1.18を使ったときに、golangci-lintのtypecheckのエラーが出たり、go generateがこけた

//

golangci-lintのtypecheckのエラー

go.modで、go1.16が指定されているプロジェクトで、手元のgoのバージョンを1.18.2にしてgolangci-lintを実行したときに、今まで出ていなかったエラーが発生しました。こんな感じ

...net/http/httptest (-: could not load export data: no export data for "net/http/httptest") (typecheck)
    "net/http/httptest"
    ^
k8sapiserver/k8sapiserver.go:23:13: could not import...load export data: no export data for "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes") (typecheck)
    authzmodes "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
               ^...
...

go1.16.15go.17.10では、上記のエラーは発生していませんでした。

対処

golangci-lintのバージョンを最新に上げました。(ちょうど別件でgo.modに記載のバージョンも上げたので念のため記載しておきます。)

# go.mod

- go 1.16
+ go 1.17

-   github.com/golangci/golangci-lint v1.41.1
+   github.com/golangci/golangci-lint v1.46.2

上記の対応により、手元のgoバージョンgo1.18.2で、先のエラーは発生しなくなりました。

go generateがこけた

上記の続きで、go1.18.2を使ったときにgo generateを実行したときに以下のエラーが発生しました。

go generate ./...
prog.go:12:2: cannot find package "." in:
        /workspace/kube-scheduler-simulator/simulator/vendor/github.com/golang/mock/mockgen/model
Please reference the steps in the README to fix this error:
        https://github.com/golang/mock#reflect-vendoring-error.prog.go:12:2: cannot find package "." in:
        /workspace/kube-scheduler-simulator/simulator/vendor/github.com/golang/mock/mockgen/model
Please reference the steps in the README to fix this error:
        https://github.com/golang/mock#reflect-vendoring-error.prog.go:12:2: no required module provides package github.com/golang/mock/mockgen/model: go.mod file not found in current directory or any parent directory; see 'go help modules'
prog.go:14:2: no required module provides package sigs.k8s.io/kube-scheduler-simulator/simulator/export: go.mod file not found in current directory or any parent directory; see 'go help modules'
2022/07/10 14:41:06 Loading input failed: exit status 1
export/export.go:3: running "mockgen": exit status 1
prog.go:12:2: cannot find package "." in:
        /workspace/kube-scheduler-simulator/simulator/vendor/github.com/golang/mock/mockgen/model
Please reference the steps in the README to fix this error:
        https://github.com/golang/mock#reflect-vendoring-error.prog.go:12:2: cannot find package "." in:
        /workspace/kube-scheduler-simulator/simulator/vendor/github.com/golang/mock/mockgen/model
Please reference the steps in the README to fix this error:
        https://github.com/golang/mock#reflect-vendoring-error.prog.go:12:2: no required module provides package github.com/golang/mock/mockgen/model: go.mod file not found in current directory or any parent directory; see 'go help modules'
prog.go:14:2: no required module provides package sigs.k8s.io/kube-scheduler-simulator/simulator/node: go.mod file not found in current directory or any parent directory; see 'go help modules'
2022/07/10 14:41:07 Loading input failed: exit status 1
node/node.go:3: running "mockgen": exit status 1
prog.go:12:2: cannot find package "." in:
        /workspace/kube-scheduler-simulator/simulator/vendor/github.com/golang/mock/mockgen/model
Please reference the steps in the README to fix this error:
        https://github.com/golang/mock#reflect-vendoring-error.prog.go:12:2: cannot find package "." in:
        /workspace/kube-scheduler-simulator/simulator/vendor/github.com/golang/mock/mockgen/model
Please reference the steps in the README to fix this error:
        https://github.com/golang/mock#reflect-vendoring-error.prog.go:12:2: no required module provides package github.com/golang/mock/mockgen/model: go.mod file not found in current directory or any parent directory; see 'go help modules'
prog.go:14:2: no required module provides package sigs.k8s.io/kube-scheduler-simulator/simulator/replicateexistingcluster: go.mod file not found in current directory or any parent directory; see 'go help modules'
2022/07/10 14:41:08 Loading input failed: exit status 1
replicateexistingcluster/replicateexistingcluster.go:3: running "mockgen": exit status 1
prog.go:12:2: cannot find package "." in:
        /workspace/kube-scheduler-simulator/simulator/vendor/github.com/golang/mock/mockgen/model
Please reference the steps in the README to fix this error:
        https://github.com/golang/mock#reflect-vendoring-error.prog.go:12:2: cannot find package "." in:
        /workspace/kube-scheduler-simulator/simulator/vendor/github.com/golang/mock/mockgen/model
Please reference the steps in the README to fix this error:
        https://github.com/golang/mock#reflect-vendoring-error.prog.go:12:2: no required module provides package github.com/golang/mock/mockgen/model: go.mod file not found in current directory or any parent directory; see 'go help modules'
prog.go:14:2: no required module provides package sigs.k8s.io/kube-scheduler-simulator/simulator/scheduler/plugin: go.mod file not found in current directory or any parent directory; see 'go help modules'
2022/07/10 14:41:09 Loading input failed: exit status 1
scheduler/plugin/wrappedplugin.go:13: running "mockgen": exit status 1
make: *** [Makefile:3: generate] Error 1

go1.16.15を使用していたときは発生していませんでしたが、こちらgo1.18.2を使用し始めたときに発生するようになりました。

対処

ここにある2つのうちのどちらかで対処可能です。

dev.to

例えば以下のように修正します。

- //go:generate mockgen -destination=./mock_$GOPACKAGE/pod.go . PodService
+ //go:generate mockgen --build_flags=--mod=mod -destination=./mock_$GOPACKAGE/pod.go . PodService