grpc / grpc/grpc-web

Generalize npm dependency support

Open
#584 1 comment 5 reactions 0 assignees View on GitHub
codegen
Dominant language
JavaScript
Stars
9.3k
Forks
802
Avg merge
1d 7h
Merged PRs (30d)
5

Description

All generated code currently generates js import paths with similar structure with that of the imported `.proto` files.
The one exception for protobuf well-known types - `google-protobuf` based ont he import prefix:
```c
string GetRootPath(const string& from_filename, const string& to_filename) {
if (HasPrefixString(to_filename, "google/protobuf")) {
// Well-known types (.proto files in the google/protobuf directory) are
// assumed to come from the 'google-protobuf' npm package. We may want to
// generalize this exception later by letting others put generated code in
// their own npm packages.
return "google-protobuf/";
}
```
https://github.com/grpc/grpc-web/blob/master/javascript/net/grpc/web/grpc_generator.cc#L461

This will generate something like

```js
import * as google_api_annotations_pb from './google/api/annotations_pb';
import * as google_protobuf_timestamp_pb from 'google-protobuf/google/protobuf/timestamp_pb';
```

Given that the annotations are not part of the well-known types, and instead part of the google api common protos (e.g. https://github.com/googleapis/api-common-protos). This can cause problems.

An import option would allow users to explicitly override the relative path and specify a npm package at `.proto` import would be the right approach to generalize this.

Of course, because grpc-web and protobuf-js are more or less independent, this needs to be taken care in protobuf js generator as well, from where it was likely copied in the first place:
https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/js/js_generator.cc#L128

```c++
string GetRootPath(const std::string& from_filename,
const std::string& to_filename) {
if (to_filename.find("google/protobuf") == 0) {
// Well-known types (.proto files in the google/protobuf directory) are
// assumed to come from the 'google-protobuf' npm package. We may want to
// generalize this exception later by letting others put generated code in
// their own npm packages.
return "google-protobuf/";
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.