rules_scala icon indicating copy to clipboard operation
rules_scala copied to clipboard

Unused dependency checker exception when wrapping proto_library

Open kitbuilder587 opened this issue 8 months ago • 1 comments

I'm currently migrating our Scala 2 monorepo to Scala 3. During this process, we discovered that scala_proto_library does not support a scala_version attribute and therefore cannot be cross-compiled.

Given that our monorepo is too large to migrate to Scala 3 in a single commit, we decided to continue using Scala 2-generated protos even in Scala 3 code while the migration is running. To enable this, I wrote a wrapper around scala_proto_library:

load("@io_bazel_rules_scala//scala_proto:scala_proto.bzl", _scala_proto_library = "scala_proto_library")

def scala_proto_library(name, visibility, deps, **kwargs):
    _scala_proto_library(
        name = name + "_impl",
        visibility = visibility,
        deps = deps,
    )

    _scala_library(
        name = name,
        visibility = visibility,
        deps = [":" + name + "_impl"],
        scala_version = scala2_V,
    )

This approach works well for Scala 3 builds. However, Scala 2 builds fail with an error about an unused dependency—even though the dependency is actually used in other targets. Replacing deps with exports also doesn't help.

I created a minimal reproduction repository to demonstrate the issue.

Do you have any suggestions on how to resolve this? Also, is there any way to enable cross-compilation support for scala_proto_library?

kitbuilder587 avatar Jun 23 '25 07:06 kitbuilder587

I haven't tried this, but ScalaPB > SBT Settings > Additional options to the generator mentions the scala3_sources option:

If set, generates sources that are error-free under -source future with Scala 3, or Xsource:3 with Scala 2.13.

Looking briefly at the example repo, it's using some very old versions of rules_scala, rules_python, and rules_jvm_external, to mention just a few. scala3_sources first appeared in ScalaPB v0.11.14, whereas rules_scala v6.6.0 uses ScalaPB 0.9.7.

So I'd encourage upgrading to rules_scala v7.0.0 (and newer rules_jvm_external and rules_python) and giving scala3_sources a try. There are examples of passing scala3_sources via default_gen_opts in the scala_proto_library documentation. (The default_gen_opts option didn't land until #1718 and #1730, which are part of v7.0.0.)

As a bonus, v7.0.0 fully supports Bzlmod now, too. It still supports WORKSPACE, but with an updated API resembling the Bzlmod API. The top level README has all the details.

Another option is to load("@rules_scala_config//:config.bzl", "SCALA_VERSION") and use it to do different things in the macro. That would seem far from ideal, but maybe it could help if you're desperate.

mbland avatar Jun 23 '25 17:06 mbland