4 ms·
I agree! Protoc allows comments anywhere, but it doesn't bother preserving them all in the descriptor. At one point I thought this was a bug, since comments _co
by jhumphries131 4y ago
I agree! Protoc allows comments anywhere, but it doesn't bother preserving them all in the descriptor. At one point I thought this was a bug, since comments _could_ be preserved in far greater contexts (though definitely not all). But then I realized that the descriptor.proto comments do actually state the places where comments are retained:
> If this SourceCodeInfo represents a complete declaration, these are any
> comments appearing before and after the declaration which appear to be
> attached to the declaration.
https://github.com/protocolbuffers/protobuf/blob/v21.5/src/google/protobuf/descriptor.proto#L844 https://github.com/protocolbuffers/protobuf/blob/v21.5/src/g...
After re-reading with this caveat in mind -- that comments are only preserved before and after complete declarations -- I realized that it does retain the comments that are expected.
Long story short: the descriptor is bad as an AST if you care about recovering the original source. The descriptor is lossy. I'd recommend using a real AST for use cases that want something non-lossy:
https://pkg.go.dev/github.com/jhump/protoreflect/desc/protoparse#Parser.ParseToAST https://pkg.go.dev/github.com/jhump/protoreflect/desc/protop...