Skip to content

Commit fa6f9de

Browse files
authored
Add --exclude-source-retention-options flag to buf build (#2807)
1 parent d526527 commit fa6f9de

File tree

3 files changed

+72
-22
lines changed

3 files changed

+72
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
field of the `CodeGeneratorRequest` message. This provides the plugin with access to options
88
that are configured to only be retained in source and not at runtime (via
99
[field option](https://github.com/protocolbuffers/protobuf/blob/v24.0/src/google/protobuf/descriptor.proto#L693-L702)).
10-
Descriptors in the `proto_file` field will not include any options configured this way.
10+
Descriptors in the `proto_file` field will not include any options configured this way
11+
for the files named in `file_to_generate` field.
12+
- Add `--exclude-source-retention-options` flag to `buf build`, which
13+
causes options configured to only be retained in source to be stripped
14+
from the output descriptors.
1115

1216
## [v1.29.0] - 2024-01-24
1317

private/buf/cmd/buf/command/build/build.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@ import (
3131
)
3232

3333
const (
34-
asFileDescriptorSetFlagName = "as-file-descriptor-set"
35-
errorFormatFlagName = "error-format"
36-
excludeImportsFlagName = "exclude-imports"
37-
excludeSourceInfoFlagName = "exclude-source-info"
38-
pathsFlagName = "path"
39-
outputFlagName = "output"
40-
outputFlagShortName = "o"
41-
configFlagName = "config"
42-
excludePathsFlagName = "exclude-path"
43-
disableSymlinksFlagName = "disable-symlinks"
44-
typeFlagName = "type"
34+
asFileDescriptorSetFlagName = "as-file-descriptor-set"
35+
errorFormatFlagName = "error-format"
36+
excludeImportsFlagName = "exclude-imports"
37+
excludeSourceInfoFlagName = "exclude-source-info"
38+
excludeSourceRetentionOptionsFlagName = "exclude-source-retention-options"
39+
pathsFlagName = "path"
40+
outputFlagName = "output"
41+
outputFlagShortName = "o"
42+
configFlagName = "config"
43+
excludePathsFlagName = "exclude-path"
44+
disableSymlinksFlagName = "disable-symlinks"
45+
typeFlagName = "type"
4546
)
4647

4748
// NewCommand returns a new Command.
@@ -66,16 +67,17 @@ func NewCommand(
6667
}
6768

6869
type flags struct {
69-
AsFileDescriptorSet bool
70-
ErrorFormat string
71-
ExcludeImports bool
72-
ExcludeSourceInfo bool
73-
Paths []string
74-
Output string
75-
Config string
76-
ExcludePaths []string
77-
DisableSymlinks bool
78-
Types []string
70+
AsFileDescriptorSet bool
71+
ErrorFormat string
72+
ExcludeImports bool
73+
ExcludeSourceInfo bool
74+
ExcludeSourceRetentionOptions bool
75+
Paths []string
76+
Output string
77+
Config string
78+
ExcludePaths []string
79+
DisableSymlinks bool
80+
Types []string
7981
// special
8082
InputHashtag string
8183
}
@@ -92,6 +94,12 @@ func (f *flags) Bind(flagSet *pflag.FlagSet) {
9294
bufcli.BindPaths(flagSet, &f.Paths, pathsFlagName)
9395
bufcli.BindExcludePaths(flagSet, &f.ExcludePaths, excludePathsFlagName)
9496
bufcli.BindDisableSymlinks(flagSet, &f.DisableSymlinks, disableSymlinksFlagName)
97+
flagSet.BoolVar(
98+
&f.ExcludeSourceRetentionOptions,
99+
excludeSourceRetentionOptionsFlagName,
100+
false,
101+
"Exclude options whose retention is source",
102+
)
95103
flagSet.StringVar(
96104
&f.ErrorFormat,
97105
errorFormatFlagName,
@@ -165,6 +173,12 @@ func run(
165173
return err
166174
}
167175
}
176+
if flags.ExcludeSourceRetentionOptions {
177+
image, err = bufimageutil.StripSourceRetentionOptions(image)
178+
if err != nil {
179+
return err
180+
}
181+
}
168182
return bufcli.NewWireImageWriter(
169183
container.Logger(),
170184
).PutImage(

private/bufpkg/bufimage/bufimageutil/bufimageutil.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/bufbuild/buf/private/bufpkg/bufimage"
2424
"github.com/bufbuild/buf/private/pkg/protosource"
25+
"github.com/bufbuild/protocompile/options"
2526
"google.golang.org/protobuf/proto"
2627
"google.golang.org/protobuf/reflect/protoreflect"
2728
"google.golang.org/protobuf/types/descriptorpb"
@@ -372,6 +373,21 @@ func ImageFilteredByTypesWithOptions(image bufimage.Image, types []string, opts
372373
return bufimage.NewImage(includedFiles)
373374
}
374375

376+
// StripSourceRetentionOptions strips any options with a retention of "source" from
377+
// the descriptors in the given image. The image is not mutated but instead a new
378+
// image is returned. The returned image may share state with the original.
379+
func StripSourceRetentionOptions(image bufimage.Image) (bufimage.Image, error) {
380+
updatedFiles := make([]bufimage.ImageFile, len(image.Files()))
381+
for i, inputFile := range image.Files() {
382+
updatedFile, err := stripSourceRetentionOptionsFromFile(inputFile)
383+
if err != nil {
384+
return nil, fmt.Errorf("failed to strip source-retention options from file %q: %w", inputFile.Path(), err)
385+
}
386+
updatedFiles[i] = updatedFile
387+
}
388+
return bufimage.NewImage(updatedFiles)
389+
}
390+
375391
// trimMessageDescriptors removes (nested) messages and nested enums from a slice
376392
// of message descriptors if their type names are not found in the toKeep map.
377393
func trimMessageDescriptors(
@@ -917,3 +933,19 @@ func newImageFilterOptions() *imageFilterOptions {
917933
allowImportedTypes: false,
918934
}
919935
}
936+
937+
func stripSourceRetentionOptionsFromFile(imageFile bufimage.ImageFile) (bufimage.ImageFile, error) {
938+
updatedFileDescriptor, err := options.StripSourceRetentionOptionsFromFile(imageFile.FileDescriptorProto())
939+
if err != nil {
940+
return nil, err
941+
}
942+
return bufimage.NewImageFile(
943+
updatedFileDescriptor,
944+
imageFile.ModuleIdentity(),
945+
imageFile.Commit(),
946+
imageFile.ExternalPath(),
947+
imageFile.IsImport(),
948+
imageFile.IsSyntaxUnspecified(),
949+
imageFile.UnusedDependencyIndexes(),
950+
)
951+
}

0 commit comments

Comments
 (0)