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

CommonJS require paths when proto files are in subdirectories #244

Closed
rwlincoln opened this issue Aug 13, 2018 · 5 comments · Fixed by #249
Closed

CommonJS require paths when proto files are in subdirectories #244

rwlincoln opened this issue Aug 13, 2018 · 5 comments · Fixed by #249

Comments

@rwlincoln
Copy link

Lets say we have .proto files organised into directories:

apis/myproject/myapi/v1/myapi.proto
apis/myproject/otherapi/v1/otherapi.proto

Generating code involves including the apis directory:

$ protoc -I./apis  ...  ./apis/myproject/myapi/v1/myapi.proto

The generated code might be output to:

src/myproject/myapi/v1/
src/myproject/otherapi/v1/

The google/protobuf and grpc/grpc plug-ins generated require calls like this:

... = require("../../../myproject/myapi/v1/myapi_pb.js");

However, the grpc-web plug-in generates require calls like this:

... = require('./myproject/myapi/v1/myapi_pb.js');

The generated require paths should account for the directory structure relative to the import path.

@stanley-cheung
Copy link
Collaborator

I can see this, in the grpc-web plugin generated code myapi_grpc_pb.js:

... = require('./myproject/myapi/v1/myapi_pb.js');

That file myapi_grpc_pb.js is also placed in the src/ directory (assuming we are passing :./src to both --js_out and --grpc-web_out), so it can indeed find the require('./myproject/myapi/v1/myapi_pb.js').

Does this mean that you want to put the myapi_grpc_pb.js generated code in the src/myproject/myapi/v1 directory instead?

@rwlincoln
Copy link
Author

That is correct. I would like the generated grpc-web code to be in the same directory as all the other generated JS code (Protobuf, gRPC, TypeScript definitions) for that package. For example:

src/myproject/myapi/v1/myapi_pb.js
src/myproject/myapi/v1/myapi_pb.d.ts
src/myproject/myapi/v1/myapi_grpc_pb.js
src/myproject/myapi/v1/myapi_grpcweb_pb.js

As you point out, the grpc-web code can be placed in the top-level src directory and the require path is valid. However, when dealing with several APIs, with the goal of publishing the source and distributing packages, I believe the best practice is to have all the code organised into a hierarchy of directories. The google/protobuf, grpc/grpc and improbable-eng/ts-protoc-gen plug-ins create the myproject/myapi/v1/ directory structure when passed :./src.

@rwlincoln
Copy link
Author

As in #245 (comment), in order to maintain the current behaviour, the out= configuration option could be made optional and when omitted the plug-in could be made to work in a way that is consistent with the google/protobuf and grpc/grpc plug-ins.

@rogchap
Copy link
Contributor

rogchap commented Oct 30, 2018

@rwlincoln @stanley-cheung
I'm still seeing this behaviour, but only for the typescript output

The generated typescript tries to import from "./myproject/myapi/v1/myapi_pb" and not "../../../myproject/myapi/v1/myapi_pb"

I think the same fix in #249 needs to be applied to the typescript output.

@rogchap
Copy link
Contributor

rogchap commented Nov 30, 2018

This seems to have been fixed by #360

loyalpartner pushed a commit to loyalpartner/grpc-web that referenced this issue Sep 4, 2020
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 a pull request may close this issue.

3 participants