Skip to content

Commit 96a4ae2

Browse files
authored
Fixes a bug when handling proto files inside directories without BUILD files. (#143)
1 parent 87ad92a commit 96a4ae2

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

src/typescript_proto_build.bzl

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ This rule should not be used directly. Use one of these instead:
1010
- typescript_grpc_node_library
1111
- typescript_grpc_web_library
1212
"""
13+
1314
load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSNamedModuleInfo")
1415
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
1516

16-
TypescriptProtoLibraryAspect = provider("establishes transitive dependencies for typescript on protobuf files",
17+
TypescriptProtoLibraryAspect = provider(
18+
"establishes transitive dependencies for typescript on protobuf files",
1719
fields = {
1820
"es5_outputs": "The ES5 JS files produced directly from the src protos",
1921
"es6_outputs": "The ES6 JS files produced directly from the src protos",
@@ -28,6 +30,7 @@ def _proto_path(proto):
2830
"""
2931
Normalizes the path to an actual proto file path.
3032
"""
33+
3134
# The proto path is not really a file path.
3235
# It's the path to the proto that was seen when the descriptor file was generated.
3336
path = proto.path
@@ -121,6 +124,28 @@ def _create_post_process_command(target, ctx, js_outputs, js_outputs_es6):
121124

122125
return " && ".join(convert_commands)
123126

127+
def _get_path_relative_to_build(ctx, src):
128+
"""
129+
Gets the path of a file relative to where the build file is. This is required to handle cases where there are
130+
directories without BUILD files inside of them. E.g.
131+
132+
dir/
133+
BUILD.bazel
134+
api1/
135+
api1.proto
136+
"""
137+
num_dirs = ctx.build_file_path.count("/")
138+
index_of_dir = 0
139+
num_dirs_seen = 0
140+
last_dir_index = 0
141+
for i in range(0, len(src.short_path)):
142+
if src.short_path[i] == "/":
143+
num_dirs_seen += 1
144+
last_dir_index = i
145+
if num_dirs_seen == num_dirs:
146+
index_of_dir = i
147+
return src.short_path[index_of_dir + 1:last_dir_index + 1]
148+
124149
def _get_outputs(target, ctx):
125150
"""
126151
Calculates all of the files that will be generated by the aspect.
@@ -148,16 +173,17 @@ def _get_outputs(target, ctx):
148173

149174
for src in target[ProtoInfo].direct_sources:
150175
file_name = src.basename[:-len(src.extension) - 1]
176+
relative_path = _get_path_relative_to_build(ctx, src)
151177
for f in js_file_suffixes:
152-
output = ctx.actions.declare_file(file_name + f)
178+
output = ctx.actions.declare_file(relative_path + file_name + f)
153179
js_outputs.append(output)
154180

155181
for f in mjs_file_suffixes:
156-
output_es6 = ctx.actions.declare_file(file_name + f)
182+
output_es6 = ctx.actions.declare_file(relative_path + file_name + f)
157183
js_outputs_es6.append(output_es6)
158184

159185
for f in ts_file_suffixes:
160-
output = ctx.actions.declare_file(file_name + f)
186+
output = ctx.actions.declare_file(relative_path + file_name + f)
161187
dts_outputs.append(output)
162188

163189
return [js_outputs, js_outputs_es6, dts_outputs]

test/BUILD.bazel

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jasmine_node_test(
1010
"@npm//@improbable-eng/grpc-web",
1111
"@npm//google-protobuf",
1212
],
13+
templated_args = ["--bazel_patch_module_resolver"],
1314
deps = [
1415
":commonjs_test_lib",
1516
],
16-
templated_args = ["--bazel_patch_module_resolver"],
1717
)
1818

1919
ts_library(
@@ -23,6 +23,7 @@ ts_library(
2323
],
2424
tsconfig = ":tsconfig.json",
2525
deps = [
26+
"//test/proto:inside_dir_ts_proto",
2627
"//test/proto:pizza_service_ts_base_proto",
2728
"//test/proto:pizza_service_ts_grpc_web_proto",
2829
"//test/proto/common:delivery_person_ts_proto",
@@ -100,13 +101,13 @@ ts_library(
100101
jasmine_node_test(
101102
name = "grpc_node_test",
102103
data = [
103-
"@npm//grpc",
104104
"@npm//google-protobuf",
105+
"@npm//grpc",
105106
],
107+
templated_args = ["--bazel_patch_module_resolver"],
106108
deps = [
107109
":pizza_service_proto_grpc_node_test",
108110
],
109-
templated_args = ["--bazel_patch_module_resolver"],
110111
)
111112

112113
karma_web_test_suite(
@@ -150,6 +151,7 @@ ts_library(
150151
srcs = ["test_bundling.ts"],
151152
tsconfig = ":test_bundling_tsconfig.json",
152153
deps = [
154+
"//test/proto:inside_dir_ts_proto",
153155
"//test/proto:naming_styles_ts_proto",
154156
"//test/proto:pizza_service_ts_base_proto",
155157
"//test/proto:pizza_service_ts_grpc_web_proto",

test/commonjs_test.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import deliveryPersonPb = require('rules_typescript_proto/test/proto/common/delivery_person_pb');
22
import {PizzaService} from 'rules_typescript_proto/test/proto/pizza_service_pb_service';
3+
import {InsideDir} from 'rules_typescript_proto/test/proto/dir/inside_pb';
34

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

1314
it('Loads imports using TS from syntax', () => {
1415
expect(PizzaService).toBeDefined();
16+
expect(InsideDir).toBeDefined();
1517
});
1618
});

test/proto/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,15 @@ typescript_grpc_web_library(
4040
name = "pizza_service_ts_grpc_web_proto",
4141
proto = ":pizza_service_proto",
4242
)
43+
44+
proto_library(
45+
name = "inside_dir_proto",
46+
srcs = [
47+
"dir/inside.proto",
48+
],
49+
)
50+
51+
typescript_proto_library(
52+
name = "inside_dir_ts_proto",
53+
proto = ":inside_dir_proto",
54+
)

test/proto/dir/inside.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package test.bazel.proto;
4+
5+
message InsideDir {
6+
string test = 1;
7+
}

test/test_bundling.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
export {DeliveryPerson} from 'rules_typescript_proto/test/proto/common/delivery_person_pb';
2+
export {Pizza, PizzaSize} from 'rules_typescript_proto/test/proto/common/pizza_pb';
3+
export {InsideDir} from 'rules_typescript_proto/test/proto/dir/inside_pb';
14
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';
2-
export { DeliveryPerson } from 'rules_typescript_proto/test/proto/common/delivery_person_pb';
3-
export { Pizza, PizzaSize } from 'rules_typescript_proto/test/proto/common/pizza_pb';
4-
export { PizzaService, PizzaServiceClient } from 'rules_typescript_proto/test/proto/pizza_service_pb_service';
5+
export {PizzaService, PizzaServiceClient} from 'rules_typescript_proto/test/proto/pizza_service_pb_service';

0 commit comments

Comments
 (0)