Skip to content

compileSafety: false positive 'Missing definition' error during test source set compilation (deferred validation regression) #31

@dmitry-stakhov

Description

@dmitry-stakhov

Description

After upgrading from 0.6.2 to 1.0.0-RC2, compileSafety causes a false positive "Missing definition" error when compiling test source sets (compileDebugUnitTestKotlin). The main source set compiles successfully — the error only appears during test compilation.

The generated hint file for the main source set correctly includes the binding (verified via javap), but the compiler plugin's deferred validation in the test compilation cannot resolve it.

Environment

  • Koin BOM: 4.2.1
  • Koin Compiler Plugin: 1.0.0-RC2
  • Kotlin: 2.3.20
  • Project type: Kotlin Multiplatform (Android + iOS), multi-module

Reproduction

Module structure

:shared   → SharedModule   @ComponentScan("com.example.shared")
:feature  → FeatureModule  @ComponentScan("com.example.feature"), includes SharedModule
:app      → AppModule      @ComponentScan("com.example"), includes FeatureModule

Shared module — interface

// :shared — com.example.shared.bridge.UserRepository
interface UserRepository {
    fun getUserId(): String
}

Feature module — ViewModel consuming the interface

// :feature — com.example.feature.ui.HomeViewModel
@KoinViewModel
class HomeViewModel(
    @Provided
    private val userRepository: UserRepository,
) : ViewModel() { ... }

App module — implementation

// :app — com.example.app.data.UserRepositoryImpl
@Single
class UserRepositoryImpl(
    private val context: Context,
) : UserRepository {
    override fun getUserId(): String = "..."
}

App module — root module

// :app
@Module(includes = [FeatureModule::class])
@ComponentScan("com.example")
class AppModule { ... }

Koin initialization

startKoin {
    androidContext(application)
    module<AppModule>()
}

Test (any test in :app)

// :app/src/test/...
class SomeTest {
    @Test
    fun example() {
        // Doesn't even need to reference UserRepository
        assertEquals(1, 1)
    }
}

Error

> Task :app:compileDebugUnitTestKotlin FAILED
e: [Koin] Missing definition: com.example.shared.bridge.UserRepository
  Required by a call site in a dependency module (deferred validation).
  No matching definition found in any declared module.
  Check your declaration with Annotation or DSL.

Key observations

  1. Main compilation succeeds — the generated hint file correctly registers the binding:

    componentscan_..._AppModule_single(UserRepositoryImpl, UserRepository)
    
  2. Test compilation fails — the deferred validation from the dependency module :feature cannot be resolved during the test source set compilation, even though the main source set already resolved it.

  3. @Provided is correctly set on the ViewModel parameter in the :feature module.

  4. Workaround: Setting koinCompiler { compileSafety = false } in the :app module resolves the issue, but disables all compile-time validation for that module.

Expected behavior

The compiler plugin should either:

  • Not run deferred validation during test source set compilation, or
  • Resolve deferred bindings against the main source set's generated hints during test compilation

Related issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions