Skip to content

Honor exclusions when compiling protobuf files - #336

Merged
stephanos merged 7 commits into
mainfrom
fix-protogen-exclusions
Dec 2, 2026
Merged

stephanos merged 7 commits into
mainfrom
fix-protogen-exclusions

Conversation

@stephanos

@stephanos stephanos commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What changed?

Match directory exclusions on path boundaries, reject file exclusions, and pass only the discovered proto files to protoc.

Why?

Prefix matching could exclude similarly named directories, and globbing after discovery could reintroduce skipped files.

How did you test it?

Ran go test -count=1 ./cmd/protogen and verified that the CLI rejects a file exclusion.

Potential risks

Callers passing a file path to --exclude now receive a CLI error.

Match exclusions on path boundaries and pass the discovered files directly to protoc so excluded siblings cannot be picked up again.
Comment thread cmd/protogen/main.go
}

for i := 0; i < len(exclusions); i++ {
if strings.HasPrefix(path, exclusions[i]) {

@stephanos stephanos Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Bug: For example, excluding temporal/api_next also matches temporal/api_next_other.

Comment thread cmd/protogen/main.go

for i := 0; i < len(protoDirs); i++ {
dir := protoDirs[i]
files, err := filepath.Glob(filepath.Join(dir, "*.proto"))

@stephanos stephanos Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Bug: This glob adds excluded files back. Once discovery finds any included proto in this directory, the glob passes every sibling to protoc, including the files the walker skipped.

Reject file paths with a clear error and test that excluded directories leave similarly named siblings eligible for compilation.
Clean directory paths before matching exclusions and shorten the flag help.
Verify the descriptor output and rejected file paths through one table-driven compile test.
@stephanos
stephanos marked this pull request as ready for review November 24, 2026 02:32
@stephanos
stephanos requested review from a team as code owners November 24, 2026 02:32
@stephanos
stephanos requested a review from mavemuri November 24, 2026 02:32
Comment thread cmd/protogen/main.go Outdated

relPath, relErr := filepath.Rel(root, path)
for _, exclusion := range exclusions {
exact := path == exclusion || relErr == nil && relPath == exclusion

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
exact := path == exclusion || relErr == nil && relPath == exclusion
exact := path == exclusion || (relErr == nil && relPath == exclusion)

Comment thread cmd/protogen/main.go Outdated
}

prefix := exclusion + string(filepath.Separator)
if exact || strings.HasPrefix(path, prefix) || relErr == nil && strings.HasPrefix(relPath, prefix) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if exact || strings.HasPrefix(path, prefix) || relErr == nil && strings.HasPrefix(relPath, prefix) {
if exact || strings.HasPrefix(path, prefix) || (relErr == nil && strings.HasPrefix(relPath, prefix)) {

Comment thread cmd/protogen/main.go Outdated
if strings.HasPrefix(path, exclusions[i]) {
return fs.SkipDir

relPath, relErr := filepath.Rel(root, path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

wdyt about checking relErr here itself? exact/path==exclusion would not care about the error but this relErr shouldnt really happen anyway, itll clear up the relErr checks further down

Comment thread cmd/protogen/main_test.go
wantFiles []string
wantError string
}{
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe add some full path TCs too?

Check the Rel error up front so the exclusion matching does not need to
guard on it, and add full-path exclusion cases.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@stephanos
stephanos enabled auto-merge (squash) December 2, 2026 03:35
@stephanos
stephanos merged commit ea8b6f7 into main Dec 2, 2026
4 checks passed
@stephanos
stephanos deleted the fix-protogen-exclusions branch December 2, 2026 03:38
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.

2 participants