grpc-node icon indicating copy to clipboard operation
grpc-node copied to clipboard

Generation issue with import style commonjs_strict

Open Wambou opened this issue 5 years ago • 2 comments

Problem description

When generating gRPC client services with import-style=commonjs_strict, the extra namespace is not use in _grpc_pb.js. Then, when calling the client function to send a message, the following exception is thrown: Request message serialization failure: Right-hand side of 'instanceof' is not an object.

The problem is very similar to issue #1326.

Reproduction steps

Generate files using import-style=commonjs_strict instead of import-style=commonjs.

I copied the gRPC node static codegen example to expose the issue clearly. https://github.com/Wambou/grpc_node_example.git

Environment

  • Windows 10 x64
  • Node version: 12.16.1
  • Node installation method: exe file from Node website
  • Package name and version:
    • @grpc/grpc-js: 1.0.3
    • @grpc/proto-loader: 0.5.4
    • google-protobuf: 3.12.1
    • grpc-tools: 1.9.0

Additional context

The noticeable difference in code generation is the following. commonjs:

goog.exportSymbol('proto.helloworld.HelloRequest', null, global);

commonjs_strict:

goog.exportSymbol('helloworld.HelloRequest', null, proto);

Then we need to use the extra namespace when creating a request:

var messages = require('./helloworld_pb');
[...]
var request = new messages.helloworld.HelloRequest();

or do:

var messages = require('./helloworld_pb').helloworld;
[...]
var request = new messages.HelloRequest();

From my example, the fix is to edit helloworld_grpc_pb.js and add replace:

var helloworld_pb = require('./helloworld_pb.js');

with

var helloworld_pb = require('./helloworld_pb.js').helloworld;

But since this is the generated code, it is easier to use import_style=commonjs instead.

Wambou avatar May 27 '20 08:05 Wambou

Any idea when this is going to be fixed?

smares avatar Jun 01 '22 10:06 smares

I managed to fix this with --ts_proto_opt=esModuleInterop=true. Basically, incompatible module types were leading to a mess of default and normal exports, breaking dependencies in a way.

I use different plugin (this one) and set of options, but maybe you will find this helpful in your case as well.

mutantcornholio avatar Aug 31 '22 17:08 mutantcornholio