Skip to content

Commit

Permalink
Bump ScalaPB, protoc-bridge, gRPC, and Guava deps, and add `ProtobufA…
Browse files Browse the repository at this point in the history
…dapter`s and `ScalaPBCodeGenerator` wrappers (#1648)

* Bump ScalaPB and gRPC, exclude listenablefuture

Adds these versions to `scripts/create_repository.py`.

There are breakages still that future commits will address, but the
following are fixed in this one.

Removes the unnecessary `true` parameter from `.getPlaintext(true)` in
`test/TestServer.scala` to fix this error after the gRPC bump:

```txt
ERROR: .../test/BUILD:676:14:
  scala @//test:lib_with_scala_proto_dep failed:
  (Exit 1): scalac failed: error executing command
  (from target //test:lib_with_scala_proto_dep)
  bazel-bin/src/java/io/bazel/rulesscala/scalac/scalac
    @bazel-out/darwin_arm64-fastbuild/bin/test/lib_with_scala_proto_dep.jar-0.params

test/TestServer.scala:70: error: no arguments allowed for nullary method usePlaintext: ()?0
    val channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build
                                                                            ^
```

We exclude `com.google.guava:listenablefuture` because trying to
download it breaks the build with an HTTP 404 (everything builds fine
without it anyway):

```txt
INFO: repository @io_bazel_rules_scala_listenablefuture' used the
  following cache hits instead of downloading the corresponding file.
  * Hash '...' for https://repo.maven.apache.org/maven2/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar

WARNING: Download from
  https://repo.maven.apache.org/maven2/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava-sources.jar
  failed: class java.io.FileNotFoundException GET returned 404 Not Found

WARNING: Download from
  https://maven-central.storage-download.googleapis.com/maven2/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava-sources.jar
  failed: class java.io.FileNotFoundException GET returned 404 Not Found

WARNING: Download from
  https://mirror.bazel.build/repo1.maven.org/maven2/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava-sources.jar
  failed: class java.io.FileNotFoundException GET returned 404 Not Found

WARNING: Download from
https://jcenter.bintray.com/com/google/guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava-sources.jar
  failed: class java.io.FileNotFoundException GET returned 404 Not Found

ERROR: An error occurred during the fetch of repository 'io_bazel_rules_scala_listenablefuture':
  Traceback (most recent call last):
    File ".../scala/scala_maven_import_external.bzl",
    line 130, column 32, in _jvm_import_external
      repository_ctx.download(srcurls, srcpath, srcsha, auth = _get_auth(repository_ctx, srcurls))

Error in download: java.io.IOException: Error downloading
  [ ...4 URLs from WARNINGs above... ]
to .../external/io_bazel_rules_scala_listenablefuture/listenablefuture-9999.0-empty-to-avoid-conflict-with-guava-src.jar:
GET returned 404 Not Found

ERROR: /Users/mbland/src/bazelbuild/rules_scala/WORKSPACE:75:25:
  fetching jvm_import_external rule
  //external:io_bazel_rules_scala_listenablefuture:
  [ ...Traceback, java.io.IOException, and HTTP 404 from above... ]

ERROR: .../external/io_bazel_rules_scala_guava/BUILD:9:13:
  @io_bazel_rules_scala_guava//:io_bazel_rules_scala_guava depends on
  @io_bazel_rules_scala_listenablefuture//:io_bazel_rules_scala_listenablefuture
  in repository @io_bazel_rules_scala_listenablefuture
  which failed to fetch. no such package
  '@io_bazel_rules_scala_listenablefuture//':
     [ ...java.io.IOException and HTTP 404 from above... ]

ERROR: Analysis of target
  '//test/proto/custom_generator:failing_scala_proto_deps_toolchain_def'
  failed; build aborted:
```

* Update scala_proto repositories for Scala 2.11

Since Scala 2.11 is stuck on ScalaPB 0.9.8, its dependencies don't
include the `dev.dirs:directories` artifact. Before this change,
building with `--repo_env=SCALA_VERSION=2.11.12 would die with:

```txt
$ bazel build --repo_env=SCALA_VERSION=2.11.12 \
  //src/... //test/...  //third_party/... //scala_proto/...

ERROR: Traceback (most recent call last):
  File "/Users/mbland/src/bazelbuild/rules_scala/WORKSPACE",
  line 75, column 25, in <toplevel>
    scala_proto_repositories()

  File "/Users/mbland/src/bazelbuild/rules_scala/scala_proto/scala_proto.bzl",
  line 17, column 37, in scala_proto_repositories
    scala_proto_default_repositories(**kwargs)

  File "/Users/mbland/src/bazelbuild/rules_scala/scala_proto/default/repositories.bzl",
  line 7, column 17, in scala_proto_default_repositories
    repositories(

  File "/Users/mbland/src/bazelbuild/rules_scala/third_party/repositories/repositories.bzl",
  line 107, column 17, in repositories
    fail("artifact %s not in third_party/repositories/scala_%s.bzl" % (

Error in fail: artifact dev_dirs_directories not in
  third_party/repositories/scala_2_11.bzl

ERROR: Error computing the main repository mapping:
  Encountered error while reading extension file 'go/deps.bzl':
  no such package '@io_bazel_rules_go//go':
  error loading package 'external': Could not load //external package
```

This is a preview of scala_proto toolchainization, given the addition of
`scala_version` and `register_toolchains` parameters to
`scala_proto_default_repositories`.

* Add scalapb:compilerplugin to root artifacts

Without this, the `com.thesamet.scalapb:compilerplugin` artifact is out
of sync with other ScalaPB artifacts, leading to this error:

```txt
$ bazel build //test/proto_cross_repo_boundary/...

ERROR: .../external/proto_cross_repo_boundary/BUILD.bazel:3:14:
  scala @proto_cross_repo_boundary//:sample_proto failed:
  (Exit 1): scalac failed: error executing command
  (from target @proto_cross_repo_boundary//:sample_proto)

bazel-out/.../bin/src/java/io/bazel/rulesscala/scalac/scalac
  @bazel-out/.../bin/external/proto_cross_repo_boundary/sample_proto_scalapb.jar-0.params

bazel-out/.../bin/external/proto_cross_repo_boundary/_scalac/sample_proto/sources/1_sample_proto_scala_scalapb.srcjar/sample/sample/Sample.scala:11:
  error: class Any needs to be a trait to be mixed in
    ) extends scalapb.GeneratedMessage with scalapb.Message[Sample] with scalapb.lenses.Updatable[Sample] {
                                                    ^
Target //test/proto_cross_repo_boundary:sample_scala_proto failed to build
```

* Fix scalapb-runtime 0.11.17 errors

There were API changes from scalapb-runtime 0.9.7 causing the following
errors:

```txt
ERROR: .../test/src/main/scala/scalarules/test/extra_protobuf_generator/BUILD:3:14:
  scala @//test/src/main/scala/scalarules/test/extra_protobuf_generator:extra_protobuf_generator
  failed: (Exit 1): scalac failed: error executing Scalac command
  (from target //test/src/main/scala/scalarules/test/extra_protobuf_generator:extra_protobuf_generator)
  bazel-out/darwin_arm64-opt-exec-ST-a828a81199fe/bin/src/java/io/bazel/rulesscala/scalac/scalac
    ... (remaining 1 argument skipped)

test/src/main/scala/scalarules/test/extra_protobuf_generator/ExtraProtobufGenerator.scala:20:
  error: value nameSymbol is not a member of com.google.protobuf.Descriptors.Descriptor
      .add(s"final case object Custom${message.nameSymbol}{}")
                                               ^
test/src/main/scala/scalarules/test/extra_protobuf_generator/ExtraProtobufGenerator.scala:34:
  error: value fileDescriptorObjectName is not a member of com.google.protobuf.Descriptors.FileDescriptor
    b.setName(file.scalaDirectory + "/Custom" + file.fileDescriptorObjectName + ".scala")
                                                     ^
test/src/main/scala/scalarules/test/extra_protobuf_generator/ExtraProtobufGenerator.scala:66:
  error: value FileDescriptorPimp is not a member of scalapb.compiler.DescriptorImplicits
          import implicits.FileDescriptorPimp
                 ^
```

Since Scala 2.11 can't advance past scalapb-runtime 0.9.8, we use
`select_for_scala_version` to select the appropriate `ProtobufAdapter`
to maintain API compatibility.

Also, `import implicits.FileDescriptorPimp` turned out to be unnecessary
after all, even under Scala 2.11.

* Add `null` as dummy `TestMessage` parameter

Fixes the `test_demonstrate_INCORRECT_scala_proto_library_stamp` test
case from `test/shell/test_strict_dependency.sh` after the ScalaPB
0.11.17 bump.

Somehow, bumping that library (presumably, instead of gRPC, et. al.)
changed the parameter list of `TestMessage` from `String` to
`String,UnknownFieldSet`. The test then failed because the following
command produced this unexpected error:

```txt
$ bazel build --verbose_failures \
  //test_expect_failure/missing_direct_deps/scala_proto_deps:uses_transitive_scala_proto \
  --extra_toolchains=//test/toolchains:ast_plus_one_deps_strict_deps_error

ERROR: .../test_expect_failure/missing_direct_deps/scala_proto_deps/BUILD:25:13:
  Building test_expect_failure/missing_direct_deps/scala_proto_deps/libtransitive.jar
  (1 source file) failed: (Exit 1): java failed: error executing command
  (from target //test_expect_failure/missing_direct_deps/scala_proto_deps:transitive)

  (cd ...
    [ ...snip... ]
  external/remotejdk11_macos_aarch64/bin/java -XX:-CompactStrings
    [ ...snip... ]
  external/remote_java_tools/java_tools/JavaBuilder_deploy.jar
  @bazel-out/.../bin/test_expect_failure/missing_direct_deps/scala_proto_deps/libtransitive.jar-0.params
  @bazel-out/.../bin/test_expect_failure/missing_direct_deps/scala_proto_deps/libtransitive.jar-1.params)

test_expect_failure/missing_direct_deps/scala_proto_deps/UseTestMessage.java:7:
  error: constructor TestMessage in class TestMessage
  cannot be applied to given types;

  private final TestMessage m = new TestMessage("");
                                ^
  required: String,UnknownFieldSet
  found: String
  reason: actual and formal argument lists differ in length
```

After this change, the same command produces the following failure that
the test is expecting to catch:

```txt
ERROR: .../test_expect_failure/missing_direct_deps/scala_proto_deps/BUILD:18:14:
  scala @//test_expect_failure/missing_direct_deps/scala_proto_deps:uses_transitive_scala_proto
  failed: (Exit 1): scalac failed: error executing command
  (from target //test_expect_failure/missing_direct_deps/scala_proto_deps:uses_transitive_scala_proto)

  (cd .../execroot/io_bazel_rules_scala && \
  exec env - \
  bazel-out/.../bin/src/java/io/bazel/rulesscala/scalac/scalac
  @bazel-out/.../bin/test_expect_failure/missing_direct_deps/scala_proto_deps/uses_transitive_scala_proto.jar-0.params)

test_expect_failure/missing_direct_deps/scala_proto_deps/UseScalaProtoIndirectly.scala:6:
  error: Target '@//test_expect_failure/missing_direct_deps/scala_proto_deps:some_proto'
  is used but isn't explicitly declared, please add it to the deps.

You can use the following buildozer command:
  buildozer 'add deps
    @//test_expect_failure/missing_direct_deps/scala_proto_deps:some_proto'
    //test_expect_failure/missing_direct_deps/scala_proto_deps:uses_transitive_scala_proto

  val foo: TestMessage = new UseTestMessage().getM
           ^
one error found
Build failure with errors.
Target //test_expect_failure/missing_direct_deps/scala_proto_deps:uses_transitive_scala_proto
  failed to build
```

* Remove unnecessary .getPlaintext() parameter

Already did this for `test/TestServer.scala` as part of the gRPC bump.
`test_version/version_specific_tests_dir/TestServer.scala` gets the same
treatment now to fix `test_scala_version 2.11.12` from
`test_version.sh`, which failed with:

```
ERROR: .../test_version/test_scala_version_1731027515/BUILD:161:14:
  scala @//:lib_with_scala_proto_dep failed: (Exit 1): scalac failed:
  error executing command (from target //:lib_with_scala_proto_dep)

bazel-out/.../bin/external/io_bazel_rules_scala/src/java/io/bazel/rulesscala/scalac/scalac
  @bazel-out/.../bin/lib_with_scala_proto_dep.jar-0.params

TestServer.scala:70: error: too many arguments for method usePlaintext: ()?0
    val channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext(true).build
                                                                           ^
```

* Add org_scala_lang_modules_scala_collection_compat

More specifically, add it to the list of core scala repository imports.
It had been generated before, but somehow hadn't been required until
now.

With this change, all tests now pass after the ScalaPB 0.11.17, gRPC
1.68.1, and Guava 33.3.1-jre version bumps.

Fixes the following error in `test_scala_version 2.12.20` from
`test_version.sh`:

```txt
ERROR: .../external/scala_proto_rules_scalapb_compilerplugin/BUILD:9:13:
  no such package '@org_scala_lang_modules_scala_collection_compat//':

  The repository '@org_scala_lang_modules_scala_collection_compat'
  could not be resolved:

  Repository '@org_scala_lang_modules_scala_collection_compat'
  is not defined and referenced by
  '@scala_proto_rules_scalapb_compilerplugin//:scala_proto_rules_scalapb_compilerplugin'

ERROR: Analysis of target '//proto:test_proto_nogrpc' failed; build aborted:
```

* Wrap ScalaPbCodeGenerator, expose Scala 2.11 error

Wraps `scalapb.ScalaPbCodeGenerator` to catch and return all errors.
Without this, any errors escaping from `scalapb.ScalaPbCodeGenerator`
will cause the `scala_proto` aspect workers to hang.

Also deletes some `@io_bazel_rules_scala` references, replacing them
with `Label` instances as appropriate.

The `test_scala_version 2.11.12` case from `test_version.sh` would
previously hang given the combination of ScalaPB 0.9.8 (the newest
available for Scala 2.11) and Protobuf v28.3. Running the following in a
`test_version/test_scala_version_*` repo generated by `test_version.sh`
reproduced the hang:

```txt
$ bazel build --repo_env=SCALA_VERSION=2.11.12 //proto:test_proto

[ ...wait 10 seconds, then CTRL-C... ]

INFO: Analyzed target //proto:test_proto (2 packages loaded, 22 targets configured).
INFO: Found 1 target...
[1,038 / 1,047] 4 actions running
    ProtoScalaPBRule proto/test_service_scala_scalapb.srcjar; 10s worker
    ProtoScalaPBRule proto/test2_scala_scalapb.srcjar; 10s worker
Target //proto:test_proto failed to build
```

With this change, the command now exposes the exact incompatibility
between these versions:

```txt
$ bazel build --repo_env=SCALA_VERSION=2.11.12 //proto:test_proto

ERROR: .../test_version/test_scala_version_1731169107/proto/BUILD:14:14:
  ProtoScalaPBRule proto/test3_scala_scalapb.srcjar failed: (Exit 1):
  scalapb_worker failed: error executing command
  (from target //proto:test3)

bazel-out/.../bin/external/io_bazel_rules_scala/src/scala/scripts/scalapb_worker
  ... (remaining 2 arguments skipped)

--scala_out: java.lang.NoSuchMethodError:
  'void com.google.protobuf.Descriptors$FileDescriptor.internalBuildGeneratedFileFrom(java.lang.String[],
    com.google.protobuf.Descriptors$FileDescriptor[],
    com.google.protobuf.Descriptors$FileDescriptor$InternalDescriptorAssigner)'
      at scalapb.options.compiler.Scalapb.<clinit>(Scalapb.java:10592)
      at scalapb.ScalaPbCodeGenerator$.run(ScalaPbCodeGenerator.scala:14)
      at scalapb.ScalaPbCodeGenerator$.run(ScalaPbCodeGenerator.scala:10)
      at scripts.ScalaPbCodeGenerator$.run(ScalaPbCodeGeneratorWrapper_2_11.scala:8)
      at protocbridge.frontend.PluginFrontend$$anonfun$runWithBytes$2.apply(PluginFrontend.scala:52)
      at protocbridge.frontend.PluginFrontend$$anonfun$runWithBytes$2.apply(PluginFrontend.scala:52)
      at scala.util.Try$.apply(Try.scala:192)
      at protocbridge.frontend.PluginFrontend$.runWithBytes(PluginFrontend.scala:51)
      at protocbridge.frontend.PluginFrontend$.runWithInputStream(PluginFrontend.scala:103)
      at protocbridge.frontend.PosixPluginFrontend$$anonfun$prepare$1.apply$mcV$sp(PosixPluginFrontend.scala:33)
      at protocbridge.frontend.PosixPluginFrontend$$anonfun$prepare$1.apply(PosixPluginFrontend.scala:31)
      at protocbridge.frontend.PosixPluginFrontend$$anonfun$prepare$1.apply(PosixPluginFrontend.scala:31)
      at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
      at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
      at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
      at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
      at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
      at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
      at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

java.lang.RuntimeException: Exit with code 1
      at scala.sys.package$.error(package.scala:27)
      at scripts.ScalaPBWorker$.work(ScalaPBWorker.scala:44)
      at io.bazel.rulesscala.worker.Worker.persistentWorkerMain(Worker.java:96)
      at io.bazel.rulesscala.worker.Worker.workerMain(Worker.java:49)
      at scripts.ScalaPBWorker$.main(ScalaPBWorker.scala:39)
      at scripts.ScalaPBWorker.main(ScalaPBWorker.scala)

Target //proto:test_proto failed to build
```

* Extract `scala_proto_artifact_ids`

This will support the upcoming schema whereby `scala_toolchains` will
import all artifact IDs via macros like this, and invoke `repositories`
itself. This will avoid instantiating the same artifact repository
multiple times for different frameworks that depend on it.

Under `WORKSPACE`, this isn't a problem, but it's an error under Bzlmod,
as a module extension can only instantiate a repository once. The commit
message to toolchainize Scalafmt will provide more details on this.
  • Loading branch information
mbland authored Nov 27, 2024
1 parent d43ae0d commit 23ae356
Show file tree
Hide file tree
Showing 25 changed files with 2,237 additions and 471 deletions.
1 change: 1 addition & 0 deletions scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def _artifact_ids(scala_version):
"io_bazel_rules_scala_scala_library",
"io_bazel_rules_scala_scala_parser_combinators",
"io_bazel_rules_scala_scala_xml",
"org_scala_lang_modules_scala_collection_compat",
]

if scala_version.startswith("2."):
Expand Down
23 changes: 10 additions & 13 deletions scala_proto/BUILD
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
load(
"//scala_proto:scala_proto_toolchain.bzl",
"scala_proto_deps_toolchain",
"scala_proto_toolchain",
)
load("//scala:providers.bzl", "declare_deps_provider")
load(
"//scala_proto/default:default_deps.bzl",
"DEFAULT_SCALAPB_COMPILE_DEPS",
"DEFAULT_SCALAPB_GRPC_DEPS",
"DEFAULT_SCALAPB_WORKER_DEPS",
)
load("//scala:providers.bzl", "declare_deps_provider")
load("//scala_proto/private:toolchain_deps.bzl", "export_scalapb_toolchain_deps")
load(
"//scala_proto:scala_proto_toolchain.bzl",
"scala_proto_deps_toolchain",
"scala_proto_toolchain",
)

toolchain_type(
name = "toolchain_type",
Expand Down Expand Up @@ -37,7 +38,7 @@ scala_proto_toolchain(
toolchain(
name = "default_toolchain",
toolchain = ":default_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala_proto:toolchain_type",
toolchain_type = "//scala_proto:toolchain_type",
visibility = ["//visibility:public"],
)

Expand All @@ -58,7 +59,7 @@ scala_proto_toolchain(
toolchain(
name = "enable_all_options_toolchain",
toolchain = ":enable_all_options_toolchain_impl",
toolchain_type = "@io_bazel_rules_scala//scala_proto:toolchain_type",
toolchain_type = "//scala_proto:toolchain_type",
visibility = ["//visibility:public"],
)

Expand All @@ -80,11 +81,7 @@ declare_deps_provider(
name = "scalapb_worker_deps_provider",
deps_id = "scalapb_worker_deps",
visibility = ["//visibility:public"],
deps = [
"@com_google_protobuf//:protobuf_java",
"@scala_proto_rules_scalapb_compilerplugin",
"@scala_proto_rules_scalapb_protoc_bridge",
],
deps = DEFAULT_SCALAPB_WORKER_DEPS,
)

export_scalapb_toolchain_deps(
Expand Down
22 changes: 17 additions & 5 deletions scala_proto/default/default_deps.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# These are the compile/runtime dependencies needed for scalapb compilation
# and grpc compile/runtime.
#
# In a complex environment you may want to update the toolchain to not refer to these anymore
# If you are using a resolver (like bazel-deps) that can export compile + runtime jar paths
# for you, then you should only need much shorter dependency lists. This needs to be the unrolled
# transitive path to be used without such a facility.
#
# In a complex environment you may want to update the toolchain to not refer to
# these anymore If you are using a resolver (like bazel-deps) that can export
# compile + runtime jar paths for you, then you should only need much shorter
# dependency lists. This needs to be the unrolled transitive path to be used
# without such a facility.

load("//scala:scala_cross_version_select.bzl", "select_for_scala_version")

DEFAULT_SCALAPB_COMPILE_DEPS = [
"//scala/private/toolchain_deps:scala_library_classpath",
"@com_google_protobuf//:protobuf_java",
Expand Down Expand Up @@ -41,3 +44,12 @@ DEFAULT_SCALAPB_GRPC_DEPS = [
"@scala_proto_rules_perfmark_api",
"@scala_proto_rules_scalapb_runtime_grpc",
]

DEFAULT_SCALAPB_WORKER_DEPS = [
"@com_google_protobuf//:protobuf_java",
"@scala_proto_rules_scalapb_compilerplugin",
"@scala_proto_rules_scalapb_protoc_bridge",
] + select_for_scala_version(
any_2_11 = [],
since_2_12 = ["@scala_proto_rules_scalapb_protoc_gen"],
)
101 changes: 66 additions & 35 deletions scala_proto/default/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,47 +1,78 @@
load("//scala:scala_cross_version.bzl", "default_maven_server_urls")
load("//third_party/repositories:repositories.bzl", "repositories")
load("@io_bazel_rules_scala_config//:config.bzl", "SCALA_VERSION")

# Needed by scalafmt
SCALAPB_COMPILE_ARTIFACT_IDS = [
"com_google_protobuf_protobuf_java",
"com_lihaoyi_fastparse",
"com_lihaoyi_sourcecode",
"scala_proto_rules_scalapb_lenses",
"scala_proto_rules_scalapb_runtime",
]

# Needed by twitter_scrooge
GUAVA_ARTIFACT_IDS = [
"com_google_code_findbugs_jsr305",
"com_google_errorprone_error_prone_annotations",
"com_google_j2objc_j2objc_annotations",
"io_bazel_rules_scala_guava",
"io_bazel_rules_scala_failureaccess",
"org_checkerframework_checker_qual",
]

def scala_proto_artifact_ids(scala_version):
return ([] if scala_version.startswith("2.11.") else [
"dev_dirs_directories",
"scala_proto_rules_scalapb_protoc_gen",
]) + [
"com_google_android_annotations",
"com_google_code_gson_gson",
"org_codehaus_mojo_animal_sniffer_annotations",
"scala_proto_rules_disruptor",
"scala_proto_rules_grpc_api",
"scala_proto_rules_grpc_context",
"scala_proto_rules_grpc_core",
"scala_proto_rules_grpc_netty",
"scala_proto_rules_grpc_protobuf",
"scala_proto_rules_grpc_protobuf_lite",
"scala_proto_rules_grpc_stub",
"scala_proto_rules_grpc_util",
"scala_proto_rules_instrumentation_api",
"scala_proto_rules_netty_buffer",
"scala_proto_rules_netty_codec",
"scala_proto_rules_netty_codec_http",
"scala_proto_rules_netty_codec_http2",
"scala_proto_rules_netty_codec_socks",
"scala_proto_rules_netty_common",
"scala_proto_rules_netty_handler",
"scala_proto_rules_netty_handler_proxy",
"scala_proto_rules_netty_resolver",
"scala_proto_rules_netty_transport",
"scala_proto_rules_netty_transport_native_unix_common",
"scala_proto_rules_opencensus_api",
"scala_proto_rules_opencensus_contrib_grpc_metrics",
"scala_proto_rules_opencensus_impl",
"scala_proto_rules_opencensus_impl_core",
"scala_proto_rules_perfmark_api",
"scala_proto_rules_proto_google_common_protos",
"scala_proto_rules_scalapb_compilerplugin",
"scala_proto_rules_scalapb_protoc_bridge",
"scala_proto_rules_scalapb_runtime_grpc",
] + SCALAPB_COMPILE_ARTIFACT_IDS + GUAVA_ARTIFACT_IDS

def scala_proto_default_repositories(
maven_servers = default_maven_server_urls(),
scala_version = SCALA_VERSION,
overriden_artifacts = {}):
repositories(
for_artifact_ids = [
"com_google_protobuf_protobuf_java",
"com_lihaoyi_fastparse",
"com_lihaoyi_sourcecode",
"io_bazel_rules_scala_guava",
"scala_proto_rules_disruptor",
"scala_proto_rules_instrumentation_api",
"scala_proto_rules_grpc_api",
"scala_proto_rules_grpc_context",
"scala_proto_rules_grpc_core",
"scala_proto_rules_grpc_netty",
"scala_proto_rules_grpc_protobuf",
"scala_proto_rules_grpc_stub",
"scala_proto_rules_netty_buffer",
"scala_proto_rules_netty_codec",
"scala_proto_rules_netty_codec_http",
"scala_proto_rules_netty_codec_http2",
"scala_proto_rules_netty_codec_socks",
"scala_proto_rules_netty_common",
"scala_proto_rules_netty_handler",
"scala_proto_rules_netty_handler_proxy",
"scala_proto_rules_netty_resolver",
"scala_proto_rules_netty_transport",
"scala_proto_rules_opencensus_api",
"scala_proto_rules_opencensus_contrib_grpc_metrics",
"scala_proto_rules_opencensus_impl",
"scala_proto_rules_opencensus_impl_core",
"scala_proto_rules_perfmark_api",
"scala_proto_rules_scalapb_compilerplugin",
"scala_proto_rules_scalapb_lenses",
"scala_proto_rules_scalapb_protoc_bridge",
"scala_proto_rules_scalapb_runtime",
"scala_proto_rules_scalapb_runtime_grpc",
],
scala_version = scala_version,
for_artifact_ids = scala_proto_artifact_ids(scala_version),
maven_servers = maven_servers,
fetch_sources = True,
overriden_artifacts = overriden_artifacts,
)

native.register_toolchains("@io_bazel_rules_scala//scala_proto:default_deps_toolchain")
native.register_toolchains(
str(Label("//scala_proto:default_deps_toolchain")),
)
29 changes: 21 additions & 8 deletions scala_proto/scala_proto_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_scala//scala:providers.bzl", "DepsInfo")
load("//scala:providers.bzl", "DepsInfo")

def _generators(ctx):
return dict(
Expand All @@ -7,9 +7,12 @@ def _generators(ctx):
)

def _generators_jars(ctx):
generator_deps = ctx.attr.extra_generator_dependencies + [
ctx.attr._main_generator_dep,
]
return depset(transitive = [
dep[JavaInfo].transitive_runtime_jars
for dep in ctx.attr.extra_generator_dependencies
for dep in generator_deps
])

def _generators_opts(ctx):
Expand Down Expand Up @@ -74,18 +77,20 @@ scala_proto_toolchain = rule(
"code_generator": attr.label(
executable = True,
cfg = "exec",
default = Label("@io_bazel_rules_scala//src/scala/scripts:scalapb_worker"),
default = Label("//src/scala/scripts:scalapb_worker"),
allow_files = True,
),
"main_generator": attr.string(default = "scalapb.ScalaPbCodeGenerator"),
"main_generator": attr.string(
default = "scripts.ScalaPbCodeGenerator",
),
"named_generators": attr.string_dict(),
"extra_generator_dependencies": attr.label_list(
providers = [JavaInfo],
),
"scalac": attr.label(
executable = True,
cfg = "exec",
default = Label("@io_bazel_rules_scala//src/java/io/bazel/rulesscala/scalac"),
default = Label("//src/java/io/bazel/rulesscala/scalac"),
allow_files = True,
),
"protoc": attr.label(
Expand All @@ -105,6 +110,14 @@ scala_proto_toolchain = rule(
[proto rules documentation](https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_library)
""",
),
"_main_generator_dep": attr.label(
default = Label(
"//src/scala/scripts:scalapb_codegenerator_wrapper",
),
allow_single_file = True,
executable = False,
cfg = "exec",
),
},
)

Expand All @@ -119,9 +132,9 @@ scala_proto_deps_toolchain = rule(
attrs = {
"dep_providers": attr.label_list(
default = [
"@io_bazel_rules_scala//scala_proto:scalapb_compile_deps_provider",
"@io_bazel_rules_scala//scala_proto:scalapb_grpc_deps_provider",
"@io_bazel_rules_scala//scala_proto:scalapb_worker_deps_provider",
Label("//scala_proto:scalapb_compile_deps_provider"),
Label("//scala_proto:scalapb_grpc_deps_provider"),
Label("//scala_proto:scalapb_worker_deps_provider"),
],
cfg = "target",
providers = [DepsInfo],
Expand Down
26 changes: 21 additions & 5 deletions scripts/create_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@
KIND_PROJECTOR_VERSION = "0.13.3"
PROTOBUF_JAVA_VERSION = "4.28.3"
JLINE_VERSION = '3.27.1'
SCALAPB_VERSION = '0.9.8'
SCALAPB_VERSION = '0.11.17'
PROTOC_BRIDGE_VERSION = '0.9.7'
GRPC_VERSION = '1.68.1'
GRPC_COMMON_PROTOS_VERSION = '2.48.0'
GRPC_LIBS = ['netty', 'protobuf', 'stub']
GUAVA_VERSION = '33.3.1-jre'

EXCLUDED_ARTIFACTS = set()
EXCLUDED_ARTIFACTS = set(["com.google.guava:listenablefuture"])

THIS_FILE = Path(__file__)
REPO_ROOT = THIS_FILE.parent.parent
Expand Down Expand Up @@ -79,26 +84,37 @@ def select_root_artifacts(scala_version, scala_major, is_scala_3) -> List[str]:

scalafmt_version = SCALAFMT_VERSION
scalapb_version = SCALAPB_VERSION
protoc_bridge_version = PROTOC_BRIDGE_VERSION

if scala_major == '2.11':
scalafmt_version = '2.7.5'
scalapb_version = '0.9.8'
protoc_bridge_version = '0.7.14'

root_artifacts = [
'com.google.api.grpc:proto-google-common-protos:' +
GRPC_COMMON_PROTOS_VERSION,
f'com.google.guava:guava:{GUAVA_VERSION}',
f'com.google.protobuf:protobuf-java:{PROTOBUF_JAVA_VERSION}',
f'com.thesamet.scalapb:compilerplugin_{scala_2_major}:' +
scalapb_version,
f'com.thesamet.scalapb:protoc-bridge_{scala_2_major}:' +
protoc_bridge_version,
f'com.thesamet.scalapb:scalapb-runtime_{scala_2_major}:' +
scalapb_version,
f'com.thesamet.scalapb:scalapb-runtime-grpc_{scala_2_major}:' +
scalapb_version,
f'org.scala-lang.modules:scala-parser-combinators_{scala_2_major}:' +
PARSER_COMBINATORS_VERSION,
f'org.scalameta:scalafmt-core_{scala_2_major}:{scalafmt_version}',
f'org.scalatest:scalatest_{scalatest_major}:{SCALATEST_VERSION}',
f'org.scala-lang:scala-compiler:{scala_2_version}',
f'org.scala-lang:scala-library:{scala_2_version}',
f'org.scala-lang:scala-reflect:{scala_2_version}',
f'org.scala-lang:scalap:{scala_2_version}',
f'org.scalameta:scalafmt-core_{scala_2_major}:{scalafmt_version}',
f'org.scalatest:scalatest_{scalatest_major}:{SCALATEST_VERSION}',
f'org.typelevel:kind-projector_{scala_2_version}:' +
KIND_PROJECTOR_VERSION,
]
] + [f'io.grpc:grpc-{lib}:{GRPC_VERSION}' for lib in GRPC_LIBS]

if scala_version == max_scala_2_version or is_scala_3:
# Since the Scala 2.13 compiler is included in Scala 3 deps.
Expand Down
13 changes: 13 additions & 0 deletions src/scala/scripts/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//scala:scala.bzl", "scala_binary", "scala_library")
load("//scala:scala_cross_version_select.bzl", "select_for_scala_version")

scala_library(
name = "scrooge_worker_lib",
Expand All @@ -22,6 +23,18 @@ scala_binary(
],
)

scala_library(
name = "scalapb_codegenerator_wrapper",
srcs = select_for_scala_version(
any_2_11 = ["ScalaPbCodeGeneratorWrapper_2_11.scala"],
since_2_12 = ["ScalaPbCodeGeneratorWrapper.scala"],
),
visibility = ["//visibility:public"],
deps = [
"//scala_proto:scalapb_worker_deps",
],
)

scala_library(
name = "scalapb_worker_lib",
srcs = ["ScalaPBWorker.scala"],
Expand Down
17 changes: 17 additions & 0 deletions src/scala/scripts/ScalaPbCodeGeneratorWrapper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package scripts

import protocgen.{CodeGenApp,CodeGenRequest,CodeGenResponse}

object ScalaPbCodeGenerator extends CodeGenApp {
def process(request: CodeGenRequest): CodeGenResponse = {
try {
scalapb.ScalaPbCodeGenerator.process(request)

} catch {
case e: Throwable =>
val stackStream = new java.io.ByteArrayOutputStream
e.printStackTrace(new java.io.PrintStream(stackStream))
CodeGenResponse.fail(stackStream.toString())
}
}
}
Loading

0 comments on commit 23ae356

Please sign in to comment.