Skip to content

Support for Kaptish feature - a build time optimization for Kapt#1529

Open
oliviernotteghem wants to merge 1 commit intobazel-contrib:masterfrom
oliviernotteghem:upstream-kaptish
Open

Support for Kaptish feature - a build time optimization for Kapt#1529
oliviernotteghem wants to merge 1 commit intobazel-contrib:masterfrom
oliviernotteghem:upstream-kaptish

Conversation

@oliviernotteghem
Copy link
Copy Markdown
Contributor

Add Kaptish: Fast Annotation Processing for Kotlin

Summary

This PR introduces kaptish, an experimental optimization for annotation processing that bypasses KAPT's expensive stub generation phase, resulting in 1.75-2x faster builds for projects using annotation processors
with Kotlin.

How it works

Traditional KAPT uses a 3-pass approach:

  1. Generate Java stubs from Kotlin sources (expensive)
  2. Run annotation processors on stubs
  3. Compile everything

Kaptish simplifies this to:

  1. Compile Kotlin sources directly to .class files
  2. Inject compiled class names into javac's annotation processing phase
  3. Run annotation processors on the compiled classes

This works because annotation processors primarily need type information (class names, method signatures, annotations), which is available in compiled bytecode.

Usage

Enable globally in toolchain:
define_kt_toolchain(
name = "kotlin_toolchain",
experimental_kaptish_enabled = True,
)

Opt-out per target:
kt_jvm_library(
name = "my_library",
srcs = ["MyClass.kt"],
plugins = ["//path/to:annotation_processor"],
tags = ["kaptish_disabled"], # Falls back to KAPT
)

Files changed

New files:

  • kotlin/internal/jvm/kaptish.bzl - Helper functions for kaptish mode
  • src/main/kotlin/io/bazel/kotlin/plugin/kaptish/KotlinClassInjectorPlugin.java - Annotation processor that injects Kotlin class names
  • src/main/kotlin/io/bazel/kotlin/plugin/kaptish/BUILD.bazel - Build rules
  • src/main/kotlin/io/bazel/kotlin/plugin/kaptish/README.md - Documentation
  • src/test/data/jvm/kaptish/ - Test fixtures
  • src/test/kotlin/io/bazel/kotlin/KotlinJvmKaptishAssertionTest.kt - Tests

Modified files:

  • kotlin/internal/toolchains.bzl - Added experimental_kaptish_enabled attribute
  • kotlin/internal/jvm/compile.bzl - Integrated kaptish into compilation flow
  • src/main/kotlin/BUILD - Added kaptish alias

Test plan

  • KotlinJvmKaptishAssertionTest - Verifies kaptish mode generates AP code
  • KotlinJvmKaptAssertionTest - Verifies KAPT still works
  • KotlinJvmBasicAssertionTest - Verifies no regressions
  • All assertion tests pass

bazel test //src/test/kotlin/io/bazel/kotlin:KotlinJvmKaptishAssertionTest
bazel test //src/test/kotlin/io/bazel/kotlin:assertion_tests

Limitations

  • Uses internal javac APIs (com.sun.tools.javac.*) which may change between JDK versions
  • Some annotation processors may behave differently with bytecode vs source stubs
  • Experimental: users should test thoroughly before enabling globally

@restingbull
Copy link
Copy Markdown
Collaborator

Add an example of using kaptish? (Those are our integration tests, for the most part.)

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

Successfully merging this pull request may close these issues.

2 participants