Skip to content

Commit 824d4be

Browse files
authored
Support processing Bazel generated proto files
1 parent b06983b commit 824d4be

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

rules.bzl

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,48 @@ def _protoc_plugin_rule_implementation(context):
7878
]
7979

8080
_virtual_imports = "/_virtual_imports/"
81+
82+
# Remove duplicate proto files, if the proto file location is the same
83+
# it won't break the `protoc` invocation, however we might have a
84+
# generated proto file (for example we have to generate proto files when
85+
# we have a Pydantic API) and if we have a `proto_library` which
86+
# depends on that proto, it will create a copy of the same proto file
87+
# inside `_virtual_imports` and that will break the build.
88+
seen = {}
89+
unique_proto_files = []
90+
for proto_file in proto_files:
91+
short_path = proto_file.short_path
92+
if _virtual_imports in proto_file.path:
93+
before, after = proto_file.path.split(_virtual_imports)
94+
import_path = before + _virtual_imports + after.split("/")[0] + "/"
95+
short_path = proto_file.path.replace(import_path, "")
96+
97+
if short_path not in seen:
98+
seen[short_path] = True
99+
unique_proto_files.append(proto_file)
100+
101+
proto_files = unique_proto_files
102+
81103
for proto_file in proto_files:
82104
if len(proto_file.owner.workspace_root) == 0:
83105
# Handle case where `proto_file` is a local file.
84-
args += [
85-
"-I" + ".",
106+
# We treate also treate proto files generated from Pydantic
107+
# API as "local file", but they will be placed in the
108+
# `bazel-out` directory so we need to get the import path
109+
# correctly for that case. For files which are part of the
110+
# workspace, the `import_path` will be empty and we will
111+
# set it to `./`.
112+
elements = proto_file.path.split("/")
113+
import_path = proto_file.path[:-len(proto_file.short_path)]
114+
115+
if import_path == "":
116+
import_path = "./"
117+
args.append(
118+
"-I" + import_path,
119+
)
120+
args.append(
86121
proto_file.short_path,
87-
]
122+
)
88123
elif proto_file.path.startswith("external"):
89124
# Handle case where `proto_file` is from an external
90125
# repository (i.e., from 'git_repository()' or

0 commit comments

Comments
 (0)