protocolbuffers/protobuf

Allow custom plugin codegenerators to use imports

Open

#8,174 创建于 2020年12月31日

在 GitHub 查看
 (3 评论) (0 反应) (0 负责人)C++ (16,128 fork)batch import
enhancementhelp wantedprotoc

仓库指标

Star
 (71,223 star)
PR 合并指标
 (平均合并 2天 11小时) (30 天内合并 185 个 PR)

描述

Describe the problem you are trying to solve. At Dropbox, we have a custom code generation plugin which makes use of custom options. For example

import "dropbox/api/errors.proto";

message Request {}
message Response {}
service MyService {
  rpc MyRPC(Request) returns (Response) {
    option (api_v2.method_error_type) = "errors.SpecialError";
  }
}

Our custom plugin generator currently (and ideally) depends on the import in order to bring in the special error type. However, protoc will warn

dropbox/api/my_service.proto:1:1: warning: Import dropbox/api/errors.proto is unused.

Describe the solution you'd like Ideally, there would be a way to suppress this warning in cases where the import is being used by the custom plugin, while still providing in the warning in true cases of unused imports. Perhaps the codegenerator response could include a list of imports that were used by the plugin - for unused import detection.

I could understand if this was an overgeneralization of this problem and I'm open to other ideas.

Describe alternatives you've considered We currently ignore unused import warnings for this reason - which is a possibility here, though suboptimal. Another idea might be to give a fully qualified path in the option string and teach the custom generator to pull in the import separately from protoc. This seems suboptimal as it's reimplementing import logic that protoc has already implemented, but it could work. Another idea might be to implement support for a warning-suppression comment into protoc eg:

import dropbox/api/errors.proto;  // ignore: unused

Another idea might be a file-level option for additional imports eg:

option (api.custom_generator.additional_imports) = "dropbox/api/my_service.proto";

Thanks!

贡献者指南