Upgrade to Pro — share decks privately, control downloads, hide ads and more …

コンパイルのたびに出力が変わる!?protocプラグインをめぐるトラブルシュート...

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 コンパイルのたびに出力が変わる!?protocプラグインをめぐるトラブルシュートとHyrum'Lawの教訓

Go Conference mini 2026 in Sendai
https://sendaigo.jp/

Avatar for Yoshiki Fujikane

Yoshiki Fujikane

February 21, 2026
Tweet

More Decks by Yoshiki Fujikane

Other Decks in Programming

Transcript

  1. 自己紹介 Name • Fujikane Yoshiki (ふじを) Company • CyberAgent, Inc.

    • AmebaLIFE事業部 Platform Engineer Love • Go ʕ◔ϖ◔ʔ / ビール🍺 @ffjlabo
  2. • 「ユーザのRoleに基づいて対象リソースへのAPIの実行を制限」 RBAC機能 rpc GetDeployment Role: Viewer ❌ ✅ ✅

    resource: Deployment resources=application,deployment,piped; actions=get,list rpc ListDeployment rpc CreateDeployment
  3. バグ、襲来 codegenのアップグレード作業をしていた時のこと… • CI上で自動生成コードの生成漏れチェックが失敗するように ◦ codegenを実行し、差分をチェック • protoc-gen-authの生成結果に差分が発生 HEAD detached

    at pull/4970/merge Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: pkg/app/server/service/webservice/service.pb.auth.go
  4. バグ、襲来 直近実施したこと • Go versionのアップグレード(1.20.4 -> 1.22.4) • 依存モジュールのprotobufアップグレード (1.28.1

    -> 1.33.0) • コンテナのbase imageをAlpineからDebianへ変更 けれども… • コードは特に変更していない • 上記実施した際に、ローカル環境(M1 Mac上)で動作確認済み
  5. バグ、襲来 ローカル環境(M1 Mac上)で事象が再現するか確認 • ❌ `go build` (darwin/arm64) • ❌

    `docker build` (linux/arm64) • ✅ `docker build --platform linux/amd64` (linux/amd64)
  6. バグ、襲来 ローカル環境で事象が再現するか確認 • ❌ `go build` (darwin/arm64) • ❌ `docker

    build` (linux/arm64) • ✅ `docker build --platform linux/amd64` (linux/amd64) amd64向けのバイナリでのみ再現
  7. バグ、再び 直近実施したこと • Go versionのアップグレード(1.22.4 -> 1.23) • golang/mock ->

    uber-go/mockへの移行 • またしてもコードは特に変更していない
  8. protoc-gen-debug • 出力ファイルを使うことで、protocプラグイン単体で動かすことが できる # before protoc \ -I .

    \ --auth_out=. \ --auth_opt=paths=source_relative \ # after ./protoc-gen-auth < ./code_generator_request.pb.bin
  9. 事前準備 • devleのインストール • go build -gcflags="all=-N -l" でデバッグ情報を含めるようビルド •

    protoc -I . –debug_out=”${dir}:${dir}” でprotocプラグインへの インプットを作成
  10. methodデータ作成直前のrpc数 🤔 少なくとも50個以上 (dlv) b 69 Breakpoint 5 set at

    0x100d71d34 for main.main.func1() /tmp/pipecd/tool/codegen/protoc-gen-auth/main.go:69 (dlv) c … } (dlv) p svc.Methods Sending output to pager... []*google.golang.org/protobuf/compiler/protogen.Method len: 50, cap: 64, [ *{ … GoName: "RegisterPiped", … *{ … GoName: "UpdatePiped", …
  11. レンダリング時のmethodデータ 🤔 複数あるrpcのうち1つのみ 格納されている (dlv) b 86 Breakpoint 3 set

    at 0x10292e280 for main.main.func1() /tmp/pipecd/tool/codegen/protoc-gen-auth/main.go:86 (dlv) c … (dlv) p methods []*main.Method len: 1, cap: 50, [ *{ Name: "GetCommand", Resource: "", Action: "", Ignored: true,}, ]
  12. デバッグによる詳細な挙動分析 (dlv) b 128 Breakpoint 7 set at 0x100d71260 for

    main.generateMethods.func1() /tmp/pipecd/tool/codegen/protoc-gen-auth/main.go:128 (dlv) c > [Breakpoint 7] main.generateMethods.func1() /tmp/pipecd/tool/codegen/protoc-gen-auth/main.go:128 (hits total:1) (PC: 0x100d71260) … (dlv) p vs []string len: 1, cap: 1, [ "resource:PIPED action:CREATE", ] 🤔 文字列がSplitされていない!?
  13. protoreflect.Value.String() dynamicpb.Message.String()を実行 (dlv) s … 275: // Sprint formats using

    the default formats for its operands and returns the resulting string. 276: // Spaces are added between operands when neither is a string. => 277: func Sprint(a ...any) string { 278: p := newPrinter() 279: p.doPrint(a) 280: s := string(p.buf) 281: p.free() 282: return s (dlv) p a []interface {} len: 1, cap: 1, [ *google.golang.org/protobuf/types/dynamicpb.Message { … }, ]
  14. upstreamでのディスカッション encoding: provide canonical output format https://github.com/golang/protobuf/issues/1121 投稿主 テストなどに影響でるので せめて機能をオフにできるように

    してくれないか? メンテナ 今回は対応しません。 むしろ不安定な出力に依存する実 装をしない方がいいです。
  15. 参考文献 • https://github.com/golang/protobuf/issues/1121 • https://protobuf.dev/reference/go/faq/#hyrums-law • https://www.hyrumslaw.com/ • http://pipecd.dev/ •

    https://github.com/pipe-cd/pipecd/pull/4919 • https://github.com/pipe-cd/pipecd/pull/4972 • https://github.com/pipe-cd/pipecd/pull/5436 • https://github.com/lyft/protoc-gen-star/tree/master/protoc-g en-debug • https://github.com/go-delve/delve