@@ -6,6 +6,30 @@ case class PBGenerateRequest(jarOutput: String, scalaPBOutput: Path, scalaPBArgs
66
77object PBGenerateRequest {
88
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+
933 def from (args : java.util.List [String ]): PBGenerateRequest = {
1034 val jarOutput = args.get(0 )
1135 val protoFiles = args.get(4 ).split(':' )
@@ -23,17 +47,18 @@ object PBGenerateRequest {
2347 case s if s.charAt(0 ) == '-' => Some (s.tail) // drop padding character
2448 case other => sys.error(s " expected a padding character of - (dash), but found: $other" )
2549 }
26- val transitiveProtoPaths = (args.get(3 ) match {
50+
51+ val transitiveProtoPaths : List [String ] = (args.get(3 ) match {
2752 case " -" => Nil
2853 case s if s.charAt(0 ) == '-' => s.tail.split(':' ).toList // drop padding character
2954 case other => sys.error(s " expected a padding character of - (dash), but found: $other" )
30- }) ++ List (" ." )
55+ }).map(fixTransitiveProtoPath(includedProto)) ++ List (" ." )
3156
3257 val tmp = Paths .get(Option (System .getProperty(" java.io.tmpdir" )).getOrElse(" /tmp" ))
3358 val scalaPBOutput = Files .createTempDirectory(tmp, " bazelscalapb" )
3459 val flagPrefix = flagOpt.fold(" " )(_ + " :" )
3560
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 =>
3762 val kv = e.split('=' )
3863 (kv(0 ), kv(1 ))
3964 }
0 commit comments