Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use a scala_proto_library target as a dependency? #1304

Closed
gergelyfabian opened this issue Oct 16, 2021 · 10 comments
Closed

How to use a scala_proto_library target as a dependency? #1304

gergelyfabian opened this issue Oct 16, 2021 · 10 comments

Comments

@gergelyfabian
Copy link
Contributor

gergelyfabian commented Oct 16, 2021

I've create a Protobuf configuration basing on available examples (https://github.com/liucijus/rules-scala-proto-toolchain):

proto_library(
    name = "proto_lib",
    srcs = [
        "src/main/protobuf/myproto1.proto",
        "src/main/protobuf/myproto2.proto",
    ],
    strip_import_prefix = "src/main/protobuf",
    deps = [],
)

scala_proto_library(
    name = "proto_scala_lib",
    deps = [":proto_lib"],
)

# Then I'd like to use the generated Scala files a dependency:
scala_library(
    name = "library",
    srcs = glob([
        "src/main/scala/**/*.scala",
    ]),
    deps = [
        ":proto_scala_lib",
    ],
)

With this configuration the "library" target does not see the files generated from protobuf ("not a member of package" errors).
I can see that the necessary files are produced by scala_proto_library ("proto_scala_lib"):

Target //{my-package}:proto_scala_lib up-to-date:
  bazel-bin/{my-package}/proto_lib_scalapb.jar
  ...
  bazel-bin/{my-package}/proto_lib_scalapb-src.jar

When I unpack bazel-bin/{my-package}/proto_lib_scalapb.jar, the generated files are in those jars. However I cannot give proto_lib_scalapb as a dependency for my library target.

I'm using rules_scala at version 6e37eac.

@gergelyfabian
Copy link
Contributor Author

Very interestingly, the following file is on the classpath, when I debug compiling my library target:

ZipArchiveClassPath(bazel-out/k8-fastbuild/bin/{my-package}/proto_lib_scalapb.jar,None)

@gergelyfabian
Copy link
Contributor Author

gergelyfabian commented Oct 16, 2021

If I add a repl target as:

scala_repl(
    name = "repl",
    deps = [":proto_scala_lib"],
)

Then I compile the REPL target, start the REPL and from there I can import the generated protobuf classes.

@gergelyfabian
Copy link
Contributor Author

This is some other issue, closing.

@gergelyfabian
Copy link
Contributor Author

This issue was caused by the scalapb compiler plugin's version.

@gergelyfabian
Copy link
Contributor Author

Reiterating. The issue is, that (comparing to sbt), when I create this configuration, there are missing Java classes, with the "${file}OuterClass" names. Is there any way to enable those?

I found a ticket that mentions this feature: scalapb/ScalaPB#26

I'm not sure, is there maybe a flag that I should turn on when using scala_proto_library to have these kind of classes generated?

@gergelyfabian gergelyfabian reopened this Oct 16, 2021
@gergelyfabian
Copy link
Contributor Author

It is about adding the following settings, it would be in sbt:

  PB.gens.java                    -> (sourceManaged in Compile).value,
  scalapb.gen(flatPackage = true) -> (sourceManaged in Compile).value

Wondering how these can be set up for Bazel.

@gergelyfabian
Copy link
Contributor Author

I found that I can create a proto toolchain, that has with_flat_package, but there is no more with_java option.

scala_proto_toolchain(
    name = "enable_all_options_toolchain_impl",
    visibility = ["//visibility:public"],
    with_flat_package = True,
    #with_java = True,
)

I guess with_java was removed. Is there a way to get an equivalent feature?

@gergelyfabian
Copy link
Contributor Author

This may be a duplicate of: #765

@simuons
Copy link
Collaborator

simuons commented Oct 18, 2021

Hi @gergelyfabian indeed this is duplicate of mentioned issue. with_java was dropped after implementation moved to aspects. java_conversions flag generates conversion methods to java code but unfortunately it (code generated by java plugin ie java_proto_library) cannot be made available to aspects (maybe things have changed since then). Sadly we don't have solution for this yet. I'm closing this issue and please follow the one you mentioned.

@simuons simuons closed this as completed Oct 18, 2021
@gergelyfabian
Copy link
Contributor Author

Thanks! I'll just write my workaround here, if anyone is wondering in the future:

# WORKSPACE:

http_archive(
    name = "rules_proto_grpc",
    sha256 = "28724736b7ff49a48cb4b2b8cfa373f89edfcb9e8e492a8d5ab60aa3459314c8",
    strip_prefix = "rules_proto_grpc-4.0.1",
    urls = ["https://github.com/rules-proto-grpc/rules_proto_grpc/archive/4.0.1.tar.gz"],
)

load("@rules_proto_grpc//:repositories.bzl", "rules_proto_grpc_repos", "rules_proto_grpc_toolchains")

rules_proto_grpc_toolchains()

rules_proto_grpc_repos()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

rules_proto_dependencies()

rules_proto_toolchains()

load("@rules_proto_grpc//scala:repositories.bzl", RULES_PROTO_GRPC_SCALA_MAVEN_ARTIFACTS = "MAVEN_ARTIFACTS", rules_proto_grpc_scala_repos = "scala_repos")

rules_proto_grpc_scala_repos()

load("@io_grpc_grpc_java//:repositories.bzl", "IO_GRPC_GRPC_JAVA_ARTIFACTS", "IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS", "grpc_java_repositories")

maven_install(
    name = "rules_proto_grpc_java_maven",
    artifacts = IO_GRPC_GRPC_JAVA_ARTIFACTS,
    generate_compat_repositories = True,
    override_targets = IO_GRPC_GRPC_JAVA_OVERRIDE_TARGETS,
    repositories = [
        "https://repo.maven.apache.org/maven2/",
    ],
)

load("@rules_proto_grpc_java_maven//:compat.bzl", rules_proto_grpc_java_compat_repositories = "compat_repositories")

# Note: This wasn't well documented, but I needed to include Java compat repositories.
rules_proto_grpc_java_compat_repositories()

grpc_java_repositories()

load("@rules_proto_grpc//java:repositories.bzl", rules_proto_grpc_java_repos = "java_repos")

rules_proto_grpc_java_repos()

# Override proto-grpc-scala artifacts with your own versions (if you need).
#RULES_PROTO_GRPC_SCALA_MAVEN_ARTIFACTS = [...]

maven_install(
    name = "rules_proto_grpc_scala_maven",
    artifacts = RULES_PROTO_GRPC_SCALA_MAVEN_ARTIFACTS,
    maven_install_json = "@//:rules_proto_grpc_scala_maven_install.json",
    repositories = [
        "https://repo1.maven.org/maven2",
    ],
)

load("@rules_proto_grpc_scala_maven//:defs.bzl", rules_proto_grpc_scala_pinned_maven_install = "pinned_maven_install")

rules_proto_grpc_scala_pinned_maven_install()


# BUILD:
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_proto_grpc//scala:defs.bzl", "scala_grpc_library")
load("@rules_proto_grpc//java:defs.bzl", "java_grpc_library")

proto_library(
    name = "proto_lib",
    srcs = [
        "test.proto"
    ],
    deps = [],
)

# You need to generate Java code separately.
java_grpc_library(
    name = "proto_java_lib",
    protos = [":proto_lib"],
)

# Generating scala code with "java_conversions" only creates the converters, but not the Java code itself.
scala_grpc_library(
    name = "proto_scala_lib",
    options = {
        "@rules_proto_grpc//scala:grpc_scala_plugin": [
            "java_conversions",
            "flat_package",
        ],
    },
    protos = [":proto_lib"],
    deps = [":proto_java_lib"],
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants