Skip to content

Commit

Permalink
Fixes a bug when handling proto files inside directories without BUIL…
Browse files Browse the repository at this point in the history
…D files. (#143)
  • Loading branch information
Dig-Doug authored Feb 18, 2021
1 parent 87ad92a commit 96a4ae2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
34 changes: 30 additions & 4 deletions src/typescript_proto_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ This rule should not be used directly. Use one of these instead:
- typescript_grpc_node_library
- typescript_grpc_web_library
"""

load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSNamedModuleInfo")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")

TypescriptProtoLibraryAspect = provider("establishes transitive dependencies for typescript on protobuf files",
TypescriptProtoLibraryAspect = provider(
"establishes transitive dependencies for typescript on protobuf files",
fields = {
"es5_outputs": "The ES5 JS files produced directly from the src protos",
"es6_outputs": "The ES6 JS files produced directly from the src protos",
Expand All @@ -28,6 +30,7 @@ def _proto_path(proto):
"""
Normalizes the path to an actual proto file path.
"""

# The proto path is not really a file path.
# It's the path to the proto that was seen when the descriptor file was generated.
path = proto.path
Expand Down Expand Up @@ -121,6 +124,28 @@ def _create_post_process_command(target, ctx, js_outputs, js_outputs_es6):

return " && ".join(convert_commands)

def _get_path_relative_to_build(ctx, src):
"""
Gets the path of a file relative to where the build file is. This is required to handle cases where there are
directories without BUILD files inside of them. E.g.
dir/
BUILD.bazel
api1/
api1.proto
"""
num_dirs = ctx.build_file_path.count("/")
index_of_dir = 0
num_dirs_seen = 0
last_dir_index = 0
for i in range(0, len(src.short_path)):
if src.short_path[i] == "/":
num_dirs_seen += 1
last_dir_index = i
if num_dirs_seen == num_dirs:
index_of_dir = i
return src.short_path[index_of_dir + 1:last_dir_index + 1]

def _get_outputs(target, ctx):
"""
Calculates all of the files that will be generated by the aspect.
Expand Down Expand Up @@ -148,16 +173,17 @@ def _get_outputs(target, ctx):

for src in target[ProtoInfo].direct_sources:
file_name = src.basename[:-len(src.extension) - 1]
relative_path = _get_path_relative_to_build(ctx, src)
for f in js_file_suffixes:
output = ctx.actions.declare_file(file_name + f)
output = ctx.actions.declare_file(relative_path + file_name + f)
js_outputs.append(output)

for f in mjs_file_suffixes:
output_es6 = ctx.actions.declare_file(file_name + f)
output_es6 = ctx.actions.declare_file(relative_path + file_name + f)
js_outputs_es6.append(output_es6)

for f in ts_file_suffixes:
output = ctx.actions.declare_file(file_name + f)
output = ctx.actions.declare_file(relative_path + file_name + f)
dts_outputs.append(output)

return [js_outputs, js_outputs_es6, dts_outputs]
Expand Down
8 changes: 5 additions & 3 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jasmine_node_test(
"@npm//@improbable-eng/grpc-web",
"@npm//google-protobuf",
],
templated_args = ["--bazel_patch_module_resolver"],
deps = [
":commonjs_test_lib",
],
templated_args = ["--bazel_patch_module_resolver"],
)

ts_library(
Expand All @@ -23,6 +23,7 @@ ts_library(
],
tsconfig = ":tsconfig.json",
deps = [
"//test/proto:inside_dir_ts_proto",
"//test/proto:pizza_service_ts_base_proto",
"//test/proto:pizza_service_ts_grpc_web_proto",
"//test/proto/common:delivery_person_ts_proto",
Expand Down Expand Up @@ -100,13 +101,13 @@ ts_library(
jasmine_node_test(
name = "grpc_node_test",
data = [
"@npm//grpc",
"@npm//google-protobuf",
"@npm//grpc",
],
templated_args = ["--bazel_patch_module_resolver"],
deps = [
":pizza_service_proto_grpc_node_test",
],
templated_args = ["--bazel_patch_module_resolver"],
)

karma_web_test_suite(
Expand Down Expand Up @@ -150,6 +151,7 @@ ts_library(
srcs = ["test_bundling.ts"],
tsconfig = ":test_bundling_tsconfig.json",
deps = [
"//test/proto:inside_dir_ts_proto",
"//test/proto:naming_styles_ts_proto",
"//test/proto:pizza_service_ts_base_proto",
"//test/proto:pizza_service_ts_grpc_web_proto",
Expand Down
2 changes: 2 additions & 0 deletions test/commonjs_test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import deliveryPersonPb = require('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';

describe('CommonJs', () => {
it('Loads imports using require()', () => {
Expand All @@ -12,5 +13,6 @@ describe('CommonJs', () => {

it('Loads imports using TS from syntax', () => {
expect(PizzaService).toBeDefined();
expect(InsideDir).toBeDefined();
});
});
12 changes: 12 additions & 0 deletions test/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,15 @@ typescript_grpc_web_library(
name = "pizza_service_ts_grpc_web_proto",
proto = ":pizza_service_proto",
)

proto_library(
name = "inside_dir_proto",
srcs = [
"dir/inside.proto",
],
)

typescript_proto_library(
name = "inside_dir_ts_proto",
proto = ":inside_dir_proto",
)
7 changes: 7 additions & 0 deletions test/proto/dir/inside.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package test.bazel.proto;

message InsideDir {
string test = 1;
}
7 changes: 4 additions & 3 deletions test/test_bundling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export {DeliveryPerson} from 'rules_typescript_proto/test/proto/common/delivery_person_pb';
export {Pizza, PizzaSize} from 'rules_typescript_proto/test/proto/common/pizza_pb';
export {InsideDir} from 'rules_typescript_proto/test/proto/dir/inside_pb';
export {alllowercase, ALLUPPERCASE, lowerCamelCase, m24M_, M2_M, M2M, M2M_, m42_M, M9, m_22M, M_2M, snake_case_snake_case, Upper_snake_Case, UpperCamelCase,} from 'rules_typescript_proto/test/proto/naming_styles_pb';
export { DeliveryPerson } from 'rules_typescript_proto/test/proto/common/delivery_person_pb';
export { Pizza, PizzaSize } from 'rules_typescript_proto/test/proto/common/pizza_pb';
export { PizzaService, PizzaServiceClient } from 'rules_typescript_proto/test/proto/pizza_service_pb_service';
export {PizzaService, PizzaServiceClient} from 'rules_typescript_proto/test/proto/pizza_service_pb_service';

0 comments on commit 96a4ae2

Please sign in to comment.