Skip to content

Apply WKT / _virtual_imports patch #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/typescript_proto_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
Expand Down
6 changes: 5 additions & 1 deletion test/commonjs_test.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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();
});

Expand Down
1 change: 1 addition & 0 deletions test/proto/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ proto_library(
"delivery_person.proto",
],
deps = [
"@com_google_protobuf//:timestamp_proto",
":pizza_proto",
],
)
Expand Down
3 changes: 3 additions & 0 deletions test/proto/common/delivery_person.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";

package test.bazel.proto;

import "google/protobuf/timestamp.proto";
import "test/proto/common/pizza.proto";

message DeliveryPerson {
Expand All @@ -16,4 +17,6 @@ message DeliveryPerson {
string id = 3;
int64 id_v2 = 4;
}

google.protobuf.Timestamp last_delivery_time = 5;
}
3 changes: 2 additions & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"]
}
}
}