evant / kotlin-inject

Dependency injection lib for kotlin
Apache License 2.0
1.29k stars 59 forks source link

Fix `NullPointerException` when resolving types #411

Closed vRallev closed 4 months ago

vRallev commented 4 months ago

KSP indicates that a type can for a type argument can be null, but kotlin-inject ignored that. This caused crashes in our case:

java.lang.NullPointerException
    at me.tatarka.kotlin.ast.KSAstType.getArguments(KSAst.kt:469)
    at me.tatarka.inject.compiler.InjectGeneratorKt.qualifier$checkTypeArgs(InjectGenerator.kt:256)
    at me.tatarka.inject.compiler.InjectGeneratorKt.qualifier$checkTypeArgs(InjectGenerator.kt:261)
    at me.tatarka.inject.compiler.InjectGeneratorKt.qualifier$checkTypeArgs(InjectGenerator.kt:261)
    at me.tatarka.inject.compiler.InjectGeneratorKt.qualifier$qualifier(InjectGenerator.kt:278)
    at me.tatarka.inject.compiler.InjectGeneratorKt.qualifier(InjectGenerator.kt:283)
    at me.tatarka.inject.compiler.TypeCollector.collectTypeInfo(TypeCollector.kt:391)
    at me.tatarka.inject.compiler.TypeCollector.collect(TypeCollector.kt:16)
    at me.tatarka.inject.compiler.TypeCollector.collect$default(TypeCollector.kt:15)
    at me.tatarka.inject.compiler.InjectGenerator.collectTypes(InjectGenerator.kt:177)
    at me.tatarka.inject.compiler.InjectGenerator.generateInjectComponent(InjectGenerator.kt:85)
    at me.tatarka.inject.compiler.InjectGenerator.generate(InjectGenerator.kt:68)
    at me.tatarka.inject.compiler.ksp.ProcessInjectKt.process(ProcessInject.kt:78)
    at me.tatarka.inject.compiler.ksp.ProcessInjectKt.processInject(ProcessInject.kt:28)
    at me.tatarka.inject.compiler.ksp.ProcessInjectKt.processInject$default(ProcessInject.kt:19)
    at me.tatarka.inject.compiler.ksp.InjectProcessor.process$lambda$0(InjectProcessor.kt:46)
    at kotlin.sequences.FilteringSequence$iterator$1.calcNext(Sequences.kt:171)
    at kotlin.sequences.FilteringSequence$iterator$1.hasNext(Sequences.kt:194)
    at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:813)
    at me.tatarka.inject.compiler.ksp.InjectProcessor.process(InjectProcessor.kt:47)

I tried reproducing this issue, but I believe it has to do with certain types coming from pre-compiled artifacts. The crash occurred for us with:

Pair<KClass<out BaseModel>, () -> Renderer<*>>

Where for Renderer<*> the argument is resolved as star variance without any further type.

vRallev commented 4 months ago

Fixed Detekt

vRallev commented 4 months ago

Good enough for now, may be worth filing an issue about star projections since this may come up again in the future.

I filed #412