Skip to content

Commit 854a19b

Browse files
authored
Fix unused dependencies on type filters for buf generate (#3695)
1 parent d891bd2 commit 854a19b

File tree

8 files changed

+64
-6
lines changed

8 files changed

+64
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Fix use of deprecated flag `--include-types` for `buf generate`.
77
- Add `--against-registry` flag to `buf breaking` that runs breaking checks against the latest
88
commit on the default branch of the corresponding module in the registry.
9+
- Fix type filter with unused image dependencies for `buf generate`.
910

1011
## [v1.50.1] - 2025-03-10
1112

private/bufpkg/bufimage/bufimageutil/bufimageutil.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ func ImageFilteredByTypesWithOptions(image bufimage.Image, types []string, opts
244244
if !ok {
245245
continue
246246
}
247-
includedFiles = append(includedFiles, imageFile)
248247
imageFileDescriptor := imageFile.FileDescriptorProto()
249248

250249
importsRequired := closure.imports[imageFile.Path()]
@@ -365,6 +364,20 @@ func ImageFilteredByTypesWithOptions(image bufimage.Image, types []string, opts
365364
}
366365
imageFileDescriptor.SourceCodeInfo.Location = imageFileDescriptor.SourceCodeInfo.Location[:i]
367366
}
367+
imageFile, err = bufimage.NewImageFile(
368+
imageFileDescriptor,
369+
imageFile.FullName(),
370+
imageFile.CommitID(),
371+
imageFile.ExternalPath(),
372+
imageFile.LocalPath(),
373+
imageFile.IsImport(),
374+
imageFile.IsSyntaxUnspecified(),
375+
nil, // There are no unused dependencies.
376+
)
377+
if err != nil {
378+
return nil, err
379+
}
380+
includedFiles = append(includedFiles, imageFile)
368381
}
369382
return bufimage.NewImage(includedFiles)
370383
}

private/bufpkg/bufimage/bufimageutil/bufimageutil_test.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
2828
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduletesting"
2929
"github.com/bufbuild/buf/private/pkg/protoencoding"
30+
"github.com/bufbuild/buf/private/pkg/slicesext"
3031
"github.com/bufbuild/buf/private/pkg/slogtestext"
3132
"github.com/bufbuild/buf/private/pkg/storage"
3233
"github.com/bufbuild/buf/private/pkg/storage/storageos"
@@ -161,6 +162,11 @@ func TestSourceCodeInfo(t *testing.T) {
161162
runSourceCodeInfoTest(t, "foo.bar", "all.txtar")
162163
}
163164

165+
func TestUnusedDeps(t *testing.T) {
166+
t.Parallel()
167+
runDiffTest(t, "testdata/unuseddeps", []string{"a.A"}, "a.txtar")
168+
}
169+
164170
func TestTransitivePublic(t *testing.T) {
165171
t.Parallel()
166172
ctx := context.Background()
@@ -264,16 +270,23 @@ func runDiffTest(t *testing.T, testdataDir string, typenames []string, expectedF
264270
assert.NotNil(t, image)
265271
assert.True(t, imageIsDependencyOrdered(filteredImage), "image files not in dependency order")
266272

273+
// Convert the filtered image back to a proto image and then back to an image to ensure that the
274+
// image is still valid after filtering.
275+
protoImage, err := bufimage.ImageToProtoImage(filteredImage)
276+
require.NoError(t, err)
277+
filteredImage, err = bufimage.NewImageForProto(protoImage)
278+
require.NoError(t, err)
279+
267280
// We may have filtered out custom options from the set in the step above. However, the options messages
268281
// still contain extension fields that refer to the custom options, as a result of building the image.
269282
// So we serialize and then de-serialize, and use only the filtered results to parse extensions. That
270283
// way, the result will omit custom options that aren't present in the filtered set (as they will be
271284
// considered unrecognized fields).
272-
data, err := protoencoding.NewWireMarshaler().Marshal(bufimage.ImageToFileDescriptorSet(filteredImage))
273-
require.NoError(t, err)
274-
fileDescriptorSet := &descriptorpb.FileDescriptorSet{}
275-
err = protoencoding.NewWireUnmarshaler(filteredImage.Resolver()).Unmarshal(data, fileDescriptorSet)
276-
require.NoError(t, err)
285+
fileDescriptorSet := &descriptorpb.FileDescriptorSet{
286+
File: slicesext.Map(filteredImage.Files(), func(imageFile bufimage.ImageFile) *descriptorpb.FileDescriptorProto {
287+
return imageFile.FileDescriptorProto()
288+
}),
289+
}
277290

278291
files, err := protodesc.NewFiles(fileDescriptorSet)
279292
require.NoError(t, err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
package a;
3+
import "b.proto";
4+
import "c.proto";
5+
message A{
6+
b.B b = 1;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- a.proto --
2+
syntax = "proto3";
3+
package a;
4+
import "b.proto";
5+
message A {
6+
b.B b = 1;
7+
}
8+
-- b.proto --
9+
syntax = "proto3";
10+
package b;
11+
message B {
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- a.proto --
2+
syntax = "proto3";
3+
package a;
4+
message A {
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
syntax = "proto3";
2+
package b;
3+
import "c.proto";
4+
message B {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
syntax = "proto3";
2+
package c;
3+
message C {}

0 commit comments

Comments
 (0)