Skip to content

feat: add example with go proto codegen#15

Open
alexeagle wants to merge 8 commits intomainfrom
verify_semantics
Open

feat: add example with go proto codegen#15
alexeagle wants to merge 8 commits intomainfrom
verify_semantics

Conversation

@alexeagle
Copy link
Collaborator

@alexeagle alexeagle commented Feb 25, 2026

This is a primary motivating use case for this ruleset.

Use case: developer changes foo.proto to remove a field. They either

  1. don't update the foo.pb.go file in the source tree
  2. they do update it but incorrectly (commonly an AI agent might do this)

Their editor doesn't show an issue in the Go code that uses the proto, but their Bazel tests will fail. Because we have flag //diff:validate_diffs set in .bazelrc, attempting to execute a test which depends on the protobuf file won't even build:

diff.bzl % bazel test examples:foo_usage_test
INFO: Analyzed target //examples:foo_usage_test (168 packages loaded, 12749 targets configured).
ERROR: /Users/alexeagle/Projects/diff.bzl/examples/BUILD:23:5: Action examples/bazel-out/darwin_arm64-fastbuild/bin/examples/go_codegen_diff.exit_code.valid failed: (Exit 1): bash failed: error executing Action command (from diff_rule rule target //examples:go_codegen_diff) /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
        ERROR: diff command exited with non-zero status.

        To accept the diff, run:
        patch -d $(bazel info workspace) -p0 < $(bazel info bazel-bin)/examples/go_codegen_diff.patch

Target //examples:foo_usage_test failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.952s, Critical Path: 0.04s
INFO: 2 processes: 518 action cache hit, 2 internal.
ERROR: Build did NOT complete successfully
//examples:foo_usage_test                                       FAILED TO BUILD

If you now simply run the command printed, it works:

alexeagle@aspect-build diff.bzl % patch -d $(bazel info workspace) -p0 < $(bazel info bazel-bin)/examples/go_codegen_diff.patch
patching file 'examples/foo.pb.go'
alexeagle@aspect-build diff.bzl % bazel test examples:foo_usage_test                                                           
INFO: Analyzed target //examples:foo_usage_test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
Target //examples:foo_usage_test up-to-date:
  bazel-bin/examples/foo_usage_test_/foo_usage_test
INFO: Elapsed time: 0.222s, Critical Path: 0.05s
INFO: 5 processes: 2 internal, 3 darwin-sandbox.
INFO: Build completed successfully, 5 total actions
//examples:foo_usage_test                                                PASSED in 0.0s

This is a primary motivating use case for this ruleset.

Behavior:
Use case: developer changes foo.proto to remove a field. They either
1. don't update the foo.pb.go file in the source tree
2. they do update it but incorrectly (commonly an AI agent might do this)

Their editor doesn't show an issue, but their Bazel tests will fail.
Because we have flag //diff:validate_diffs set in bazelrc, attempting to execute a test which depends on the protobuf
file won't even build:

```
diff.bzl % bazel test examples:foo_usage_test
INFO: Analyzed target //examples:foo_usage_test (168 packages loaded, 12749 targets configured).
ERROR: /Users/alexeagle/Projects/diff.bzl/examples/BUILD:23:5: Action examples/bazel-out/darwin_arm64-fastbuild/bin/examples/go_codegen_diff.exit_code.valid failed: (Exit 1): bash failed: error executing Action command (from diff_rule rule target //examples:go_codegen_diff) /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
        ERROR: diff command exited with non-zero status.

        To accept the diff, run:
        patch -d $(bazel info workspace) -p0 < $(bazel info bazel-bin)/examples/go_codegen_diff.patch

Target //examples:foo_usage_test failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.952s, Critical Path: 0.04s
INFO: 2 processes: 518 action cache hit, 2 internal.
ERROR: Build did NOT complete successfully
//examples:foo_usage_test                                       FAILED TO BUILD
```
Copy link

@thesayyn thesayyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work perfectly with AXL, we already have retry and process bes events logic over there, looking at test.sh, it looks like almost the same layering logic as rules_lint.

@alexeagle alexeagle force-pushed the verify_semantics branch 2 times, most recently from 76c71f7 to bec02c5 Compare February 25, 2026 21:14
To accept the diff, run:
patch -d \\$(bazel info workspace) -p0 < {patch}
""".format(patch = ctx.outputs.patch.path)))
patch -d \\$(bazel info workspace) -p0 < \\$(bazel info bazel-bin)/{patch}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(for my understanding) what case does this fix?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's when your current working directory is not the repository root.
perhaps it should be $(bazel info workspace) still, since there are multiple bin dirs under transitions

Comment on lines +17 to +18
permissions:
contents: write
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this can't push to main ⚠️ . Is it necessary in addition to pull-requests: write?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, because it needs to create a branch to hold the commits

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really hesitant about including this. There are going to be lots of test cases in this repo that produce patches but we don't necessarily want the build to fail and a patch to be auto applied.

I'm also concerned about the security implications of contents: write and I don't fully understand what capabilities it would give to someone submitting a PR from their fork. Do you know?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be best kept as a documented example of how you can use the diff output groups for automatic patching and not part of ci.

Comment on lines +17 to +18
"--output_groups=diff_bzl__patch"
"--build_event_json_file=$buildevents"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this capture and patch all diffs, even if we set validate = False because we didn't want the build to fail? Not every patch represents a build error, and some generated patches may be wrong (think a snapshot test that changed unexpectedly). Is there way we can separate diffs that we want to be automatically patched versus those we don't?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants