Skip to content

@Single/@Factory annotations on top-level functions are silently ignored, no definition generated, NoDefinitionFoundException at runtime #62

Description

@dakatso

When @Single or @Factory is placed on a top-level function, the compiler
plugin silently ignores it. No definition is generated, and the app crashes
at runtime with NoDefinitionFoundException.

No compile-time warning or error is produced.

To Reproduce

@Single
fun json(): Json = Json {
    isLenient = true
    ignoreUnknownKeys = true
}

At runtime:
No definition found for type 'kotlinx.serialization.json.Json' on scope '['root']'

Workaround

Wrapping the return value in a provider class works:

@Single
class JsonProvider {
    fun get(): Json = Json {
        isLenient = true
        ignoreUnknownKeys = true
    }
}

Environment

  • koin: 4.2.2
  • koin-annotations: 4.2.2
  • koin-compiler-plugin: 1.0.1
  • kotlin: 2.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions