@@ -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