Skip to content

fix: no false KOIN-D002 on a Koin built elsewhere (#58), no false KOIN-D006 on activityViewModel (#121) - #136

Merged
arnaudgiuliani merged 2 commits into
1.2.2from
arnaud/ktz-5224-unknown-graph-warning
Nov 2, 2026
Merged

arnaudgiuliani merged 2 commits into
1.2.2from
arnaud/ktz-5224-unknown-graph-warning

Conversation

@arnaudgiuliani

Copy link
Copy Markdown
Member

Two false errors that push people to turn compileSafety off, mostly in test source sets.

#58 — koin.get<T>() on a Koin built elsewhere

A call site was judged against whatever koinApplication { } / startKoin { } the compilation declared, even when its Koin came from somewhere else: a shared test fixture, a function parameter, a stored property. One spec building a small isolated graph was enough to make every other spec's fixture.koin.get<T>() a false KOIN-D002.

val clock = TestFixture().koin.get<Clock>()   // Clock is provided by the fixture's own graph
// before: e: KOIN-D002 Missing definition: Clock
// after:  w: KOIN-W009 1 call site(s) not validated at compile time (first: get<Clock>()) …

Every call site is still judged exactly as before, except:

  • a provably foreign Koin: a function parameter, a lateinit var, a property with a custom getter or by lazy, or the result of a non-Koin function (a fixture's koin, an overridden getKoin()). Seen through val, casts and !!; a plain val is followed to its initializer, so val appKoin = startKoin { }.koin stays judged.
  • global resolvers (KoinComponent, koinInject, koinViewModel, Android, Ktor) when every entry point is a plain koinApplication { } (a test fixture or a preview). koinConfiguration { } still counts as an app root (compileSafety: Checks are bypassed when using KoinApplication Composable in Compose Multiplatform project #38).

A skipped call site that would have been KOIN-D002 is neither an error nor a hint: one new KOIN-W009 per compilation counts them. Any shape not recognised keeps the old behavior, so the worst case is an old false D002, never a lost error.

#121 — activityViewModel() and parametersOf

activityViewModel<T>() reuses the instance the activity created with its own parametersOf. It no longer gets KOIN-D006:

  • a call site in the same module creates T with parametersOf → nothing reported;
  • none does → new KOIN-W010 warning. The creator is often in another Gradle module (activity in :app, fragment in :feature), so this can't be an error.

KOIN-D005 (wrong parametersOf) and the missing-type check are unchanged.

⚠️ Behavior changes (release-noted)

  • fun f(koin: Koin) = koin.get<X>() in an app module, with X defined nowhere, now gives KOIN-W009 instead of KOIN-D002.
  • Two new warnings, KOIN-W009 and KOIN-W010; like the other KOIN-W codes they fail builds using allWarningsAsErrors.
  • No existing golden file changed.

Known limit (documented)

A judged call site is still checked against every graph of the compilation, merged: with two koinApplication { } blocks, a.koin.get<T>() passes when only the other one provides T. Unchanged from before.

Tests

  • 20 new diagnostics / cross-module tests: the false-error shapes (failed before the fix) and guards for every shape that must keep its D002 (getKoin() in a KoinComponent or a definition lambda, koinApplication { }.koin, val x = startKoin { }.koin, koinConfiguration, Android getKoin(), activityViewModel of an undefined type).
  • Full suite: 509 tests green.
  • test-apps: compileKotlinIosArm64, compileKotlinWasmJs, compileKotlinJs, jvmTest green.
  • nowinandroid (DSL migration): :app main, unit-test and androidTest compiles green; removing a definition still fails with KOIN-D002 at its by inject() call site.

Docs: docs/COMPILE_TIME_SAFETY.md gains "Which call sites are judged" and the activityViewModel() rule. Release notes: RELEASE_NOTES_1.2.2.md.

arnaudgiuliani and others added 2 commits October 30, 2026 18:05
A `get<T>()` was judged against whatever koinApplication/startKoin the
compilation declared, even on a Koin built elsewhere: a shared test
fixture's instance, a parameter, a stored property. That gave false
KOIN-D002, mostly in test source sets (#58).

Every call site is still judged exactly as before, except:
- Koin.get/inject/Scope.get on a Koin that is provably foreign: a function
  parameter, a lateinit var, a property with a custom getter or by lazy, or
  a non-Koin function's result (an overridden getKoin(), a fixture's koin).
  Seen through val, casts and !!; a plain val is followed to its
  initializer, so `val appKoin = startKoin { }.koin` stays judged.
- global resolvers (KoinComponent, Compose, Android, Ktor), when every entry
  point is a plain koinApplication { } (a test fixture or a preview).

Such a call site that would have been KOIN-D002 is neither an error nor a
hint; one KOIN-W009 per compilation counts them. An unrecognised shape keeps
the old behavior: at worst an old false D002, never a lost error.

Behavior change: `fun f(koin: Koin) = koin.get<X>()` in an app module, with
X defined nowhere, now gives KOIN-W009 instead of KOIN-D002.

Known limit: a judged call site is still checked against every graph of the
compilation, merged (documented).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ty's instance (#121)

activityViewModel / getActivityViewModel / koinActivityViewModel return the
ViewModel the activity created with its own parametersOf, so a missing
parametersOf there is not an error:
- a call site in the same compilation creates that ViewModel with
  parametersOf → nothing reported;
- none does → new KOIN-W010 warning. The creator is often in another Gradle
  module (activity in :app, fragment in :feature), so this can't be D006; if
  nothing creates it first, this call creates it without parameters and
  fails at runtime.
KOIN-D005 (wrong parametersOf) and the missing-type check are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@arnaudgiuliani
arnaudgiuliani merged commit c4e54be into 1.2.2 Nov 2, 2026
2 checks passed
@arnaudgiuliani
arnaudgiuliani deleted the arnaud/ktz-5224-unknown-graph-warning branch November 2, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant