diff --git a/src/typescript_proto_build.bzl b/src/typescript_proto_build.bzl index 25dac73..49d11c7 100644 --- a/src/typescript_proto_build.bzl +++ b/src/typescript_proto_build.bzl @@ -40,10 +40,13 @@ def _proto_path(proto): path = path[len(root):] if path.startswith("/"): path = path[1:] - if path.startswith(ws): + if path.startswith(ws) and 0 < len(ws): path = path[len(ws):] if path.startswith("/"): path = path[1:] + if path.startswith("_virtual_imports/"): + path = path.split("/")[2:] + path = "/".join(path) return path def _get_protoc_inputs(target, ctx): @@ -85,7 +88,7 @@ def _build_protoc_command(target, ctx): if ctx.attr.generate == "grpc-node": protoc_command += " --plugin=protoc-gen-grpc=%s" % (ctx.executable._grpc_protoc_gen.path) - protoc_output_dir = ctx.var["BINDIR"] + protoc_output_dir = ctx.bin_dir.path + "/" + ctx.label.workspace_root protoc_command += " --ts_out=%s%s%s" % (",".join(ts_flags), ":" if bool(ts_flags) else "", protoc_output_dir) protoc_command += " --js_out=import_style=commonjs,binary:%s" % (protoc_output_dir) @@ -172,8 +175,16 @@ def _get_outputs(target, ctx): ts_file_suffixes.append("_pb_service.d.ts") for src in target[ProtoInfo].direct_sources: - file_name = src.basename[:-len(src.extension) - 1] relative_path = _get_path_relative_to_build(ctx, src) + + # workspace_root is empty for our local workspace, or external/other_workspace + # for @other_workspace// + if ctx.label.workspace_root == "": + file_name = src.basename[:-len(src.extension) - 1] + else: + relative_path = "" + file_name = _proto_path(src)[:-len(src.extension) - 1] + for f in js_file_suffixes: output = ctx.actions.declare_file(relative_path + file_name + f) js_outputs.append(output) diff --git a/test/BUILD.bazel b/test/BUILD.bazel index 1b7fca1..275f9d3 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -27,6 +27,8 @@ ts_library( "//test/proto:pizza_service_ts_base_proto", "//test/proto:pizza_service_ts_grpc_web_proto", "//test/proto/common:delivery_person_ts_proto", + "@npm//google-protobuf", + "@npm//@types/google-protobuf", "@npm//@types/jasmine", ], ) diff --git a/test/commonjs_test.spec.ts b/test/commonjs_test.spec.ts index 6daa8f5..bce6b1a 100644 --- a/test/commonjs_test.spec.ts +++ b/test/commonjs_test.spec.ts @@ -1,4 +1,5 @@ -import deliveryPersonPb = require('rules_typescript_proto/test/proto/common/delivery_person_pb'); +import {Timestamp} from 'google-protobuf/google/protobuf/timestamp_pb'; +import * as deliveryPersonPb from 'rules_typescript_proto/test/proto/common/delivery_person_pb'; import {PizzaService} from 'rules_typescript_proto/test/proto/pizza_service_pb_service'; import {InsideDir} from 'rules_typescript_proto/test/proto/dir/inside_pb'; @@ -8,6 +9,9 @@ describe('CommonJs', () => { const person = new deliveryPersonPb.DeliveryPerson(); person.setName('Doug'); + const lastDeliveryTime = new Timestamp(); + lastDeliveryTime.fromDate(new Date()); + person.setLastDeliveryTime(lastDeliveryTime); expect(person).toBeDefined(); }); diff --git a/test/proto/common/BUILD.bazel b/test/proto/common/BUILD.bazel index 10b0f6b..fd51f82 100644 --- a/test/proto/common/BUILD.bazel +++ b/test/proto/common/BUILD.bazel @@ -10,6 +10,7 @@ proto_library( "delivery_person.proto", ], deps = [ + "@com_google_protobuf//:timestamp_proto", ":pizza_proto", ], ) diff --git a/test/proto/common/delivery_person.proto b/test/proto/common/delivery_person.proto index 851bc4b..4c467c2 100644 --- a/test/proto/common/delivery_person.proto +++ b/test/proto/common/delivery_person.proto @@ -2,6 +2,7 @@ syntax = "proto3"; package test.bazel.proto; +import "google/protobuf/timestamp.proto"; import "test/proto/common/pizza.proto"; message DeliveryPerson { @@ -16,4 +17,6 @@ message DeliveryPerson { string id = 3; int64 id_v2 = 4; } + + google.protobuf.Timestamp last_delivery_time = 5; } diff --git a/test/tsconfig.json b/test/tsconfig.json index 25349dd..718da25 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -16,8 +16,9 @@ "types": [ "jasmine" ], + "baseUrl": "..", "paths": { - "rules_typescript_proto/*": ["../bazel-out/darwin-fastbuild/bin/*"] + "rules_typescript_proto/*": ["*", "../bazel-out/darwin-fastbuild/bin/*", "bazel-bin/*"] } } }