grpc / grpc/grpc-node

grpc-reflection doesn't load properly when an imported type doesn't have a package

Open
#2,934 1 comment 0 reactions 0 assignees View on GitHub
package: @grpc/reflection
Dominant language
TypeScript
Stars
4.8k
Forks
716
Avg merge
2d 3h
Merged PRs (30d)
10

Description

### Problem description
When imported type doesn't have a package, and it is used as a field, when I try to enable reflection, first I get these warnings:

```
Could not find file associated with reference NoPackage
```

But the gRPC service starts succesfully. However, when trying to load the service definition in Postman I get this error:

![Image](https://github.com/user-attachments/assets/ae297e57-2b42-484d-9de7-e18a1a89cfaa)

### Reproduction steps
I modified your reflection example to simulate the problem we have in our code. Please check [here](https://github.com/grpc/grpc-node/compare/master...matrosovs:reflection-add-dot-reference?expand=1). See these 2 files:
- `examples/protos/helloworld.proto`
- `examples/protos/nopackage.proto`

This is what I get when I run it:
```
$ node ./reflection/server.js
Debugger listening on ws://127.0.0.1:53801/5014bd58-6641-4daa-a11b-f7b03f82abcb
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Could not find file associated with reference NoPackage <--- these are the warnings in question
Could not find file associated with reference NoPackage
(node:141961) DeprecationWarning: Calling start() is no longer necessary. It can be safely omitted.
(Use `node --trace-deprecation ...` to show where the warning was created)
```

And if I try to load the definition via reflection in postman, I will get the error on the screenshot above.

### Environment
- OS name, version and architecture: Debian GNU/Linux 12 (bookworm), AMD x64
- Node version: v20.18.0
- Node installation method: for this particular example I used npm, but in our project we use yarn
- Package name and version: @grpc/reflection@1.0.4

### Additional context
This seem related to this issue: https://github.com/grpc/grpc-node/issues/2671, but sort of in reverse. Instead of removing the dot, I need to add one.

The way I resolved it is by simply checking if there is a reference with a leading dot. See [packages/grpc-reflection/src/implementations/reflection-v1.ts](https://github.com/grpc/grpc-node/compare/master...matrosovs:reflection-add-dot-reference?expand=1#diff-03f3984f7265baebd63307ea6a9ad315ff53776691ee93b03dc2c331ab0dd783R132) in the same branch:
```typescript
// if we didn't find anything then try just a FQN lookup
if (!referencedFile) {
referencedFile = this.symbols[ref] ?? this.symbols[`.${ref}`];
}
```

I would have opened a PR with my change, but I am not sure how to test it. I tried to add a test to `packages/grpc-reflection/test/test-reflection-v1-implementation.ts` (and updated *.proto files to have the same setup), but regardless of whether this fix is present or not, I get the same content for `reflectionService`.

But if you rerun the example service with the fix, definition loads via reflection with no issues:
![Image](https://github.com/user-attachments/assets/904529f2-469c-41ff-8fcb-7077594a6c49)

And Postman can even generate some sample request:
![Image](https://github.com/user-attachments/assets/38871346-d56f-4acb-ae3c-42d48517bb7a)

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.