Part 11 / Topic 8 of 9
A real mistake: reusing one output directory
This genuinely happened while verifying this part, and it's worth seeing on purpose rather than
discovering it by accident. Compiling src/main/kotlin and src/test/kotlin into the same
output directory looks harmless — the .class files are still all there afterward — but it
silently corrupts the metadata Kotlin uses to resolve top-level functions across a classpath
boundary (the META-INF/*.kotlin_module file each kotlinc invocation writes gets
overwritten, not merged, by whichever compile ran last):
$ kotlinc src/main/kotlin -d build/classes/broken
$ kotlinc -cp build/classes/broken ... DiscountCalculatorTest.kt -d build/classes/broken # succeeds!
$ kotlinc -cp build/classes/broken ... DiscountCalculatorParamTest.kt -d build/classes/broken
src/test/kotlin/com/stepshala/billing/DiscountCalculatorParamTest.kt:20:39: error: unresolved reference 'membershipDiscountPercent'.
assertEquals(expectedPercent, membershipDiscountPercent(tier), 0.001)
^^^^^^^^^^^^^^^^^^^^^^^^^
The first test file compiled fine — it read the still-intact metadata left by the main
compile, then overwrote it with metadata describing only itself. The second test file then
couldn't see main's functions at all, even though DiscountCalculator.kt's actual .class
file was sitting right there in the directory the whole time. This is exactly why section 7
uses build/classes/main and build/classes/test as two separate directories — which is also
precisely what Gradle (and every other real build tool) does by default, for this reason.
Done here? The practice set for Part 11 — including the solutions — is on the part page.