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 --- CLAUDE.md | 48 +++++++++++++++++++-------- README.md | 68 +++++++++++++++++++++------------------ test_project/app/build.gradle.kts | 35 +++++++++----------- 3 files changed, 87 insertions(+), 64 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 63fa5d8..bdaa44c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -163,19 +163,41 @@ re-verifying, and `rm /tmp/kotlin-proxy-marker.txt` for a fresh marker. logging+null (the daemon dispatches through bridges). - **Classpath shadowing (the subtle part):** KGP ≥ 2.3.20 runs JVM compilation through the **Build Tools API** 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). Like any `defaultDependencies`-seeded - configuration, adding *any* regular dependency suppresses the defaults — so - `test_project/app/build.gradle.kts` re-declares the stub jar **first** plus - those two modules as regular deps, and the daemon loads `K2JVMCompiler` - from the stub. The older `kotlinCompilerClasspath` (legacy - `runViaBuildToolsApi=false` path, deprecated, removed in KGP 2.5) is shadowed - the same way for completeness. The root `build.gradle.kts` also puts the stub - first on the *buildscript* classpath, and pins KGP explicitly — AGP 9.x's - built-in Kotlin would otherwise apply its own pinned KGP version (AGP 9.0 - release notes, "Runtime dependency on Kotlin Gradle plugin"). - All intentional. + the resolved **`kotlinBuildToolsApiClasspath`** configuration (KGP seeds it + with *regular* deps `kotlin-build-tools-compat` + `kotlin-build-tools-impl`, + whose transitive closure *is* the compiler — verified on KGP 2.4.21, where + `config.dependencies` shows exactly those two plus our file dep; the old + `defaultDependencies` story is stale, and Gradle 9 removed + `defaultDependencies` from `Configuration` entirely). + `test_project/app/build.gradle.kts` therefore adds **only** the stub jar as + a `files(...)` dep (in `afterEvaluate`); Gradle resolves **file-collection + deps before module deps**, so the stub lands FIRST in the resolved file + order (observed on the daemon's `--daemon-compilerClasspath`) and the daemon + loads `K2JVMCompiler` from the stub — no re-declaration of compat/impl. + Do NOT resolve the configuration at configuration time (no + `Configuration.files` / `.incoming` access in `afterEvaluate` or earlier): + AGP's `DependencyResolutionChecks` raises "Configuration + 'kotlinBuildToolsApiClasspath' was resolved during configuration time". + Declaring a dependency does not resolve — resolution happens at execution + time when the compile task runs. + The legacy `kotlinCompilerClasspath` (`runViaBuildToolsApi=false` path, + removed in KGP 2.5) carries no deps in this setup and is left alone. + The root `build.gradle.kts` also puts the stub first on the *buildscript* + classpath, and pins KGP explicitly — AGP 9.x's built-in Kotlin would + otherwise apply its own pinned KGP version (AGP 9.0 release notes, "Runtime + dependency on Kotlin Gradle plugin"). All intentional. +- **Why not a `-javaagent` instead (explored, not adopted):** the daemon JVM + args channel works in this flow — the Gradle property + **`kotlin.daemon.jvmargs`** (NOT the legacy `kotlin.daemon.jvm.options`) + reaches the daemon's command line, so a `-javaagent` jar is loaded in the + daemon and its `ClassFileTransformer` successfully swaps the + `K2JVMCompiler` trio's bytes. But a transformer is only consulted for class + files the classloader can actually *find* — it replaces, it cannot ADD. + The stubs reference `kotlin.proxy.*` support classes that exist nowhere on + the daemon's classpath, so a swapped `K2JVMCompiler` dies with + `ClassNotFoundException: kotlin.proxy.KotlinProxyLog`. The agent route + would need fully self-contained stubs (the `kotlin.proxy.*` logic inlined + into the trio by `gen_class.py`) before it becomes viable. ## Gotchas 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. diff --git a/test_project/app/build.gradle.kts b/test_project/app/build.gradle.kts index b106e73..b5da6e1 100644 --- a/test_project/app/build.gradle.kts +++ b/test_project/app/build.gradle.kts @@ -41,29 +41,24 @@ dependencies { androidTestImplementation(libs.androidx.junit) } -// ---- kotlin-proxy: force our compiler stub to the FRONT of the daemon classpath ---- -// KGP seeds the daemon classpath with hidden defaultDependencies; adding ANY -// regular dependency suppresses those defaults - so we re-declare the same -// modules as regular deps WITH our stub jar FIRST. The daemon then loads -// org.jetbrains.kotlin.cli.jvm.K2JVMCompiler from OUR stub, replacing the -// real compiler with our alternative. +// kotlin-proxy: shadow the Kotlin compiler in the compile DAEMON by putting +// the stub jar FIRST on its classpath (the kotlinBuildToolsApiClasspath +// configuration, whose resolved file order becomes the daemon's -cp). // -// KGP >= 2.3.20 runs JVM compilation through the Build Tools API by default: -// the daemon classpath is the resolved `kotlinBuildToolsApiClasspath` -// (defaults: kotlin-build-tools-compat + kotlin-build-tools-impl, whose -// closure IS the compiler). -val proxyStub = file("/home/the_jk/source/kotlin_proxy/dist/kotlin-proxy-compiler-stubs.jar") -val kotlinVersion = libs.versions.kotlin.get() +// KGP adds kotlin-build-tools-compat/-impl to this configuration itself, so +// only the stub jar needs to be declared here - first, to win the lookup. +// +// Deliberately NO configuration-time resolution (no Configuration.files / +// .incoming access): AGP's DependencyResolutionChecks raises +// "was resolved during configuration time" for that. Declaring a dependency +// does not resolve; resolution happens at execution time when the compile +// task runs. +val proxyStubJar = file("/home/the_jk/source/kotlin_proxy/dist/kotlin-proxy-compiler-stubs.jar") afterEvaluate { try { - 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")) - logger.lifecycle("kotlin-proxy: AGP stub present=" + proxyStub.exists() + - " BTA ORDER=" + bta.files.joinToString(",") { it.name }) + configurations.getByName("kotlinBuildToolsApiClasspath") + .dependencies.add(dependencies.create(files(proxyStubJar))) + logger.lifecycle("kotlin-proxy: stub jar present=" + proxyStubJar.exists()) } catch (t: Throwable) { logger.lifecycle("kotlin-proxy: AGP injection FAILED: " + t) } -- cgit v1.3.1