@@ -6,6 +6,30 @@ case class PBGenerateRequest(jarOutput: String, scalaPBOutput: Path, scalaPBArgs
6
6
7
7
object PBGenerateRequest {
8
8
9
+ // This little function fixes a problem, where external/com_google_protobuf is not found. The com_google_protobuf
10
+ // is special in a way that it also brings-in protoc and also google well-known proto files. This, possibly,
11
+ // confuses Bazel and external/com_google_protobuf is not made available for target builds. Actual causes are unknown
12
+ // and this fixTransitiveProtoPath fixes this problem in the following way:
13
+ // (1) We have a list of all required .proto files; this is a tuple list (root -> full path), for example:
14
+ // bazel-out/k8-fastbuild/bin -> bazel-out/k8-fastbuild/bin/external/com_google_protobuf/google/protobuf/source_context.proto
15
+ // (2) Convert the full path to relative from the root:
16
+ // bazel-out/k8-fastbuild/bin -> external/com_google_protobuf/google/protobuf/source_context.proto
17
+ // (3) From all the included protos we find the first one that is located within dir we are processing -- relative
18
+ // path starts with the dir we are processing
19
+ // (4) If found -- the include folder is "orphan" and is not anchored in either host or target. To fix we prepend
20
+ // root. If not found, return original. This works as long as "external/com_google_protobuf" is available in
21
+ // target root.
22
+ def fixTransitiveProtoPath (includedProto : List [(Path , Path )]): String => String = {
23
+ val includedRelProto = includedProto.map { case (root, full) => (root.toString, root.relativize(full).toString) }
24
+
25
+ { orig =>
26
+ includedRelProto.find { case (_, rel) => rel.startsWith(orig) } match {
27
+ case Some ((root, _)) => s " $root/ $orig"
28
+ case None => orig
29
+ }
30
+ }
31
+ }
32
+
9
33
def from (args : java.util.List [String ]): PBGenerateRequest = {
10
34
val jarOutput = args.get(0 )
11
35
val protoFiles = args.get(4 ).split(':' )
@@ -23,17 +47,18 @@ object PBGenerateRequest {
23
47
case s if s.charAt(0 ) == '-' => Some (s.tail) // drop padding character
24
48
case other => sys.error(s " expected a padding character of - (dash), but found: $other" )
25
49
}
26
- val transitiveProtoPaths = (args.get(3 ) match {
50
+
51
+ val transitiveProtoPaths : List [String ] = (args.get(3 ) match {
27
52
case " -" => Nil
28
53
case s if s.charAt(0 ) == '-' => s.tail.split(':' ).toList // drop padding character
29
54
case other => sys.error(s " expected a padding character of - (dash), but found: $other" )
30
- }) ++ List (" ." )
55
+ }).map(fixTransitiveProtoPath(includedProto)) ++ List (" ." )
31
56
32
57
val tmp = Paths .get(Option (System .getProperty(" java.io.tmpdir" )).getOrElse(" /tmp" ))
33
58
val scalaPBOutput = Files .createTempDirectory(tmp, " bazelscalapb" )
34
59
val flagPrefix = flagOpt.fold(" " )(_ + " :" )
35
60
36
- val namedGenerators = args.get(6 ).drop(1 ).split(',' ).filter(_.nonEmpty).map { e =>
61
+ val namedGenerators = args.get(6 ).drop(1 ).split(',' ).filter(_.nonEmpty).map { e =>
37
62
val kv = e.split('=' )
38
63
(kv(0 ), kv(1 ))
39
64
}
0 commit comments