https://github.com/bitspittle/issue-ksp-gradle-cycle
A project which demonstrates how KSP generates a cyclical Gradle error if you try to generate code outside of KSP
https://github.com/bitspittle/issue-ksp-gradle-cycle
Last synced: over 1 year ago
JSON representation
A project which demonstrates how KSP generates a cyclical Gradle error if you try to generate code outside of KSP
- Host: GitHub
- URL: https://github.com/bitspittle/issue-ksp-gradle-cycle
- Owner: bitspittle
- Created: 2024-02-29T21:33:11.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-13T23:15:23.000Z (over 2 years ago)
- Last Synced: 2025-01-21T19:48:05.201Z (over 1 year ago)
- Language: Kotlin
- Size: 62.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# KSP Circular Dependency Issue
This project demonstrates the core of an issue we're having in our own real project.
* We have a KSP processor that generates a resource file (here, `":ksp:processor"`).
* We have a Gradle task which depends on the generated resource file in order to
create some Kotlin source (here, `":app"`'s `build.gradle.kts` file which defines `generateCodeTask`)
## Reproducing the issue
To see the error for yourself, just run `./gradlew :app:run` in the root directory.
The error will be something like this:
```
* Exception is:
org.gradle.api.CircularReferenceException: Circular dependency between the following tasks:
:app:generateCodeTask
\--- :app:kspKotlin
\--- :app:generateCodeTask
```
## Points of interest
### `GreetingResourceSymbolProcessor.kt`
Creates a `Greeting.txt` file in
`/app/build/generated/ksp/main/resources/org/example/ksp/`
---
### `/app/build.gradle.kts`
Defines a task `generateCodeTask` which depends on the generated resource file.
Note that we have to do a strange invocation to pull the resource out of the KSP task:
```kotlin
val inputFile = tasks.named("kspKotlin").map { kspTask ->
RegularFile {
kspTask.outputs.files.asFileTree.matching { include("**/Greeting.txt") }.singleFile
}
}
```
Maybe there's a better way, but this was the best we could find after hours of experimentation.
## Current Fix
To fix the issue, search the `build.gradle.kts` file for the string "To fix". Basically you need to comment out one way
and uncomment another. This solution came from our Slack discussion, but it will be going away in K2:
```kotlin
// Before
kotlin {
sourceSets.main {
kotlin.srcDir(generateCodeTask)
}
}
// After
tasks.named("compileKotlin") {
source(generateCodeTask)
}
```