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

Add sourceinfo plugin #22

Closed
wants to merge 30 commits into from
Closed

Add sourceinfo plugin #22

wants to merge 30 commits into from

Conversation

Alfus
Copy link
Contributor

@Alfus Alfus commented Aug 7, 2024

The sourceinfo plugin provides a protoc-gen-sourceinfo cmd that generates a *.sourceinfo.binpb file, for every .proto file. Each generated file contains the associated serialized google.protobuf.SourceInfo value. These files can then be loaded directly or embedded in a binary.

The following helpers are provided for go:

  • sourceinfo.GlobalFiles(): A drop-in replacement for protoregistry.GlobalFiles
  • sourceinfo.GlobalTypes(): A drop-in replacement for protogregistry.GlobalTypes
  • sourceinfo.RegisterAll(root string): Registers all *.sourceinfo.binpb files found under the root dir.
  • sourceinfo.RegisterAllFS(fsys fs.FS, root string): Same as above, except uses the given file system.

@Alfus Alfus requested a review from jhump August 7, 2024 17:16
@Alfus Alfus requested a review from rodaine August 7, 2024 17:35
@Alfus Alfus requested a review from mfridman August 7, 2024 17:49
Copy link
Member

@jhump jhump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one remark about fixing up doc comments, otherwise LGTM

@Alfus Alfus requested a review from bufdev August 7, 2024 21:06
Copy link
Member

@mfridman mfridman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine, only question is whether we want this available as a Buf-managed plugin over on https://github.com/bufbuild/plugins?

We also have https://github.com/bufbuild/tools which is a collection of public tools/plugins we use. But this plugin does appear to be closer to what this (bufbuild/protoschema-plugins) repository aims to be.

tl;dr - is this plugin something the rest of the community would want, or something we're primarily interested in? I'm not sure.

@Alfus
Copy link
Contributor Author

Alfus commented Aug 7, 2024

The target use case is to enable runtime, reflection-based 'plugins'. For example, using a protobuf schema as the basis for pflags requires access to the documentation, but does not require code gen otherwise. This plugin is a building block for
any similar reflection-based used cases.

@mfridman
Copy link
Member

mfridman commented Aug 7, 2024

Probably worth a mention in the README.md, even if just a brief description of what this plugin does?

fileName := pluginsourceinfo.GetSourceInfoPath(testDesc)
filePath := filepath.Join(outputDir, fileName)
// Create any missing directories
if err = os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

err :=, always (this err should be scoped to this if statement)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a lambda that is reporting the error to the outter scope, it cannot use :=

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it shouldn't be set inside of an if statement - that's confusing to readers. I'm actually surprised that there isn't a linter that bans this in golangci-lint

var (
// GlobalFiles is a replacement for protoregistry.GlobalFiles that includes
// all registered source info.
GlobalFiles protodesc.Resolver = sourceinfo.GlobalFiles
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global variables are banned - Make this API not use global variables

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not resolved - you just changed this from a global variable to a static function that wraps a global variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the sourceinfo package exposes the functionality. Are you suggesting that package can't be used? Or do you have another solution in mind?

README.md Outdated
## SourceInfo

The sourceinfo plugin provides a protoc-gen-sourceinfo command that generates an `.sourceinfo.binpb` file, for every `.proto` file.
Each generated file contains the associated serialized `google.protobuf.SourceInfo` value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no type called google.protobuf.SourceInfo. Do you mean google.protobuf.SourceCodeInfo? If so, this calls into question the naming of a lot of things here (package, plugin, etc).

Additionally, a SourceCodeInfo (if this is the intent) does not make a ton sense without an associated FileDescriptorProto - why not make the plugin provide the entire FileDescriptorProto/FileDescriptorSet?

Copy link
Contributor Author

@Alfus Alfus Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice typo find! I was following @jhump's naming scheme, I am happy to switch it to source code info every where if you think that would provide more clarity.
The normal code gen already comes packed with the Descriptors for normal reflection, this plugin provides just the part that is stripped out. @jhump's sourceinfo library nicely glues everything together at runtime, they same could be done in other languages.


The following helpers are provided for go via `import "github.com/bufbuild/protoschema-plugins/sourceinfo"`:

- `sourceinfo.GlobalFiles()`: A drop-in replacement for protoregistry.GlobalFiles
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can this be a drop-in if you just are serializing the google.protobuf.SourceCodeInfo file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hooking into @jhump's library to provide a drop in replacement, backed by normal protoregistry values.

return fmt.Sprintf("%s%s", path, FileExtension)
}

// GenFileContents generates the source info file contents for the given file descriptor.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what this means - what is the "source info file contents"? Can you explain in the documentation what this is? All I am getting back is a []byte.

"google.golang.org/protobuf/types/descriptorpb"
)

// GlobalFiles is a replacement for protoregistry.GlobalFiles that includes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs much more extensive documentation. As a reader, I don't know what "all registered source info" is, and re: comment above, I don't know how this replaces protoregistry.GlobalFiles (and I should not have to read the code to find out if it actually does or how it does)

var (
// GlobalFiles is a replacement for protoregistry.GlobalFiles that includes
// all registered source info.
GlobalFiles protodesc.Resolver = sourceinfo.GlobalFiles
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was not resolved - you just changed this from a global variable to a static function that wraps a global variable.

@Alfus Alfus closed this Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants