きっかけ
reviewdog とは、 linter などの結果を Pull Request のコメントで指摘してくれるイケてるツールです。
CI で lint するのとは違って、指摘をあえて無視して merge することもできる適度なゆるさがお気に入りです。
このように Pull Request にコメントを付けるためには、reviewdog を CI で動かす必要があります。
CircleCI との連携のやり方はドキュメントに書いてる通りです。
https://github.com/haya14busa/reviewdog#circle-ci
しかし、Go のプロジェクトにおいて次のように linter を設定しましたが、最初 golint しか動かず、他の linter の結果が来ない 現象がおきました。
runner:
golint:
cmd: golint $(go list ./...)
megacheck:
cmd: megacheck $(go list ./...)
errorformat:
- "%f:%l:%c:%m"
errcheck:
cmd: errcheck -asserts -ignoretests -blank $(go list ./...)
errorformat:
- "%f:%l:%c:%m"
ローカル環境でやると megacheck, errcheck の指摘が出るのに、CirlceCI のログだと golint しか出ていませんでした。
#!/bin/bash -eo pipefail
./reviewdog -ci="circle-ci" -conf=.reviewdog.yml
cmd/get_spreadsheet_token.go:12:6: [golint] exported type Dummy should have comment or be unexported
cmd/get_spreadsheet_token.go:13:2: [golint] struct field testJson should be testJSON
原因: dep ensure
をする前に reviewdog を動かしていた
CircleCI は毎回ソースコードを取り直すので vendor/ 以下を再取得するのですが、その前に megacheck
, errcheck
を実行するとエラーとなって reviewdog の結果に出なかったということが原因でした。reviewdog ではなく megacheck 単体で CircleCI に実行させてみる ことで発見しました。
#!/bin/bash -eo pipefail
megacheck $(go list ./...)
/go/src/github.com/.../cmd/get_spreadsheet_token.go:6:2: could not import golang.org/x/oauth2 (cannot find package "golang.org/x/oauth2" in any of:
/usr/local/go/src/golang.org/x/oauth2 (from $GOROOT)
/go/src/golang.org/x/oauth2 (from $GOPATH))
couldn't load packages due to errors: ...
Exited with code 1
というわけで、先に dep ensure
すれば解決です。
❌まちがい
- run: ./reviewdog -ci="circle-ci" -conf=.reviewdog.yml
- run: dep ensure
🔵 せいかい
- run: dep ensure
- run: ./reviewdog -ci="circle-ci" -conf=.reviewdog.yml
最終結果
ちゃんと megacheck
, errcheck
, の linter が動作して、Pull Request にも指摘が出るようになりました!
#!/bin/bash -eo pipefail
./reviewdog -ci="circle-ci" -conf=.reviewdog.yml
cmd/get_spreadsheet_token.go:12:6: [golint] exported type Dummy should have comment or be unexported
cmd/get_spreadsheet_token.go:13:2: [golint] struct field testJson should be testJSON
cmd/get_spreadsheet_token.go:47:35: [errcheck] json.NewEncoder(os.Stdout).Encode(tok)
cmd/get_spreadsheet_token.go:12:6: [megacheck] type Dummy is unused (U1000)