google / google/ksp

[question]Cross-module generation consultation

Open
#2,573 1 comment 0 reactions 0 assignees View on GitHub
stale
Dominant language
Kotlin
Stars
3.5k
Forks
415
Avg merge
1d 2h
Merged PRs (30d)
53

Description

Problem Description
I'm developing a KSP processor that requires cross-module code generation in a KMP project. The current setup is:

Analysis module: backend:model (contains @Entity annotated classes)

Target module: composeApp (KMP Compose module)

Code generation: Need to generate Compose form functions based on entity analysis

Current Situation and Issue
When generating code to the composeApp module's build directory:

kotlin
// In backend:model's KSP processor
// form gradle ksp options val outputPath = projectDir.resolve("composeApp/build/generated/ksp/metadata/commonMain/kotlin").absolutePath

codeGenerator.createNewFile(
dependencies = Dependencies(false),
packageName = "com.example.generated",
fileName = "GeneratedForm",
extensionName = "kt",
outputDirectory = outputPath //There seems to be no such option.
)
The generated code gets overwritten or cleaned by composeApp module's compilation tasks (e.g., compileKotlin, build cleanup operations).

Temporary Workaround
Currently using file IO to generate to source directory as a workaround:

kotlin
// Working but non-ideal solution
val sourceDir = projectDir.resolve("composeApp/src/commonMain/kotlin").absolutePath
// This works but pollutes the source directory
Questions
What is the recommended approach for cross-module code generation in KSP without output being overwritten?

How to properly configure Gradle tasks to preserve KSP-generated code in the build directory?

What are the best practices for maintaining generated code in multi-module KMP projects?

Expected Behavior
I expect that code generated by KSP in one module's build directory should:

Persist through the target module's compilation process

Be properly included in the target module's compilation classpath

Not be cleaned or overwritten by subsequent build tasks

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.