Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit e9f7843

Browse files
coreywoodfieldbjchambersSrodriguezO
authored
Improve proto rules (#292)
* Don't generate scala code for transitive sources Also pass in transitive-paths so protoc can find the transitive sources * Add test to check that code for proto deps is not generated The test checks that the compilation of `one.proto` generates code only for protos in one.proto, and not for protos in zero.proto. * fix format Co-authored-by: Ben Chambers <[email protected]> Co-authored-by: sergio <[email protected]>
1 parent ff423d8 commit e9f7843

File tree

8 files changed

+70
-11
lines changed

8 files changed

+70
-11
lines changed

rules/scala_proto.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ scala_proto_library = rule(
1111
{
1212
"deps": attr.label_list(
1313
doc = "The proto_library targets you wish to generate Scala from",
14+
providers = [ProtoInfo],
1415
),
1516
"_zipper": attr.label(cfg = "host", default = "@bazel_tools//tools/zip:zipper", executable = True),
1617
},

rules/scala_proto/private/ScalaProtoWorker.scala

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package annex.scala.proto
33
import higherkindness.rules_scala.common.args.implicits._
44
import higherkindness.rules_scala.common.worker.WorkerMain
55
import java.io.File
6-
import java.nio.file.{Files, Paths}
6+
import java.nio.file.Files
77
import java.util.Collections
88
import net.sourceforge.argparse4j.ArgumentParsers
99
import net.sourceforge.argparse4j.impl.Arguments
@@ -21,6 +21,13 @@ object ScalaProtoWorker extends WorkerMain[Unit] {
2121
.help("Output dir")
2222
.metavar("output_dir")
2323
.`type`(Arguments.fileType.verifyCanCreate)
24+
parser
25+
.addArgument("--proto_paths")
26+
.help("Paths to be passed to protoc as --proto_path arguments")
27+
.metavar("proto_paths")
28+
.nargs("*")
29+
.`type`(Arguments.fileType.verifyCanRead.verifyIsDirectory)
30+
.setDefault_(Collections.emptyList)
2431
parser
2532
.addArgument("sources")
2633
.help("Source files")
@@ -36,11 +43,14 @@ object ScalaProtoWorker extends WorkerMain[Unit] {
3643
protected[this] def work(ctx: Unit, args: Array[String]): Unit = {
3744
val namespace = argParser.parseArgs(args)
3845
val sources = namespace.getList[File]("sources").asScala.toList
46+
val protoPaths = namespace.getList[File]("proto_paths").asScala.toList
3947

4048
val scalaOut = namespace.get[File]("output_dir").toPath
4149
Files.createDirectories(scalaOut)
4250

43-
val params = s"--scala_out=$scalaOut" :: sources.map(_.getPath)
51+
val params = s"--scala_out=$scalaOut" ::
52+
sources.map(_.getPath) :::
53+
protoPaths.map(dir => s"--proto_path=${dir.getPath}")
4454

4555
ProtocBridge.runWithGenerators(
4656
protoc = a => com.github.os72.protocjar.Protoc.runProtoc(a.toArray),

rules/scala_proto/private/core.bzl

+5-8
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ load(
1010
scala_proto_library_private_attributes = {}
1111

1212
def scala_proto_library_implementation(ctx):
13-
proto_deps = [dep for dep in ctx.attr.deps if ProtoInfo in dep]
14-
if proto_deps != ctx.attr.deps:
15-
fail("disallowed non proto deps in %s" % ctx.attr.deps)
13+
protos = [dep[ProtoInfo] for dep in ctx.attr.deps]
1614

17-
protos = [dep[ProtoInfo] for dep in proto_deps]
18-
19-
transitive_sources = depset(transitive = [proto.transitive_sources for proto in protos])
15+
sources = depset(direct = [source for proto in protos for source in proto.direct_sources])
2016
transitive_proto_path = depset(transitive = [proto.transitive_proto_path for proto in protos])
2117

2218
compiler = ctx.toolchains["@rules_scala_annex//rules/scala_proto:compiler_toolchain_type"]
@@ -32,7 +28,8 @@ def scala_proto_library_implementation(ctx):
3228

3329
args = ctx.actions.args()
3430
args.add("--output_dir", gendir.path)
35-
args.add_all("--", transitive_sources)
31+
args.add_all("--proto_paths", transitive_proto_path)
32+
args.add_all("--", sources)
3633
args.set_param_file_format("multiline")
3734
args.use_param_file("@%s", use_always = True)
3835

@@ -43,7 +40,7 @@ def scala_proto_library_implementation(ctx):
4340

4441
ctx.actions.run(
4542
mnemonic = "ScalaProtoCompile",
46-
inputs = depset(direct = [], transitive = [transitive_sources]),
43+
inputs = depset(transitive = [proto.transitive_sources for proto in protos]),
4744
outputs = [gendir],
4845
executable = compiler.compiler.files_to_run.executable,
4946
tools = compiler_inputs,

tests/proto/BUILD

+27
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,30 @@ scala_library(
2727
"@annex_test//:com_thesamet_scalapb_scalapb_runtime_2_12",
2828
],
2929
)
30+
31+
proto_library(
32+
name = "one_proto",
33+
srcs = ["one.proto"],
34+
visibility = ["//visibility:public"],
35+
deps = [
36+
":zero_proto",
37+
],
38+
)
39+
40+
scala_proto_library(
41+
name = "one_scala_proto",
42+
deps = [
43+
":one_proto",
44+
],
45+
)
46+
47+
scala_library(
48+
name = "one_scala",
49+
srcs = [":one_scala_proto"],
50+
deps = [
51+
":zero_scala",
52+
"@annex_test//:com_google_protobuf_protobuf_java",
53+
"@annex_test//:com_thesamet_scalapb_lenses_2_12",
54+
"@annex_test//:com_thesamet_scalapb_scalapb_runtime_2_12",
55+
],
56+
)

tests/proto/expected_one

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-rw---- 2.0 fat 0 bx 0% stor 20100101.000000 META-INF/
2+
-rw---- 2.0 fat 48 b- 0% stor 20100101.000000 META-INF/MANIFEST.MF
3+
-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/
4+
-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/proto/
5+
-rw---- 1.0 fat 0 b- 0% stor 20100101.000000 anx/proto/one/
6+
-rw---- 2.0 fat 14827 b- 0% stor 20100101.000002 anx/proto/one/One$.class
7+
-rw---- 2.0 fat 3097 b- 0% stor 20100101.000002 anx/proto/one/One$OneLens.class
8+
-rw---- 2.0 fat 18959 b- 0% stor 20100101.000002 anx/proto/one/One.class
9+
-rw---- 2.0 fat 6376 b- 0% stor 20100101.000002 anx/proto/one/OneProto$.class
10+
-rw---- 2.0 fat 1937 b- 0% stor 20100101.000002 anx/proto/one/OneProto.class
File renamed without changes.

tests/proto/one.proto

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto2";
2+
3+
option java_package = "anx.proto";
4+
5+
import "proto/zero.proto";
6+
7+
message One {
8+
optional Zero zero = 1;
9+
}

tests/proto/test

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
[[ "$(bazel version | head -n1 | cut -d: -f2 | tr -d ' ')" =~ 0.15.*|0.16.* ]] || bazel build :zero_scala_proto
55

66
bazel build :zero_scala
7-
diff expected <(
7+
diff expected_zero <(
88
zipinfo -m -T --h-t "$(bazel info bazel-bin)/proto/zero_scala.jar"
99
)
10+
11+
bazel build :one_scala
12+
diff expected_one <(
13+
zipinfo -m -T --h-t "$(bazel info bazel-bin)/proto/one_scala.jar"
14+
)

0 commit comments

Comments
 (0)