stephenh / ts-proto

An idiomatic protobuf generator for TypeScript
Apache License 2.0
2.15k stars 346 forks source link

Error when calling `tsc` together with the `outputIndex=true` option #1126

Open skycrazyk opened 1 week ago

skycrazyk commented 1 week ago

Error when calling tsc together with the outputIndex=true option in version 2.2.4. Everything normal in version 1.181.2

Error

General view of errors

error TS4023: Exported variable 'X' has or is using name 'MessageFns' from external module "/path/to/dist/google/protobuf/empty" but cannot be named

Error example

dist/clients/clients.ts:1045:14 - error TS4023: Exported variable 'ClientsDefinition' has or is using name 'MessageFns' from external module "/path/to/dist/google/protobuf/empty" but cannot be named.

Protoc CLI command

protoc -I=path/to/proto \
    --plugin=path/to/node_modules/.bin/protoc-gen-ts_proto \
    --ts_proto_opt=env=browser,outputServices=generic-definitions,outputJsonMethods=false,esModuleInterop=true,useOptionals=messages,exportCommonSymbols=false,useDate=false,useExactTypes=false,outputIndex=true \
    --ts_proto_out=path/to/dist \ 
        path/to/proto/**/*

Typescript CLI command

tsc --declaration --declarationMap --sourceMap --rootDir ./dist

Dependencies

Global

protoc --version
libprotoc 28.2

NPM

{
    "@bufbuild/protobuf": "^2.2.0",
    "ts-proto": "2.2.4",
    "typescript": "5.6.3"
}

Protofile example

syntax = "proto3";

import "google/protobuf/empty.proto";

option go_package = "git.company.com/project/go/proto/clients;clients";

package content.clients;

message Client {
  message OAuth {
      string client_id = 4;     
      string auth_id = 5;   
      string token_url = 10;     
      string client_secret = 11;   
  }

  message APIKey {
    string key = 1;
    bool rotate = 2;
  }

  message TLS {
    string subject = 1;
  }

  string id = 1;
  string space_id = 2;
  string name = 3;
  string description = 6;
  optional bool disabled = 7;
  string role_id = 8;
  OAuth oauth = 10;
  TLS tls = 11;
  APIKey api_key = 12;
}

message CreateRequest {
  Client client = 1;
}

message CreateResponse {
  Client created = 1;
}

message GetRequest {
  string space_id = 1;
  string id = 2;
}

message GetResponse {
  Client client = 1;
}

message GetByRequest {
  string space_id = 1;
  string oauth_client_id = 2;
  string tls_subject= 3;
  string api_key = 4;
}

message GetByResponse {
  Client client = 1;
}

message UpdateRequest {
  Client client = 1;
}

message ListRequest {
  string space_id = 1;
}

message ListResponse {
  repeated Client clients = 1;
}

message DeleteRequest {
  string space_id = 1;
  string id = 2;
}

message EnableRequest {
  string space_id = 1;
  string id = 2;
  bool enable = 3;
}

service Clients  {
  rpc Create(CreateRequest) returns(CreateResponse) {}
  rpc Get(GetRequest) returns(GetResponse) {}
  rpc GetBy(GetByRequest) returns(GetByResponse) {}
  rpc Update(UpdateRequest) returns(google.protobuf.Empty) {}
  rpc List(ListRequest) returns(ListResponse) {}
  rpc Delete(DeleteRequest) returns(google.protobuf.Empty) {}
  rpc Enable(EnableRequest) returns(google.protobuf.Empty) {}
}
stephenh commented 3 days ago

Hi @skycrazyk ; I'm not sure what the issue is here; this github comment:

https://github.com/microsoft/TypeScript/issues/5711#issuecomment-157793294

Insinuates that adding an import of MessageFns to clients.ts will fix things.

Can you try adding that import and see if that fixes it?

It also seems like the error is related to the --declaration flag, so maybe we should try running that on for our integration/* test suite and see what happens? :thinking:

If you could try that, i.e. make sure our integration test suite is using the declaration flag, and see what happens in a PR build, that would be great. Thanks!