Skip to content
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

Adding KSP dependencies in Project.afterEvaluate results in KSP tasks not being registered #1524

Open
ZacSweers opened this issue Sep 5, 2023 · 4 comments

Comments

@ZacSweers
Copy link
Contributor

We sometimes have plugins that defer configuration until after buildscripts are evaluated to get a correct picture of the the project's setup. If we defer adding KSP dependencies until this, it appears that KSP's gradle plugin will end up incorrectly never registering KSP tasks if they're not added directly in the buildscript.

Example build file

plugins {
  kotlin("jvm")
  id("com.google.devtools.ksp")
}

afterEvaluate {
  dependencies.add("ksp", exampleDep)
}

The above will fail to register any KSP tasks even though a KSP processor dep was added eventually

@ting-yuan
Copy link
Collaborator

KSP creates tasks when it is called by Kotlin Gradle Plugin in the middle of configuration phase. If I defer the task creation (if possible; there seems to be no reliable way to enforce configuration order for loosely coupled components), it'll likely break scripts / plugins that modify or read attributs of KSP's tasks. May I learn more about your use case where the dependency has to be added in the end of configuration?

@ZacSweers
Copy link
Contributor Author

My use case is a gradle plugin that hosts a compiler plugin and enables KSP as-needed. Here's the impl: https://github.com/ZacSweers/MoshiX/blob/main/moshi-ir/moshi-gradle-plugin/src/main/kotlin/dev/zacsweers/moshix/ir/gradle/MoshiGradleSubplugin.kt

@ting-yuan
Copy link
Collaborator

KSP checks the configuration here which is called indirectly in an afterEvaluate when KGP is applied. MoshiX adds KSP in its apply so it works currently. Deferring that with afterEvaluate in MoshiX would make the outcome depend on the order of the plugins applied I guess. IMHO this (order of configurations) might not be a case that was well taken into account when Gradle is designed.

Listening to dependency changes in KSP might work, but it would also require some significant changes. I'd consider this as a new feature instead of a bug and therefore it'll need to wait until the K2 migration is done.

Or any thoughts on simpler solutions?

@charbgr
Copy link

charbgr commented Sep 26, 2023

I have pretty much the same issue as described above! I apply ksp plugin immediately on applyProject as a fix for now.

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

No branches or pull requests

3 participants