From 854788d8824fceaf0ce45045a56150e0bc92f2ee Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 16 Nov 2026 20:56:28 +0100 Subject: Cleanup stub file injection --- README.md | 68 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 31 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 0a6e7d5..38df3c6 100644 --- a/README.md +++ b/README.md @@ -250,38 +250,43 @@ the jars from `jars/defs.txt` on `javac`'s classpath if you import them. The daemon is a separate JVM. Since **KGP ≥ 2.3.20** it runs JVM compilation through the **Build Tools API (BTA) by default**, and the daemon's JVM -classpath is the resolved **`kotlinBuildToolsApiClasspath`** configuration -(default deps: `kotlin-build-tools-compat` + `kotlin-build-tools-impl`, whose -transitive closure *is* the compiler — 15 jars at 2.4.21). Like any -`defaultDependencies`-seeded configuration, adding **any** regular dependency -suppresses the defaults, so re-declare the same modules as regular -dependencies **with the stub jar first** — the daemon then loads -`K2JVMCompiler` from the stub: +classpath is the resolved **`kotlinBuildToolsApiClasspath`** configuration. +KGP itself seeds that configuration with the *regular* deps +`kotlin-build-tools-compat` + `kotlin-build-tools-impl` (whose transitive +closure *is* the compiler — 14 jars after the stub at 2.4.21; verified on +2.4.21 via `configurations.getByName(…).dependencies` at execution time). +There is nothing to re-declare: add **only** the stub jar, as a `files(...)` +dependency, and it wins the ordering — Gradle resolves **file-collection +deps before module deps**, so the stub lands FIRST in the resolved file +order (checkable on the daemon's `--daemon-compilerClasspath` line in +`/tmp/kotlin-daemon.*.log`) and the daemon loads `K2JVMCompiler` from the +stub: ```kotlin // app/build.gradle.kts -val proxyStub = file("/home/the_jk/source/kotlin_proxy/dist/kotlin-proxy-compiler-stubs.jar") -val kotlinVersion = "2.4.21" // from libs.versions.toml in the test project +val proxyStubJar = file("/home/the_jk/source/kotlin_proxy/dist/kotlin-proxy-compiler-stubs.jar") afterEvaluate { - // BTA path (the default since KGP 2.3.20): the daemon classpath - val bta = configurations.getByName("kotlinBuildToolsApiClasspath") - bta.dependencies.add(dependencies.create(files(proxyStub))) - bta.dependencies.add(dependencies.create( - "org.jetbrains.kotlin:kotlin-build-tools-compat:$kotlinVersion")) - bta.dependencies.add(dependencies.create( - "org.jetbrains.kotlin:kotlin-build-tools-impl:$kotlinVersion")) - // legacy path (kotlin.compiler.runViaBuildToolsApi=false; deprecated, - // removed in KGP 2.5.0) — shadow it the same way, with the full - // kotlinCompilerClasspath default closure re-declared + // BTA path (the default since KGP 2.3.20): the daemon classpath. + // KGP already adds kotlin-build-tools-compat/-impl here as regular + // deps; the stub jar as a files() dep sorts first in the resolved + // file order. + configurations.getByName("kotlinBuildToolsApiClasspath") + .dependencies.add(dependencies.create(files(proxyStubJar))) } ``` -The legacy `kotlinCompilerClasspath` (what the daemon used before 2.3.20) -still exists; its closure is the classic `kotlin-compiler-embeddable` + -transitives set (`kotlin-stdlib`, `kotlin-script-runtime`, -`kotlin-reflect:1.6.10`, `kotlin-daemon-embeddable`, -`kotlinx-coroutines-core-jvm:1.8.0`, `org.jetbrains:annotations:13.0`) and the -test project shadows it too. +Two rules keep this clean: + +- **No configuration-time resolution.** Do not touch `Configuration.files` / + `.incoming` (or anything that resolves) during configuration — not even in + `afterEvaluate`. AGP's `DependencyResolutionChecks` fails the build with + "Configuration 'kotlinBuildToolsApiClasspath' was resolved during + configuration time" for exactly that; declaring a dependency does not + resolve, and resolution happens at execution time when the compile task + runs. +- The legacy `kotlinCompilerClasspath` (`kotlin.compiler.runViaBuildToolsApi=false`, + removed in KGP 2.5.0) carries no dependencies in this setup and needs no + shadowing. Two more pins, both in the root `build.gradle.kts` buildscript: the stub jar is declared **before** AGP/KGP on the buildscript classpath, and KGP is @@ -298,12 +303,13 @@ kotlin.compiler.execution.strategy=daemon kotlin.daemon.useFallbackStrategy=false ``` -Expected outcome: `:app:compileDebugKotlin` **fails** (under BTA: "Internal -compiler error" — the stub returns defaults/null, so the real compiler -machinery fails after calling into the stub) — that's the point. The stub is -the only compiler; the build goes green only once the stub implements its own -compile. (A *green* build means the stub is not being loaded — that's a -failure state, not success.) The daemon's full argument dump ("Starting +Expected outcome depends on the overrides (see CLAUDE.md, Testing): with no +overrides the build **fails** (under BTA: "Internal compiler error" — the stub +returns defaults/null, so the real compiler machinery fails after calling +into the stub); with the current `doExecutePhased` implementation (which +drives a native-image kotlinc) it is **green**. In either case the marker +file is the proof the stub — not the real compiler — ran: the build outcome +alone can't tell them apart. The daemon's full argument dump ("Starting compilation with args: …") is in `/tmp/kotlin-daemon.*.log`; stale daemons hold stale classpaths, so kill them (`pkill -f 'KotlinCompile[D]aemon'`) and `rm /tmp/kotlin-proxy-marker.txt` when re-verifying. -- cgit v1.3.1