# KSP / Hilt Circular Dependency Resolution

When you add a **new `@Singleton @Inject constructor` class** and then **add it as a constructor parameter to an existing `@Singleton @Inject` class**, KSP's symbol processor may fail with:

```
e: [ksp] InjectProcessingStep was unable to process 'ExistingClass(...error.NonExistentClass...)' 
because 'error.NonExistentClass' could not be resolved.
```

This happens because KSP processes the existing class BEFORE the new class's symbol is available. There are NO actual Kotlin compilation errors — the class compiles fine but KSP can't find it.

## Fix: Use `dagger.Lazy<T>`

Instead of:
```kotlin
class ExistingRepository @Inject constructor(
    private val newService: NewService,  // ← KSP can't resolve this
)
```

Use:
```kotlin
class ExistingRepository @Inject constructor(
    private val newService: dagger.Lazy<NewService>,
)
```

And call `.get()` at the usage site:
```kotlin
val result = newService.get().doThing()
```

**Why this works:** `dagger.Lazy<T>` defers type resolution — Dagger creates a provider that constructs `T` on first `.get()`. KSP sees `Lazy<...>` as an interface it can generate a binding for without needing the actual class symbol in the same processing pass.

## Alternative: @Module @Provides

If `dagger.Lazy` doesn't work (rare), create a Hilt module:
```kotlin
@Module
@InstallIn(SingletonComponent::class)
object NewServiceModule {
    @Provides @Singleton
    fun provideNewService(deps: Deps): NewService = NewService(deps)
}
```

Then remove `@Inject constructor` from `NewService` (only one binding source).

## Clean Build After Structural Changes

If the error persists even after fixing:
```bash
rm -rf app/build
./gradlew :app:compileDebugKotlin --no-build-cache --console=plain
```

The Gradle configuration cache can retain stale KSP resolution results across builds.
