andyg
06/10/2026, 12:21 AMMultiHostInjector API, but they no longer work with changes to that API for the K2 compiler, which is now the default while K1 is deprecated (https://github.com/casid/jte-intellij/issues/49). I (no prior experience with any of the Injector APIs) am attempting to migrate to `LanguageInjectionContributor`/`LanguageInjectionPerformer` to restore the Kotlin developer experience we previously enjoyed, on the K2 compiler. The migration itself was smooth but we've hit a KaModule association problem that appears regardless of which injection API is used.
What we've tried and where each approach breaks:
1. MODULE_ROOT_TYPE_KEY (K1 approach): inert in K2, no effect.
2. resolveExtensionFileModule on the VirtualFile: getKaModule() returns the correct module, but IDE features (semantic highlighter, intentions) that analyze PSI elements from the injected file throw KaBaseIllegalPsiException: The element cannot be analyzed in the context of the current session — a session mismatch between where the PSI was created and where the analysis session was opened.
3. ktFile.contextModule = kaModule (@KaExperimentalApi): throws IllegalArgumentException: 'contextModule' cannot be set for code fragments — because for a non-Kotlin host file, the Kotlin injection machinery always creates a KtCodeFragment rather than a KtFile, regardless of the "kt" extension hint passed to startInjecting.
4. KtCodeFragment.context: constructor-only val, cannot be set post-construction, so we can't provide a context element after the platform has already created the fragment.
The core question: For a non-Kotlin host file, the platform appears to always produce a KtCodeFragment for injected Kotlin. Is there a supported K2 mechanism for associating that fragment with the correct KaModule so that IDE features can analyze its PSI elements? Or is there a way to make the platform produce a KtFile instead of a KtCodeFragment for this case?
We have a YouTrack reference KTIJ-32613 which appears related. Happy to file a new ticket if this is a known gap.bod
06/10/2026, 8:00 AMandyg
06/11/2026, 1:22 AM