@@ -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+
1314load ("@build_bazel_rules_nodejs//:providers.bzl" , "DeclarationInfo" , "JSEcmaScriptModuleInfo" , "JSNamedModuleInfo" )
1415load ("@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+
124149def _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 ]
0 commit comments