Skip to content

KSP Incremental Compilation Failure If Module Class is Generated by Another Non-Dirtied Processor #4878

@sam-step

Description

@sam-step

I have a simple KSP processor which generates a Dagger Module. When using Dagger with KSP, incremental compilation fails if the input source of that generated Module is not dirtied.

E.g. given:

package app

@TargetAnnotation
class AnnotatedClass

and the following Module generated by a KSP processor:

package app.generated

// Output is non-aggregating, declares AnnotatedClass as source file.
@dagger.Module
class Generated_AnnotatedClassModule {
    @dagger.Provides
    fun provide(): app.ClassProvidedByGeneratedModule = app.ClassProvidedByGeneratedModule()
}

which is installed in a component:

package app

@Component(modules = [app.generated.Generated_AnnotatedClassModule::class])
interface MyComponent {
    fun classProvidedByGeneratedModule(): ClassProvidedByGeneratedModule
    fun otherClass(): OtherClass
}

making a change to, say, OtherClass, cause the incremental compilation to fail because the Dagger compiler cannot see Generated_AnnotatedClassModule:

> Task :app:kspKotlin FAILED
e: [ksp] ComponentProcessingStep was unable to process 'app.MyComponent' because 'generated.Generated_AnnotatedClassModule' could not be resolved.

Dependency trace:
    => element (INTERFACE): app.MyComponent
    => annotation type: dagger.Component
    => annotation: @dagger.Component(modules={generated.Generated_AnnotatedClassModule}, dependencies={})
    => annotation value (TYPE_ARRAY): modules={generated.Generated_AnnotatedClassModule}
    => annotation value (TYPE): modules=generated.Generated_AnnotatedClassModule
    => type (ERROR annotation value type): generated.Generated_AnnotatedClassModule

If type 'generated.Generated_AnnotatedClassModule' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type 'generated.Generated_AnnotatedClassModule' is on your classpath.
4 actionable tasks: 1 executed, 3 up-to-date

My assumption is that because the Generated_AnnotatedClassModule is not declared as a source to any of the files generated by Dagger, KSP doesn't bother making it available on incremental compilation. (Playing around with a couple sample processors in a reproducer project, I see this is the case. If I have two processors A and B: A generates a class that B needs to resolve in some way to generate its own class, upon incremental compilation where A's class is not dirtied, B won't see the class A generated unless it declared it as a source to its output on the original compilation).

I'm not sure if this is an issue with Dagger, with KSP, or a misunderstanding of expectations on my part here.

I have a minimal project demonstrating the above here: https://github.com/sam-step/dagger-ksp-incremental-repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions