ts-protoc-gen: Import proto from go src generates wrong imports

Problem

When writing Go protobufs, importing proto from go src generates wrong imports

Steps to reproduce

import "git.company.com/project/repo/proto/file.proto";

generates the following CommonJS import

var git_company_com_project_repo_proto_file_pb = require('./git.company.com/project/repo/proto/file_pb.js');

which can’t be imported. I noticed non of the examples have imports too

Expected

Generate correct imports

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 20 (6 by maintainers)

Most upvoted comments

  1. Proto file auth.proto:
syntax = "proto3";

package proto;

import "google/api/annotations.proto";

message TokenRequest {
  string address = 1;
}

message TokenResponse {
  string token = 1;
}

message SuccessResponse {
  bool success = 1;
  string error = 2;
}

service Auth {
  rpc GetToken(TokenRequest) returns (TokenResponse) {
    option (google.api.http) = {
      get: "/v1/token/{address}"
    };
  }

  rpc ValidateToken(TokenRequest) returns (SuccessResponse) {
    option (google.api.http) = {
      get: "/v1/validate/{address}"
    };
  }
}
  1. Generate typescript files using protoc and ts-protoc-gen plugin:
protoc \
	-I ${GOPATH}/src \
	-I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
	-I . \
	--plugin="protoc-gen-ts=./node_modules/.bin/protoc-gen-ts" \
	--js_out="import_style=commonjs,binary:./services/ts" \
	--ts_out="service=true:./services/ts" \
	./proto/auth.proto
  1. Notice that the generated js and ts require the dependency ../google/api/annotations_pb:
// package: proto
// file: proto/auth.proto

import * as jspb from "google-protobuf";
import * as google_api_annotations_pb from "../google/api/annotations_pb";

export class TokenRequest extends jspb.Message {
  getAddress(): string;
  setAddress(value: string): void;

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): TokenRequest.AsObject;
  static toObject(includeInstance: boolean, msg: TokenRequest): TokenRequest.AsObject;
  static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
  static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
  static serializeBinaryToWriter(message: TokenRequest, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): TokenRequest;
  static deserializeBinaryFromReader(message: TokenRequest, reader: jspb.BinaryReader): TokenRequest;
}

export namespace TokenRequest {
  export type AsObject = {
    address: string,
  }
}

export class TokenResponse extends jspb.Message {
  getToken(): string;
  setToken(value: string): void;

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): TokenResponse.AsObject;
  static toObject(includeInstance: boolean, msg: TokenResponse): TokenResponse.AsObject;
  static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
  static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
  static serializeBinaryToWriter(message: TokenResponse, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): TokenResponse;
  static deserializeBinaryFromReader(message: TokenResponse, reader: jspb.BinaryReader): TokenResponse;
}

export namespace TokenResponse {
  export type AsObject = {
    token: string,
  }
}

export class SuccessResponse extends jspb.Message {
  getSuccess(): boolean;
  setSuccess(value: boolean): void;

  getError(): string;
  setError(value: string): void;

  serializeBinary(): Uint8Array;
  toObject(includeInstance?: boolean): SuccessResponse.AsObject;
  static toObject(includeInstance: boolean, msg: SuccessResponse): SuccessResponse.AsObject;
  static extensions: {[key: number]: jspb.ExtensionFieldInfo<jspb.Message>};
  static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo<jspb.Message>};
  static serializeBinaryToWriter(message: SuccessResponse, writer: jspb.BinaryWriter): void;
  static deserializeBinary(bytes: Uint8Array): SuccessResponse;
  static deserializeBinaryFromReader(message: SuccessResponse, reader: jspb.BinaryReader): SuccessResponse;
}

export namespace SuccessResponse {
  export type AsObject = {
    success: boolean,
    error: string,
  }
}

The issue is that the ../google/api/annotations_pb package does not exist, and is not a dependency that the js or ts files should import. It is only imported in the .proto spec for grpc-gateway support.