Skip to content

Generate KSP classes jar as output #1329

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 7 commits into
base: master
Choose a base branch
from

Conversation

smocherla-brex
Copy link

@smocherla-brex smocherla-brex commented Jun 7, 2025

Fixes #1328

Right now, the ksp generated src JAR has the generated sources and files from META-INF/. This doesn't suffice for KSP processors (Micronaut) which rely on generated .class files in annotation processing which isn't included in that jar now. We could include these files in that src jar but wasn't sure if it makes sense to include .class files, so I've added another output to the KotlinKsp task which generates this jar and is then forwarded as one of the outputs of the kt_jvm_library action.

I'll test this internally and verify it fixes our issue. I wanted to add tests with Micronaut in src/test along with the existing KSP test cases, but Micronaut 4/KSP requires Java 17 which would require upgrading rules_kotlin to Java 17 which could be a breaking change for other users, so I didn't (let me know if I should). Instead I've added tests with a dummy processor that generates bytecode and updated the tests to verify the functionality.

@@ -145,6 +145,9 @@ class KotlinJvmTaskExecutor
if (outputs.generatedKspSrcJar.isNotEmpty()) {
context.execute("creating KSP generated src jar", ::createGeneratedKspKotlinSrcJar)
}
if (outputs.generatedKspClassesJar.isNotEmpty()) {
context.execute("creating KSP generated classes jar", ::createdGeneratedKspClassesJar)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you be able to add some tests for this to KotlinJvmKspAssertionTest?

Copy link
Author

@smocherla-brex smocherla-brex Jun 8, 2025

Choose a reason for hiding this comment

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

I wasn't able to add tests with Micronaut because that'd require upgrading the repo to Java 17 (which introduces other breakages and probably out of scope for this PR). I'm not aware of other KSP processors that generate bytecode on the fly, so I wrote an example/dummy processor that generates class files and then added test cases to validate those class files are present in the final jar. Let me know if that works.

/**
* An example KSP processor that generates bytecode to be used only for tests
*/
class BytecodeGeneratorProcessor(
Copy link
Author

Choose a reason for hiding this comment

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

This was written purely for tests to validate the functionality.

@smocherla-brex smocherla-brex marked this pull request as ready for review June 8, 2025 03:09
@smocherla-brex smocherla-brex requested a review from Bencodes June 22, 2025 17:50
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.

KSP plugin doesn't provide generated classes as a jar
2 participants