tatethurston / TwirpScript

A protobuf RPC framework for JavaScript and TypeScript
MIT License
141 stars 14 forks source link

Name collision when using proto packages #207

Closed pwalker closed 1 year ago

pwalker commented 1 year ago

I've made an example repo demonstrating the problem: https://github.com/pwalker/twirpscript_package_name_repro

The gist is this, if I import another proto file and there is a message name collision the generated code produces a typescript error:

# first.proto
syntax = "proto3";

package first;

import "second.proto";

service Service {
  rpc SomeRpc(SomeRpcRequest) returns (SomeRpcResponse);
}

message SomeRpcRequest {
  Foo id = 1;
}

message SomeRpcResponse {
  second.Foo foo = 1;
}

message Foo {
  string id = 1;
}

And the generated typescript fragment:

//... cut out of `first.pb.ts`
import { Foo, FooJSON } from "./second.pb"; // <---- these names conflict with the interfaces below

//========================================//
//                 Types                  //
//========================================//

export interface SomeRpcRequest {
  id: Foo;
}

export interface SomeRpcResponse {
  foo: Foo;
}

export interface Foo {
  id: string;
}
//... end cut

These types and imports are also present when I just emit typescript declarations as well. I don't have a clear suggestion how to rewrite imports, maybe do something like import { Foo as second_pb_Foo } from "./second.pb"? I'm assuming that doing a glob import * as second from "./second.pb" might have implications for treeshaking or whatever, but I haven't read much about that so I can't say for sure. Anyways, I hope this helps!

tatethurston commented 1 year ago

Thanks @pwalker. Yes, switching over to glob (*) imports will resolve this and tree shaking will continue to work.

tatethurston commented 1 year ago

This is fixed in 0.0.66. LMK if you run into any issues!