Skip to content

Commit dbf5e17

Browse files
authored
Fix exclude filter behavior with empty image file (#3819)
1 parent 26429e7 commit dbf5e17

File tree

9 files changed

+47
-4
lines changed

9 files changed

+47
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add `CSR` category to breaking rules.
66
- Add support for local bufplugins for `protoc-gen-buf-breaking` and `protoc-gen-buf-lint`.
77
- Add RISC-V (64-bit) binaries for Linux to releases.
8+
- Fix type filtering on `buf generate` for empty files, files with no declared types.
89

910
## [v1.53.0] - 2025-04-21
1011

private/bufpkg/bufimage/bufimageutil/bufimageutil.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,9 @@ func (t *transitiveClosure) addElement(
450450

451451
switch typedDescriptor := descriptor.(type) {
452452
case *descriptorpb.FileDescriptorProto:
453-
typeNames, ok := imageIndex.FileTypes[typedDescriptor.GetName()]
454-
if !ok {
455-
return fmt.Errorf("missing %q", typedDescriptor.GetName())
456-
}
453+
// If the file we are attempting to include is empty (has no types defined), it is still
454+
// valid for inclusion.
455+
typeNames := imageIndex.FileTypes[typedDescriptor.GetName()]
457456
// A file includes all elements. The types are resolved in the image index
458457
// to ensure all nested types are included.
459458
for _, typeName := range typeNames {

private/bufpkg/bufimage/bufimageutil/bufimageutil_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,18 @@ func TestDependencies(t *testing.T) {
504504
})
505505
}
506506

507+
func TestEmptyFiles(t *testing.T) {
508+
t.Parallel()
509+
t.Run("include_empty_file", func(t *testing.T) {
510+
t.Parallel()
511+
runDiffTest(t, "testdata/empty", "empty.include.txtar", WithIncludeTypes("include"))
512+
})
513+
t.Run("exclude_empty_file", func(t *testing.T) {
514+
t.Parallel()
515+
runDiffTest(t, "testdata/empty", "empty.exclude.txtar", WithExcludeTypes("include"))
516+
})
517+
}
518+
507519
func getImage(ctx context.Context, logger *slog.Logger, testdataDir string, options ...bufimage.BuildImageOption) (storage.ReadWriteBucket, bufimage.Image, error) {
508520
bucket, err := storageos.NewProvider().NewReadWriteBucket(testdataDir)
509521
if err != nil {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
syntax = "proto3";
2+
package include;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
syntax = "proto3";
2+
3+
import "a.proto";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax = "proto3";
2+
package pkg;
3+
4+
import "a.proto";
5+
6+
message Foo {
7+
string name = 1;
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
syntax = "proto3";
2+
package pkg;
3+
4+
import "google/protobuf/any.proto";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- b.proto --
2+
syntax = "proto3";
3+
-- c.proto --
4+
syntax = "proto3";
5+
package pkg;
6+
message Foo {
7+
string name = 1;
8+
}
9+
-- d.proto --
10+
syntax = "proto3";
11+
package pkg;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- a.proto --
2+
syntax = "proto3";
3+
package include;

0 commit comments

Comments
 (0)