Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bufplugin-go to v0.3.0 #3334

Merged
merged 4 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ go 1.22.0
toolchain go1.23.1

require (
buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go v1.34.2-20240904181154-a0be11449112.2
buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go v1.34.2-20240920185553-cf97df2825f6.2
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.2-20240717164558-a6c49f84cc0f.2
buf.build/gen/go/bufbuild/registry/connectrpc/go v1.16.2-20240821192916-45ba72cdd479.1
buf.build/gen/go/bufbuild/registry/protocolbuffers/go v1.34.2-20240821192916-45ba72cdd479.2
buf.build/go/bufplugin v0.2.0
buf.build/go/bufplugin v0.3.0
buf.build/go/protoyaml v0.2.0
connectrpc.com/connect v1.16.2
connectrpc.com/otelconnect v0.7.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go v1.34.2-20240904181154-a0be11449112.2 h1:X9qBPcvWGOJs/CeRVLoxxLJwC/eKyWDS/G4nj+3KGMY=
buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go v1.34.2-20240904181154-a0be11449112.2/go.mod h1:B+9TKHRYqoAUW57pLjhkLOnBCu0DQYMV+f7imQ9nXwI=
buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go v1.34.2-20240920185553-cf97df2825f6.2 h1:dM/jKJP5298DVmmet50pEaREQY1ZleguYDrpUWwl6AA=
buf.build/gen/go/bufbuild/bufplugin/protocolbuffers/go v1.34.2-20240920185553-cf97df2825f6.2/go.mod h1:B+9TKHRYqoAUW57pLjhkLOnBCu0DQYMV+f7imQ9nXwI=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.2-20240717164558-a6c49f84cc0f.2 h1:SZRVx928rbYZ6hEKUIN+vtGDkl7uotABRWGY4OAg5gM=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.34.2-20240717164558-a6c49f84cc0f.2/go.mod h1:ylS4c28ACSI59oJrOdW4pHS4n0Hw4TgSPHn8rpHl4Yw=
buf.build/gen/go/bufbuild/registry/connectrpc/go v1.16.2-20240821192916-45ba72cdd479.1 h1:QaJ6UkpvlGo4dBXR41vLRfPiKungbg7brjmbBC/k6Ig=
Expand All @@ -8,8 +8,8 @@ buf.build/gen/go/bufbuild/registry/protocolbuffers/go v1.34.2-20240821192916-45b
buf.build/gen/go/bufbuild/registry/protocolbuffers/go v1.34.2-20240821192916-45ba72cdd479.2/go.mod h1:psseUmlKRo9v5LZJtR/aTpdTLuyp9o3X7rnLT87SZEo=
buf.build/gen/go/pluginrpc/pluginrpc/protocolbuffers/go v1.34.2-20240828222655-5345c0a56177.2 h1:oSi+Adw4xvIjXrW8eY8QGR3sBdfWeY5HN/RefnRt52M=
buf.build/gen/go/pluginrpc/pluginrpc/protocolbuffers/go v1.34.2-20240828222655-5345c0a56177.2/go.mod h1:GjH0gjlY/ns16X8d6eaXV2W+6IFwsO5Ly9WVnzyd1E0=
buf.build/go/bufplugin v0.2.0 h1:nnNvWzUgQXitRDmjWWIkuXj9klreAAE94sVCsL+0v5g=
buf.build/go/bufplugin v0.2.0/go.mod h1:ZZYGt6PDcjbBSywdz/G8NdXkIuWi2rzR8CduGzbCPdk=
buf.build/go/bufplugin v0.3.0 h1:0kxad9/q3nCEbHVYR02crapmRyVT0zTqC/gNkzxuWSs=
buf.build/go/bufplugin v0.3.0/go.mod h1:Znx3zF4ThDWGN1eGFMsbVT9LkMcVV4MKeAC9hiGdN6g=
buf.build/go/protoyaml v0.2.0 h1:2g3OHjtLDqXBREIOjpZGHmQ+U/4mkN1YiQjxNB68Ip8=
buf.build/go/protoyaml v0.2.0/go.mod h1:L/9QvTDkTWcDTzAL6HMfN+mYC6CmZRm2KnsUA054iL0=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
Expand Down
14 changes: 7 additions & 7 deletions private/bufpkg/bufcheck/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func annotationToFileAnnotation(
pathToExternalPath map[string]string,
annotation *annotation,
) bufanalysis.FileAnnotation {
location := annotation.Location()
if location == nil {
fileLocation := annotation.FileLocation()
if fileLocation == nil {
// We have to do this or we get a weird fileInfo != nil but it is nil thing.
return bufanalysis.NewFileAnnotation(
nil,
Expand All @@ -67,14 +67,14 @@ func annotationToFileAnnotation(
annotation.PluginName(),
)
}
path := location.File().FileDescriptor().Path()
path := fileLocation.FileDescriptor().ProtoreflectFileDescriptor().Path()
// While it never should, it is OK if pathToExternalPath returns "" for a given path.
// We handle this in fileInfo.
fileInfo := newFileInfo(path, pathToExternalPath[path])
startLine := location.StartLine() + 1
startColumn := location.StartColumn() + 1
endLine := location.EndLine() + 1
endColumn := location.EndColumn() + 1
startLine := fileLocation.StartLine() + 1
startColumn := fileLocation.StartColumn() + 1
endLine := fileLocation.EndLine() + 1
endColumn := fileLocation.EndColumn() + 1
return bufanalysis.NewFileAnnotation(
fileInfo,
startLine,
Expand Down
8 changes: 4 additions & 4 deletions private/bufpkg/bufcheck/bufcheckserver/bufcheckserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestServicePascalCase(t *testing.T) {
ExpectedAnnotations: []checktest.ExpectedAnnotation{
{
RuleID: "SERVICE_PASCAL_CASE",
Location: &checktest.ExpectedLocation{
FileLocation: &checktest.ExpectedFileLocation{
FileName: "a.proto",
StartLine: 7,
StartColumn: 8,
Expand All @@ -66,7 +66,7 @@ func TestServicePascalCase(t *testing.T) {
},
{
RuleID: "SERVICE_PASCAL_CASE",
Location: &checktest.ExpectedLocation{
FileLocation: &checktest.ExpectedFileLocation{
FileName: "a.proto",
StartLine: 8,
StartColumn: 8,
Expand All @@ -76,7 +76,7 @@ func TestServicePascalCase(t *testing.T) {
},
{
RuleID: "SERVICE_PASCAL_CASE",
Location: &checktest.ExpectedLocation{
FileLocation: &checktest.ExpectedFileLocation{
FileName: "a.proto",
StartLine: 9,
StartColumn: 8,
Expand All @@ -86,7 +86,7 @@ func TestServicePascalCase(t *testing.T) {
},
{
RuleID: "SERVICE_PASCAL_CASE",
Location: &checktest.ExpectedLocation{
FileLocation: &checktest.ExpectedFileLocation{
FileName: "a.proto",
StartLine: 10,
StartColumn: 8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"

"buf.build/go/bufplugin/check"
"buf.build/go/bufplugin/descriptor"
"github.com/bufbuild/buf/private/bufpkg/bufprotosource"
"github.com/bufbuild/buf/private/pkg/slicesext"
"google.golang.org/protobuf/reflect/protodesc"
Expand All @@ -32,11 +33,11 @@ func Before(
ctx context.Context,
request check.Request,
) (context.Context, check.Request, error) {
protosourceFiles, err := protosourceFilesForFiles(ctx, request.Files())
protosourceFiles, err := protosourceFilesForFileDescriptors(ctx, request.FileDescriptors())
if err != nil {
return nil, nil, err
}
againstProtosourceFiles, err := protosourceFilesForFiles(ctx, request.AgainstFiles())
againstProtosourceFiles, err := protosourceFilesForFileDescriptors(ctx, request.AgainstFileDescriptors())
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -94,17 +95,17 @@ func (h multiRuleHandler) Handle(ctx context.Context, responseWriter check.Respo
return nil
}

func protosourceFilesForFiles(ctx context.Context, files []check.File) ([]bufprotosource.File, error) {
if len(files) == 0 {
func protosourceFilesForFileDescriptors(ctx context.Context, fileDescriptors []descriptor.FileDescriptor) ([]bufprotosource.File, error) {
if len(fileDescriptors) == 0 {
return nil, nil
}
resolver, err := protodesc.NewFiles(
&descriptorpb.FileDescriptorSet{
File: slicesext.Map(files, check.File.FileDescriptorProto),
File: slicesext.Map(fileDescriptors, descriptor.FileDescriptor.FileDescriptorProto),
},
)
if err != nil {
return nil, err
}
return bufprotosource.NewFiles(ctx, slicesext.Map(files, newInputFile), resolver)
return bufprotosource.NewFiles(ctx, slicesext.Map(fileDescriptors, newInputFile), resolver)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
package bufcheckserverutil

import (
"buf.build/go/bufplugin/check"
"buf.build/go/bufplugin/descriptor"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/gofrs/uuid/v5"
)

type inputFile struct {
check.File
descriptor.FileDescriptor
}

func newInputFile(file check.File) *inputFile {
func newInputFile(fileDescriptor descriptor.FileDescriptor) *inputFile {
return &inputFile{
File: file,
FileDescriptor: fileDescriptor,
}
}

func (i *inputFile) Path() string {
return i.File.FileDescriptorProto().GetName()
return i.FileDescriptor.FileDescriptorProto().GetName()
}

func (i *inputFile) ExternalPath() string {
Expand Down
5 changes: 3 additions & 2 deletions private/bufpkg/bufcheck/check_client_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package bufcheck

import (
"buf.build/go/bufplugin/check"
"buf.build/go/bufplugin/option"
)

// checkClientSpec contains a check.Client and details on what to do about
Expand All @@ -25,10 +26,10 @@ import (
type checkClientSpec struct {
PluginName string
Client check.Client
Options check.Options
Options option.Options
}

func newCheckClientSpec(pluginName string, client check.Client, options check.Options) *checkClientSpec {
func newCheckClientSpec(pluginName string, client check.Client, options option.Options) *checkClientSpec {
return &checkClientSpec{
PluginName: pluginName,
Client: client,
Expand Down
48 changes: 23 additions & 25 deletions private/bufpkg/bufcheck/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"strings"

"buf.build/go/bufplugin/check"
"buf.build/go/bufplugin/descriptor"
"buf.build/go/bufplugin/option"
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
"github.com/bufbuild/buf/private/bufpkg/bufcheck/bufcheckserver"
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
Expand Down Expand Up @@ -111,7 +113,7 @@ func (c *client) Lint(
return err
}
logRulesConfig(c.logger, config.rulesConfig)
files, err := check.FilesForProtoFiles(imageToProtoFiles(image))
files, err := descriptor.FileDescriptorsForProtoFileDescriptors(imageToProtoFileDescriptors(image))
if err != nil {
// If a validated Image results in an error, this is a system error.
return syserror.Wrap(err)
Expand Down Expand Up @@ -176,20 +178,20 @@ func (c *client) Breaking(
return err
}
logRulesConfig(c.logger, config.rulesConfig)
files, err := check.FilesForProtoFiles(imageToProtoFiles(image))
fileDescriptors, err := descriptor.FileDescriptorsForProtoFileDescriptors(imageToProtoFileDescriptors(image))
if err != nil {
// If a validated Image results in an error, this is a system error.
return syserror.Wrap(err)
}
againstFiles, err := check.FilesForProtoFiles(imageToProtoFiles(againstImage))
againstFileDescriptors, err := descriptor.FileDescriptorsForProtoFileDescriptors(imageToProtoFileDescriptors(againstImage))
if err != nil {
// If a validated Image results in an error, this is a system error.
return syserror.Wrap(err)
}
request, err := check.NewRequest(
files,
fileDescriptors,
check.WithRuleIDs(config.RuleIDs...),
check.WithAgainstFiles(againstFiles),
check.WithAgainstFileDescriptors(againstFileDescriptors),
check.WithOptions(config.DefaultOptions),
)
if err != nil {
Expand Down Expand Up @@ -286,11 +288,7 @@ func (c *client) allRulesAndCategories(
// Just passing through to fulfill all contracts, ie checkClientSpec has non-nil Options.
// Options are not used here.
// config struct really just needs refactoring.
emptyOptions, err := check.NewOptions(nil)
if err != nil {
return nil, nil, err
}
multiClient, err := c.getMultiClient(fileVersion, pluginConfigs, disableBuiltin, emptyOptions)
multiClient, err := c.getMultiClient(fileVersion, pluginConfigs, disableBuiltin, option.EmptyOptions)
if err != nil {
return nil, nil, err
}
Expand All @@ -301,7 +299,7 @@ func (c *client) getMultiClient(
fileVersion bufconfig.FileVersion,
pluginConfigs []bufconfig.PluginConfig,
disableBuiltin bool,
defaultOptions check.Options,
defaultOptions option.Options,
) (*multiClient, error) {
var checkClientSpecs []*checkClientSpec
if !disableBuiltin {
Expand All @@ -316,7 +314,7 @@ func (c *client) getMultiClient(
)
}
for _, pluginConfig := range pluginConfigs {
options, err := check.NewOptions(pluginConfig.Options())
options, err := option.NewOptions(pluginConfig.Options())
if err != nil {
return nil, fmt.Errorf("could not parse options for plugin %q: %w", pluginConfig.Name(), err)
}
Expand Down Expand Up @@ -393,33 +391,33 @@ func ignoreAnnotation(
config *config,
annotation *annotation,
) (bool, error) {
if location := annotation.Location(); location != nil {
ignore, err := ignoreLocation(config, annotation.RuleID(), location)
if fileLocation := annotation.FileLocation(); fileLocation != nil {
ignore, err := ignoreFileLocation(config, annotation.RuleID(), fileLocation)
if err != nil {
return false, err
}
if ignore {
return true, nil
}
}
if againstLocation := annotation.AgainstLocation(); againstLocation != nil {
return ignoreLocation(config, annotation.RuleID(), againstLocation)
if againstFileLocation := annotation.AgainstFileLocation(); againstFileLocation != nil {
return ignoreFileLocation(config, annotation.RuleID(), againstFileLocation)
}
return false, nil
}

func ignoreLocation(
func ignoreFileLocation(
config *config,
ruleID string,
location check.Location,
fileLocation descriptor.FileLocation,
) (bool, error) {
file := location.File()
if config.ExcludeImports && file.IsImport() {
fileDescriptor := fileLocation.FileDescriptor()
if config.ExcludeImports && fileDescriptor.IsImport() {
return true, nil
}

fileDescriptor := file.FileDescriptor()
path := fileDescriptor.Path()
protoreflectFileDescriptor := fileDescriptor.ProtoreflectFileDescriptor()
path := protoreflectFileDescriptor.Path()
if normalpath.MapHasEqualOrContainingPath(config.IgnoreRootPaths, path, normalpath.Relative) {
return true, nil
}
Expand All @@ -430,7 +428,7 @@ func ignoreLocation(

// Not a great design, but will never be triggered by lint since this is never set.
if config.IgnoreUnstablePackages {
if packageVersion, ok := protoversion.NewPackageVersionForPackage(string(fileDescriptor.Package())); ok {
if packageVersion, ok := protoversion.NewPackageVersionForPackage(string(protoreflectFileDescriptor.Package())); ok {
if packageVersion.StabilityLevel() != protoversion.StabilityLevelStable {
return true, nil
}
Expand All @@ -440,15 +438,15 @@ func ignoreLocation(
// Not a great design, but will never be triggered by breaking since this is never set.
// Therefore, never called for an againstLocation (since lint never has againstLocations).
if config.AllowCommentIgnores && config.CommentIgnorePrefix != "" {
sourcePath := location.SourcePath()
sourcePath := fileLocation.SourcePath()
if len(sourcePath) == 0 {
return false, nil
}
associatedSourcePaths, err := protosourcepath.GetAssociatedSourcePaths(sourcePath)
if err != nil {
return false, err
}
sourceLocations := fileDescriptor.SourceLocations()
sourceLocations := protoreflectFileDescriptor.SourceLocations()
for _, associatedSourcePath := range associatedSourcePaths {
sourceLocation := sourceLocations.ByPath(associatedSourcePath)
if leadingComments := sourceLocation.LeadingComments; leadingComments != "" {
Expand Down
Loading