grpc / grpc/grpc-node

Generation issue with import style commonjs_strict

Open
#1,445 2 comments 3 reactions 0 assignees View on GitHub
package: grpc-tools
Dominant language
TypeScript
Stars
4.8k
Forks
716
Avg merge
2d 3h
Merged PRs (30d)
10

Description

### 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](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:
```js
goog.exportSymbol('proto.helloworld.HelloRequest', null, global);
```
commonjs_strict:
```js
goog.exportSymbol('helloworld.HelloRequest', null, proto);
```
Then we need to use the extra namespace when creating a request:
```js
var messages = require('./helloworld_pb');
[...]
var request = new messages.helloworld.HelloRequest();
```
or do:
```js
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:
```js
var helloworld_pb = require('./helloworld_pb.js');
```
with
```js
var helloworld_pb = require('./helloworld_pb.js').helloworld;
```
But since this is the generated code, it is easier to use `import_style=commonjs` instead.

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.