Skip to content

Commit 7394440

Browse files
Copilotphlax
andcommitted
Fix envoy_proto_descriptor to compute protobuf include path dynamically
Use bash parameter expansion to extract the protobuf src directory path at build time from the location of any.proto. This makes it work in both WORKSPACE mode (external/com_google_protobuf/src) and bzlmod mode (external/protobuf~/src) without hardcoding paths. Co-authored-by: phlax <454682+phlax@users.noreply.github.com> Signed-off-by: Ryan Northey <ryan@synca.io>
1 parent 76dd08f commit 7394440

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

bazel/envoy_build_system.bzl

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ def envoy_cc_platform_specific_dep(name):
200200
def envoy_proto_descriptor(name, out, srcs = [], external_deps = []):
201201
input_files = ["$(location " + src + ")" for src in srcs]
202202
include_paths = [".", native.package_name()]
203+
204+
# Track whether we need to compute protobuf include path dynamically
205+
has_protobuf_deps = False
206+
protobuf_include_marker = None
203207

204208
if "api_httpbody_protos" in external_deps:
205209
srcs.append("@com_google_googleapis//google/api:httpbody.proto")
@@ -226,13 +230,34 @@ def envoy_proto_descriptor(name, out, srcs = [], external_deps = []):
226230
srcs.append("@com_google_protobuf//src/google/protobuf:type.proto")
227231
srcs.append("@com_google_protobuf//src/google/protobuf:wrappers.proto")
228232
srcs.append("@com_google_protobuf//src/google/protobuf/compiler:plugin.proto")
229-
include_paths.append("external/com_google_protobuf/src")
233+
has_protobuf_deps = True
234+
# Use any.proto as a marker to compute the include path dynamically
235+
protobuf_include_marker = "@com_google_protobuf//src/google/protobuf:any.proto"
230236

231237
options = ["--include_imports"]
232-
options.extend(["-I" + include_path for include_path in include_paths])
233-
options.append("--descriptor_set_out=$@")
234-
235-
cmd = "$(location @com_google_protobuf//:protoc) " + " ".join(options + input_files)
238+
239+
# Build the command that computes include paths dynamically at execution time
240+
if has_protobuf_deps and protobuf_include_marker:
241+
# Extract the directory containing the protobuf proto files at build time
242+
# The proto files are in external/com_google_protobuf~/src/google/protobuf/
243+
# We need to extract up to the /src part
244+
cmd_parts = []
245+
# Get the full path to any.proto, then extract everything before /google/protobuf/
246+
cmd_parts.append("ANY_PROTO_PATH=$(location " + protobuf_include_marker + ")")
247+
cmd_parts.append("PROTOBUF_INCLUDE_PATH=$${ANY_PROTO_PATH%/google/protobuf/*}")
248+
cmd_parts.append("INCLUDE_OPTS=\"--include_imports\"")
249+
250+
# Add all include paths
251+
for include_path in include_paths:
252+
cmd_parts.append("INCLUDE_OPTS=\"$$INCLUDE_OPTS -I" + include_path + "\"")
253+
cmd_parts.append("INCLUDE_OPTS=\"$$INCLUDE_OPTS -I$$PROTOBUF_INCLUDE_PATH\"")
254+
255+
cmd_parts.append("$(location @com_google_protobuf//:protoc) $$INCLUDE_OPTS --descriptor_set_out=$@ " + " ".join(input_files))
256+
cmd = " && ".join(cmd_parts)
257+
else:
258+
options.extend(["-I" + include_path for include_path in include_paths])
259+
options.append("--descriptor_set_out=$@")
260+
cmd = "$(location @com_google_protobuf//:protoc) " + " ".join(options + input_files)
236261
native.genrule(
237262
name = name,
238263
srcs = srcs,

0 commit comments

Comments
 (0)