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

scalapb-validate integration with akka-grpc #70

Closed
thesamet opened this issue Jan 27, 2021 · 12 comments
Closed

scalapb-validate integration with akka-grpc #70

thesamet opened this issue Jan 27, 2021 · 12 comments

Comments

@thesamet
Copy link
Contributor

thesamet commented Jan 27, 2021

akka-grpc defaults to compiling with flat_package=true. This results in user's generated code to reference the flat-package location of Scala sources generated by validate.proto. However, the validate.proto shipped with scalapb-validate, through common-protos defaults to flat_package=false.

The goal of this ticket is to come up with a straightforward way for akka-grpc users to use scalapb-validate.

@bjaglin
Copy link
Contributor

bjaglin commented Jan 27, 2021

Thanks for opening this to centralize the discussion. I have started thinking about this, see scalapb/ScalaPB#873 (comment) & scalapb/protoc-bridge#158.

@thesamet
Copy link
Contributor Author

thesamet commented Jan 27, 2021

Idea 1: One workaround that would work with current versions is to have users throw in a proto file into their project that gives package-scoped option to disable flat_package for validate:

package validate;
import "scalapb/scalapb.proto";
option (scalapb.options) = {
  scope: PACKAGE
  flat_package: false
};

Idea 2: Building on idea 1, we can have a generator option that would have the same impact over specific packages, so akka-grpc users can exclude validate.

Idea 3: make a special case for validate package in the generator like we already do for google.protobuf and scalapb: https://github.com/scalapb/ScalaPB/blob/05b5aea520e621d0946ca08acc760bf306546199/compiler-plugin/src/main/scala/scalapb/compiler/DescriptorImplicits.scala#L909-L910

@bjaglin
Copy link
Contributor

bjaglin commented Jan 27, 2021

We have settled on a variant of idea 1 (since we didn't want to change all source code): disable flat_package at the generator level, but enable it selectively for our packages.

There is a gotcha with that, as Akka gRPC generators don't seem to handle package-level flat_package so flat_package should be disabled only for the ScalaPB gen. I was planning to open a PR on akka-grpc to fix that (I assume DescriptorImplicits is not used properly), update bjaglin/sbt-protoc@53dbd6a#diff-bbd8a5163e4909c406d2fd2b12cca4345f91859fae3676fd8ee189d121e2c9f1R2-R10 (which is broken at the moment), and write a scripted there.

@bjaglin
Copy link
Contributor

bjaglin commented Jan 28, 2021

Idea 1 does not seem to work as activating flat_package through the generator option takes precedence over the file/package-level setting: https://github.com/scalapb/ScalaPB/blob/05b5aea520e621d0946ca08acc760bf306546199/compiler-plugin/src/main/scala/scalapb/compiler/DescriptorImplicits.scala#L917

@thesamet
Copy link
Contributor Author

thesamet commented Jan 28, 2021

https://github.com/scalapb/ScalaPB/blob/05b5aea520e621d0946ca08acc760bf306546199/compiler-plugin/src/main/scala/scalapb/compiler/DescriptorImplicits.scala#L909-L910

I think we should change to make it possible for the the file-level option to override with a false value.

thesamet added a commit to thesamet/sbt-protoc that referenced this issue Jan 30, 2021
When a protobuf dependency jar contains a Scalapb-Options-Proto in its
manifest, the mentioned file will get automatically added to the list
of sources for the protoc invocation. This makes ScalaPB aware of
the options used to compile the third-party jar.

See scalapb/scalapb-validate#70
thesamet added a commit to scalapb/ScalaPB that referenced this issue Jan 30, 2021
thesamet added a commit to thesamet/sbt-protoc that referenced this issue Jan 31, 2021
thesamet added a commit to scalapb/common-protos that referenced this issue Jan 31, 2021
@bjaglin
Copy link
Contributor

bjaglin commented Jan 31, 2021

There is a gotcha with that, as Akka gRPC generators don't seem to handle package-level flat_package

ScalaPB extensions are currently not registered in akka-grpc so the compiler plugin does not see them, I am sending a PR to akka-grpc shortly.

@thesamet
Copy link
Contributor Author

thesamet commented Feb 1, 2021

ScalaPB extensions are currently not registered in akka-grpc so the compiler plugin does not see them, I am sending a PR to akka-grpc shortly

Thanks! Does it impact the solution? akka-grpc might erroneously think that 'validate' is compiled with flat_package - but does the code it generates references it?

@bjaglin
Copy link
Contributor

bjaglin commented Feb 1, 2021

It makes what I described in #70 (comment) easier (the scripted is precisely what we are using to workaround this issue).

I believe it does not impact in any way the solution you are implementing.

thesamet added a commit to scalapb/ScalaPB that referenced this issue Feb 15, 2021
bjaglin pushed a commit to bjaglin/akka-grpc that referenced this issue Feb 15, 2021
Fixes scripted 10-scalapb-validate, see
* scalapb/ScalaPB#873 (comment)
* scalapb/scalapb-validate#70

Other changes
* cacheClassLoaders is now deprecated, see
  thesamet/sbt-protoc@2a730a4
* The new classloader caching implementation resolves sandboxed
  generators artifacts earlier, even if they are no sources. That is
  the case for Test / protocGenerate, which was starting before
  codeGenProject / Compile / publishLocal, causing:
  > Error downloading com.lightbend.akka.grpc:akka-grpc-codegen_2.12:
  > 1.1.0-5-c5975cd5
@bjaglin
Copy link
Contributor

bjaglin commented Feb 15, 2021

I think we can close this now that akka/akka-grpc#1264 verifies the integration with sbt-protoc 1.0.1 / scalapb-validate 0.2.1. Thanks for making the integration seamless!

bjaglin pushed a commit to bjaglin/akka-grpc that referenced this issue Feb 16, 2021
Fixes scripted 10-scalapb-validate, see
* scalapb/ScalaPB#873 (comment)
* scalapb/scalapb-validate#70

Other changes
* cacheClassLoaders is now deprecated, see
  thesamet/sbt-protoc@2a730a4
* Forcing recompilation is no longer necessary as the cache gets
  invalidated if a sandbox genererator classpath is updated, see
  thesamet/sbt-protoc@80824b0
* The new classloader caching implementation resolves sandboxed
  generators artifacts earlier, even if they are no sources. That is
  the case for Test / protocGenerate, which was starting before
  codeGenProject / Compile / publishLocal, causing:
  > Error downloading com.lightbend.akka.grpc:akka-grpc-codegen_2.12:
  > 1.1.0-5-c5975cd5
bjaglin pushed a commit to bjaglin/akka-grpc that referenced this issue Feb 16, 2021
Fixes scripted 10-scalapb-validate, see
* scalapb/ScalaPB#873 (comment)
* scalapb/scalapb-validate#70

Other changes
* cacheClassLoaders is now deprecated, see
  thesamet/sbt-protoc@2a730a4
* Forcing recompilation is no longer necessary as the cache gets
  invalidated if a sandbox genererator classpath is updated, see
  thesamet/sbt-protoc@80824b0
* The new classloader caching implementation resolves sandboxed
  generators artifacts earlier, even if they are no sources. That is
  the case for Test / protocGenerate, which was starting before
  codeGenProject / Compile / publishLocal, causing:
  > Error downloading com.lightbend.akka.grpc:akka-grpc-codegen_2.12:
  > 1.1.0-5-c5975cd5
@bjaglin
Copy link
Contributor

bjaglin commented Feb 16, 2021

Actually there is still one thing missing: usage of scalapb/validate.proto to customize scalapb-validate through file/package-level options is not possible out of the box from akka-grpc.

import "scalapb/scalapb.proto";
import "scalapb/validate.proto";

option (scalapb.options) = {
    scope: FILE
    [scalapb.validate.file] {
        validate_at_construction: true
    }
};
[info] [error] /tmp/sbt_70a87d09/target/scala-2.12/akka-grpc/main/example/myapp/helloworld/grpc/HelloworldProto.scala:11:22: object ValidateProto is not a member of package scalapb.validate
[info] [error]     scalapb.validate.ValidateProto,

From my understanding, we should either

  1. have scalapb-validate-core expose its own option proto just like scalapb/common-protos@7c9595a
  2. update https://github.com/scalapb/ScalaPB/blob/05b5aea520e621d0946ca08acc760bf306546199/compiler-plugin/src/main/scala/scalapb/compiler/DescriptorImplicits.scala#L909-L910
     private def isNonFlatDependency =
-      (file.getPackage == "google.protobuf") || (file.getPackage == "scalapb")
+      (file.getPackage == "google.protobuf") || (file.getPackage.startsWith("scalapb"))

Happy to open a PR for (2) on ScalaPB as it looks like the easiest.

bjaglin pushed a commit to bjaglin/akka-grpc that referenced this issue Feb 16, 2021
Fixes scripted 10-scalapb-validate, see
* scalapb/ScalaPB#873 (comment)
* scalapb/scalapb-validate#70

Other changes
* cacheClassLoaders is now deprecated, see
  thesamet/sbt-protoc@2a730a4
* Forcing recompilation is no longer necessary as the cache gets
  invalidated if a sandbox genererator classpath is updated, see
  thesamet/sbt-protoc@80824b0
* The new classloader caching implementation resolves sandboxed
  generators artifacts earlier, even if they are no sources. That is
  the case for Test / protocGenerate, which was starting before
  codeGenProject / Compile / publishLocal, causing:
  > Error downloading com.lightbend.akka.grpc:akka-grpc-codegen_2.12:
  > 1.1.0-5-c5975cd5
@thesamet thesamet reopened this Feb 16, 2021
@thesamet
Copy link
Contributor Author

v0.2.2 is released with the manifest option - can you try it out?

@bjaglin
Copy link
Contributor

bjaglin commented Feb 16, 2021

v0.2.2 is released with the manifest option - can you try it out?

I just verified the fix with akka/akka-grpc@e4a39c6, thanks for the quick turnaround!

And this time, I think we are good to close this.

raboof pushed a commit to akka/akka-grpc that referenced this issue Feb 17, 2021
* move scalapb-validation sbt instructions to scripted

* bump sbt-protoc for better integration with common-protos

Fixes scripted 10-scalapb-validate, see
* scalapb/ScalaPB#873 (comment)
* scalapb/scalapb-validate#70

Other changes
* cacheClassLoaders is now deprecated, see
  thesamet/sbt-protoc@2a730a4
* Forcing recompilation is no longer necessary as the cache gets
  invalidated if a sandbox genererator classpath is updated, see
  thesamet/sbt-protoc@80824b0
* The new classloader caching implementation resolves sandboxed
  generators artifacts earlier, even if they are no sources. That is
  the case for Test / protocGenerate, which was starting before
  codeGenProject / Compile / publishLocal, causing:
  > Error downloading com.lightbend.akka.grpc:akka-grpc-codegen_2.12:
  > 1.1.0-5-c5975cd5

* demonstrate usage of validate_at_construction

Requires scalapb/scalapb-validate@8529a55
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