Incremental Compilation
Status: 🟢 Skipping whole units, the local build cache, and recompiling only what changed inside a unit all work. ⚪ Nothing is compiled in parallel: every main source set is flattened into one unit, and that unit compiles on one thread.
Why a second kargo build finishes in milliseconds.
Fingerprints
Before compiling a unit, Kargo hashes everything that could change its output:
| Input | Why it matters |
|---|---|
| Source file contents | The obvious one |
| Resolved dependency set | A new library version changes what compiles |
| Compiler arguments | Including profile compiler-args |
| Compiler version | Two Kotlin versions produce different bytecode |
| Target | Same sources, different output |
| Generated inputs | BuildConfig, KSP and KAPT output |
| The output a unit compiles against | The tests compile against the main classes, so editing main recompiles them |
Equal fingerprint, no compile. Different fingerprint, compile.
Fingerprints are stored in .kargo/fingerprints/, outside build/, so a partial
clean does not destroy them. kargo clean removes both.
Note that contents are hashed, not timestamps. Touching a file without changing
it does not trigger a rebuild, and git checkout moving a file back to a previous
state restores a hit rather than a miss.
Resources have a fingerprint of their own, because they change a different output. Nothing under a resources directory reaches the compiler, so editing one rebuilds the jar and leaves the compile alone.
The unit graph
A build is a directed graph of units. Edges mean "must finish first" — a JVM unit
waits for the commonMain unit it depends on.
graph LR
COMMON["commonMain"] --> JVM["jvmMain"]
COMMON --> IOS["iosMain"]
COMMON --> JS["jsMain"]
JVM --> JVMTEST["jvmTest"]
COMMON --> COMMONTEST["commonTest"]
⚪ The graph is the model, not the implementation. Every main source set is flattened into one
mainunit, so there is nothing yet for a graph to order or for a scheduler to overlap.
Two consequences, once it is in place: units with no path between them compile in
parallel, up to KARGO_JOBS; and a change to commonMain invalidates everything
downstream, while a change to jvmMain invalidates only jvmMain and jvmTest.
That asymmetry is worth knowing when a build feels slower than it should. Code
that changes often sits better in a platform source set than in commonMain.
Three levels of avoidance
In order of how often they save you time:
1. Fingerprint hit. Nothing to do. This is the no-op build.
2. Build cache hit. The fingerprint is new to this working tree, but the
output is in the build cache from a previous build —
another branch, a git bisect step, a pre-clean build. Restore instead of
compile.
3. Compile. Actually invoke the compiler, then store the output in the cache keyed by the fingerprint.
Within a unit
Status: 🟢 On by default. A project it cannot serve compiles the way it always did.
Edit one file and Kargo recompiles that file and whatever referenced what changed in it — not the whole source set. On a 400-file project, a one-file edit recompiles one file.
This is the Kotlin compiler's own incremental compilation, which Kargo drives
through the Kotlin Build Tools API. The kotlinc command-line compiler cannot
do it at all, which is why Kargo only falls back to that.
What is left of the wall clock after this is compiler startup, and the daemon is what removes that.
It needs:
- Kotlin 2.4 or later. Earlier versions compile the whole source set.
- A JVM or Android target.
- About 60MB, once per Kotlin version. The API ships as its own artifacts
rather than inside the Kotlin distribution, so the first build on a new
Kotlin version downloads them into the artifact cache.
kargo fetchkeeps them there.
Nothing about it can fail a build. Anything that stops it — an older Kotlin,
jars that will not download, a JDK without javac — compiles the whole source
set instead, which is what every build did before. kargo build -v says which
happened.
To turn it off:
# ~/.kargo/config.toml
[build]
compiler-backend = "cli"
KARGO_COMPILER_BACKEND=cli kargo build # for one build
Correctness over speed
The fingerprint deliberately includes inputs that rarely change, such as the compiler version and the full argument list. Hashing more than strictly necessary costs a few milliseconds and makes a stale hit impossible.
So reaching for kargo clean should never be the fix for a wrong build. If a
clean build differs from an incremental one, that is a bug worth
filing with both -v outputs.
When everything recompiles anyway
| Cause | Fix |
|---|---|
compiler-backend = "cli", or Kotlin older than 2.4 | Expected: the kotlinc launcher has no within-unit incremental compilation. See Within a unit |
.kargo/ deleted between builds | Include it in the CI cache — both fingerprints/ and incremental/ |
kargo clean, or build/ removed by hand | Expected: the next build has no output to be incremental against |
| The build cache restored the output | Expected once; the build after it is incremental again |
| A hook rewriting a source file every run | Make the hook idempotent |
| Generated code containing a timestamp | Remove it — it changes the hash every build |
kargo test after any edit to main | Expected: the test sources are compiled against main's output |
kargo update ran | Expected: the dependency set is an input |
A profile's compiler-args changed | Expected |
| The Kotlin version changed | Expected |
kargo build --timings
kargo build -v # shows which units missed and why