Skip to content

Kmp Source Sets for gRPC #402

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

Open
wants to merge 13 commits into
base: grpc-release
Choose a base branch
from
Open

Kmp Source Sets for gRPC #402

wants to merge 13 commits into from

Conversation

Mr3zee
Copy link
Collaborator

@Mr3zee Mr3zee commented Jul 17, 2025

Subsystem
Gradle Plugin

Problem Description
Current solution uses com.google.protobuf plugin.
It is limiting in different ways:

  • No support of KMP sources (and it is hard to add on top of it)
  • Limited customisation - no lint, no breaking changes detection
  • Somewhat hard Gradle plugin integration

Options:

  • Custom Gradle plugin that manages protoc binary
    • Most flexible (+)
    • Most work to do (-)
  • builf.buf Gradle plugin
    • All perks of Buf - linter, generator, breaking changes detector, etc (+)
    • Subpar Gradle plugin (-)
      • Users need to manage custom Buf config files
      • Tasks are not source set independent - one task per module.
      • No usage of Gradle Provider API

Solution
Blend of Buf's great capabilities to manage proto files and our Gradle plugin to manage Buf.

We use Buf's executable but we configure its workspace using our Gradle tasks:

  • We generate a workspace per source set
  • For Kotlin/JVM projects we use src/main/proto and src/test/proto directory sets
  • for Kotlin/Multilptaform we use src/jvmMain/proto and src/jvmTest/proto directory sets
    • Will be replaced with src/commonMain/proto and src/commonTest/proto directory sets later
  • Other source directory sets are NOT used (nativeMain, jsMain, etc)
  • Each sourceSet can be customised using protoSourceSets extension
    • include/exclude files as in a regular source set protoSourceSets.jvmTest { proto.exclude("exclude/**") }
    • Add custom protoc plugins using protoSourceSets.jvmTest { protocPlugin(rpc.grpc.protocPlugins.kxrpc) }
      • Predifined plugins are added by default
  • Each source set is a separate Buf workspace
    • It has a generated buf.yaml and buf.gen.yaml files
    • buf.yaml defines modules:
      • proto for files to be used for generation (in both main and test sets)
      • import for files used as imported (resolvable but not used for generation) - used in test sets to import proto files from main
    • buf.gen.yaml defines plugins:
      • Uses plugins from the rpc.grpc.protocPlugins container.
        • Predefined kxrpc, protobufJava, grpcJava and grpcKotlin
      • Plugins can be customized with both protoc options and Buf options using ProtocPlugin class
      • Plugins can be marked as generating java code to be automatically added to java source sets
      • New plugins can be added in rpc.grpc.protocPlugins container.
    • Each workspace has dedicated tasks:
      • generateBufYaml<sourceSet> - generates buf.yaml
      • generateBufGenYaml<sourceSet> - generates buf.gen.yaml
      • processProtoFiles<sourceSet> - copies proto files to the workspace directory
      • bufGenerate<sourceSet> - runs buf generate command
    • buf tasks can be customised using rpc.grpc.buf extension.
      • buf generate task can be customised using rpc.grpc.buf.generate extension.
    • Only buf generate task is supported out-of-the-box
    • Users can add custom buf tasks
      • Plain buf task (only access to the executable) - using registerBufExecTask
      • Workspace tasks (they are run one generated workspace) - using rpc.grpc.buf.tasks.registerWorkspaceTask
      • This way users can add first class support for other functions like lint without explicit support from us
  • Buf output can be seen using --debug option
  • As we can't publish to BSR that easily - our protoc plugin is published to Maven, dowloaded and used as a local plugin, and executed with java.home or a user specified java executable

Next steps

Comming in next PRs in no particular order and with no particular deadlines:

  • Gradle plugin tests
  • Android source sets
  • Fixes in our protoc plugin for import types
  • User managed buf workspace
  • Dokka APIs for the gradle plugin
  • Docs for new gradle plugin apis
  • Ktor Generator plugin

@Mr3zee Mr3zee self-assigned this Jul 17, 2025
@Mr3zee Mr3zee added the feature New feature or request label Jul 17, 2025
@Mr3zee Mr3zee force-pushed the grpc/kmp-sources branch from 8baedaf to 06a0daf Compare July 18, 2025 16:38
@Mr3zee Mr3zee force-pushed the grpc/kmp-sources branch from 3829654 to 45bd933 Compare July 22, 2025 14:55
@Mr3zee Mr3zee marked this pull request as ready for review July 22, 2025 20:53
@Mr3zee Mr3zee requested review from e5l and Jozott00 July 22, 2025 20:54
return property
}

// TODO change to commonMain/commonTest in docs when it's supported
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add link to an issue

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

Successfully merging this pull request may close these issues.

2 participants