protocolbuffers / protocolbuffers/protobuf

Allow custom plugin codegenerators to use imports

Open
#8,174 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted protoc
Dominant language
C++
Stars
72k
Forks
16.3k
Avg merge
1d 17h
Merged PRs (30d)
140

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading protoc's unused-import warning handling and the code generator response interface. Compare the proposed ways for a custom plugin to report imports it uses, and define done as suppressing the warning only for imports confirmed as used by that plugin while retaining true unused-import warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.