-
Notifications
You must be signed in to change notification settings - Fork 289
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
Fix Go feature set handling in editions for checks #3590
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8fc576e
Fix Go feature set handling in editions for checks
emcfarlane 210ed02
Add check testcase for go features
emcfarlane a658f2a
Fix doc
emcfarlane 665110a
Fix testcase
emcfarlane aca44b7
Use the resolver from the image
emcfarlane a86fc33
Feedback
emcfarlane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
private/bufpkg/bufcheck/testdata/breaking/current/breaking_editions_go_features/a.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
edition = "2023"; | ||
|
||
import "google/protobuf/go_features.proto"; | ||
|
||
option features.(pb.go).api_level = API_OPAQUE; |
4 changes: 4 additions & 0 deletions
4
private/bufpkg/bufcheck/testdata/breaking/current/breaking_editions_go_features/buf.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: v1 | ||
lint: | ||
use: | ||
- PACKAGE_DEFINED |
5 changes: 5 additions & 0 deletions
5
private/bufpkg/bufcheck/testdata/breaking/previous/breaking_editions_go_features/a.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
edition = "2023"; | ||
|
||
import "google/protobuf/go_features.proto"; | ||
|
||
option features.(pb.go).api_level = API_HYBRID; |
4 changes: 4 additions & 0 deletions
4
private/bufpkg/bufcheck/testdata/breaking/previous/breaking_editions_go_features/buf.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: v1 | ||
lint: | ||
use: | ||
- PACKAGE_DEFINED |
5 changes: 5 additions & 0 deletions
5
private/bufpkg/bufcheck/testdata/lint/editions_go_features/a.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
edition = "2023"; | ||
|
||
import "google/protobuf/go_features.proto"; | ||
|
||
option features.(pb.go).api_level = API_OPAQUE; |
4 changes: 4 additions & 0 deletions
4
private/bufpkg/bufcheck/testdata/lint/editions_go_features/buf.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: v1 | ||
lint: | ||
use: | ||
- PACKAGE_DEFINED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,11 @@ import ( | |
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/reflect/protodesc" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
"google.golang.org/protobuf/reflect/protoregistry" | ||
"google.golang.org/protobuf/testing/protocmp" | ||
"google.golang.org/protobuf/types/descriptorpb" | ||
"google.golang.org/protobuf/types/dynamicpb" | ||
"google.golang.org/protobuf/types/gofeaturespb" | ||
"google.golang.org/protobuf/types/known/durationpb" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
@@ -126,3 +128,66 @@ func TestReparseExtensions(t *testing.T) { | |
}) | ||
assert.Equal(t, 2, found) | ||
} | ||
|
||
func TestReparseExtensionsGoFeatures(t *testing.T) { | ||
t.Parallel() | ||
|
||
goFeaturesMessageDesc := gofeaturespb.File_google_protobuf_go_features_proto.Messages().ByName("GoFeatures") | ||
dynamicGoFeatures := dynamicpb.NewMessage(goFeaturesMessageDesc) | ||
dynamicGoFeatures.Set( | ||
goFeaturesMessageDesc.Fields().ByName("api_level"), | ||
protoreflect.ValueOfEnum(gofeaturespb.GoFeatures_API_OPAQUE.Number()), | ||
) | ||
assert.True(t, dynamicGoFeatures.IsValid()) | ||
dynamicExt := dynamicpb.NewExtensionType(gofeaturespb.E_Go.TypeDescriptor().Descriptor()) | ||
|
||
featureSet := &descriptorpb.FeatureSet{} | ||
featureSetReflect := featureSet.ProtoReflect() | ||
featureSetReflect.Set( | ||
dynamicExt.TypeDescriptor(), | ||
protoreflect.ValueOfMessage(dynamicGoFeatures), | ||
) | ||
|
||
// Validates the error conditions that cause this panic. | ||
// See issue https://github.com/golang/protobuf/issues/1669 | ||
assert.Panics(t, func() { | ||
proto.GetExtension(featureSet, gofeaturespb.E_Go) | ||
}) | ||
descFileDesc, err := protoregistry.GlobalFiles.FindFileByPath("google/protobuf/descriptor.proto") | ||
assert.NoError(t, err) | ||
goFeaturesFileDesc, err := protoregistry.GlobalFiles.FindFileByPath("google/protobuf/go_features.proto") | ||
assert.NoError(t, err) | ||
fileDesc := &descriptorpb.FileDescriptorProto{ | ||
Name: proto.String("a.proto"), | ||
Dependency: []string{ | ||
"google/protobuf/go_features.proto", | ||
}, | ||
Edition: descriptorpb.Edition_EDITION_2023.Enum(), | ||
Syntax: proto.String("editions"), | ||
Options: &descriptorpb.FileOptions{ | ||
Features: featureSet, | ||
}, | ||
} | ||
fileSet := &descriptorpb.FileDescriptorSet{ | ||
File: []*descriptorpb.FileDescriptorProto{ | ||
protodesc.ToFileDescriptorProto(descFileDesc), | ||
protodesc.ToFileDescriptorProto(goFeaturesFileDesc), | ||
fileDesc, | ||
}, | ||
} | ||
assert.Panics(t, func() { | ||
// TODO: if this no longer panics, we can remove the code handling | ||
// this workaround in bufcheck.imageToProtoFileDescriptors. | ||
_, err := protodesc.NewFiles(fileSet) | ||
assert.NoError(t, err) | ||
}) | ||
Comment on lines
+178
to
+183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
// Run the resvoler to convert the extension. | ||
goFeaturesResolver, err := newGoFeaturesResolver() | ||
require.NoError(t, err) | ||
err = ReparseExtensions(goFeaturesResolver, featureSetReflect) | ||
require.NoError(t, err) | ||
goFeatures, ok := proto.GetExtension(featureSet, gofeaturespb.E_Go).(*gofeaturespb.GoFeatures) | ||
require.True(t, ok) | ||
assert.Equal(t, goFeatures.GetApiLevel(), gofeaturespb.GoFeatures_API_OPAQUE) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this matter? This is an unexported method, not an exported helper. So do the call sites really re-use the original? If not, seems like a waste to generate an unnecessary copy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will modify the image so anything using that would see it. I don't think it matters though this was just done to be defensive. For the lint/breaking checks the images aren't used elsewhere.