The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S44 · V3·43

A Build That Knows Its Inputs

Volume 3, chapter 43. TiffinBox's build was four shell scripts run in order: 75 seconds every time, whatever changed. Copied into Gradle for comparison, its contract test stayed UP-TO-DATE after the contract file it checks was broken, because the file came in as a path in a system property; Gradle refused LangLint passed as a plain compiler argument for the same reason. This stage adds one build entry point that skips each module whose inputs haven't changed and says which input changed when it doesn't, finds the files the tests read by tracing them, pins the JDK, puts the commit in the version, keeps core's public API in a committed file (which would have flagged stage 42's record change), and turns LangLint's rules into a registry that every rule's tests and every suppression are checked against.

6 parts · ~5 min to read
Updated Suggest a correction

Stage 44: a build that knows its inputs

The brief

What you'll build.

  • tools/build.sh: builds tools/lint, core, tools/tbx and server in order. A module is skipped when its inputs hash the same as at its last successful build: every file in its directory except out/, the jars of the modules it uses, the JDK, the files outside it that its tests read, and, for the server, the commit. When it builds, it prints which inputs changed. --force builds everything; --trace re-traces the tests' file reads and fails on any undeclared one; --accept-api accepts a change to core's API.
  • .java-version: the JDK releases are built with; tools/build.sh refuses another unless TB_ANY_JDK=1.
  • Build info: the server's version is 0.3.0-dev+<commit> (with -dirty for uncommitted changes), shown by /version and the page footer; the golden master normalises it.
  • tools/ApiDump.java and core/api.txt: core's public types, members and record components, one per line; the build fails when they differ from the committed file.
  • LangLint's rule registry: LangLint.RULES (name and reason); warn refuses an unregistered rule; test.sh checks every rule has a test case; check-suppressions.sh checks every @SuppressWarnings("langlint:…") names a rule.
  • Release check: its first step is now tools/build.sh --force --trace.
  • Measurements in exp/44: TiffinBox in Gradle (build.gradle, gradle-incr.sh, gradle-test-inputs.sh), build-scenarios.sh, api-41-to-42.out.

Why a real company needs this. A slow build gets run less, and a fast build that's sometimes wrong is worse than a slow one. Most stale results come from inputs nobody declared. Knowing a build's inputs is also what makes it explainable ("why did this rebuild?"), cacheable and provable.

Why now. Chapter 43 is build systems: what a build does, Maven, Gradle, caching, hermetic tasks and reproducible builds. Stage 43 made the jars reproducible; this stage uses that to skip work safely.

What you'll have at the end.

  • a no-change build in under a second instead of 75;
  • every input of every module named, and the tests' file reads checked against them;
  • ADR-0046: how TiffinBox builds.

Theory to own

  1. Inputs. What must a step's key include for skipping it to be safe? (The chapter, "Build caching and incremental builds"; Companion, Gaps 1 and Theory 1.)
  2. Early cutoff. Why does a reproducible jar make its dependents' builds cheaper? (Companion, Gaps 3 and Theory 2.)
  3. Compiler plugins. What does changing a lint rule cost a build that tracks inputs correctly? (Companion, Gaps 2.)
  4. API as a file. What does a committed API dump catch that compiling doesn't? (Companion, Gaps 4.)

Build it

Step 1: measure the build you have

Time the four module scripts, the way the release check ran them, three times with no changes in between.

Checkpoint. tools/build.sh --force prints built 4, up to date 0, in 75 s.

Step 2: TiffinBox in Gradle

Write exp/44/build.gradle for core and server (offline, the java plugin only). Pass LangLint the way server/build.sh does, as compilerArgs; then as annotationProcessorPath. Run gradle-incr.sh (no change, a method body, a public method, a changed lint jar) and gradle-test-inputs.sh (break contract/api.json, then declare it).

Checkpoint. exp/44/gradle-processorpath.out, gradle-incr.out, gradle-test-inputs.out.

Step 3: skip what didn't change

Write tools/build.sh. Hash each module's inputs into out/.inputs after a successful build; skip when the hashes match; print the names of the inputs that differ; keep a .failed marker so a failed build never counts as up to date.

Checkpoint. exp/44/build-scenarios.out.

Step 4: find what the tests read

Run each module's tests under strace -f -e trace=openat and list the files they open outside the JDK, /tmp and their own out/. Put the ones outside the module directory into EXTRA, and make tools/build.sh --trace fail on any read that isn't declared.

Checkpoint. --trace prints the reads; the release check runs it.

Step 5: core's API in a file

Write tools/ApiDump.java (load core's jar in its own class loader; list public and protected types, fields, constructors, methods and record components with generic types; sort). Accept the current API into core/api.txt. Then run it with stage 41's API as the committed file against stage 42's core, and try adding a public method.

Checkpoint. exp/44/api-41-to-42.out; core's API: unchanged (524 lines).

Step 6: the toolchain, the commit and the rules

Add .java-version and the check; the commit in the server's version (and in tools/reproduce.sh, which now reads the version from the built jar); a normalisation in the golden master (case-insensitive, and with normalised byte counts, since -dirty changes the page's length). Then add LangLint's RULES, the registry checks, and check-suppressions.sh.

Checkpoint. langlint: 7 rules, each with a test case; suppressions: 10, each naming a rule LangLint has; golden master: 47 exchanges, identical.

Senior and architect lens

1. A build is a function of declared inputs, and it can explain itself. "Why did this rebuild?" now has an answer in the build's output. "Why didn't it?" has a check (--trace).

2. ADR-0046: how TiffinBox builds. Write it with these points:

  • One entry point: tools/build.sh; the module scripts are its steps.
  • Inputs: module directory, upstream jars, JDK, extra files the tests read (checked by tracing), the commit for the server.
  • Skipping: by content hash, which relies on reproducible jars (ADR-0045).
  • Toolchain: pinned in .java-version; other JDKs only through the JDK matrix.
  • API: core/api.txt is reviewed like code; a change needs --accept-api and a note for the next release.
  • Analysis: LangLint's rules are a registry, each with tests; suppressions must name a rule.
  • Not adopting Gradle or Maven, yet: no repository access in this environment and four modules; the measurements in exp/44 show what they'd give (compile avoidance by ABI) and what they'd still need from us (declared test inputs).

3. Correct first, then fast. Every skip is a claim that nothing relevant changed. Make the claim checkable before making it common.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
the server's 62 s of tests on every server change slow feedback stage 55 (backend testing: test selection and faster tests)
new extension tags in new modules the tag-number check only covers listed tags stage 50 (persistence: a schema and tag registry)
lock-order and call-graph checks (promised for this stage) still by review stage 55 (backend testing: lock-order and call-graph checks with the stress tests)
a damaged log repaired by hand operators editing bytes stage 50 (persistence: tbx log repair, with a backup)
images built on developers' machines; core published for others platform differences, copies of core Volume 8's stages (CI/CD and release engineering)

Evidence

  • exp/44/gradle-processorpath.out, gradle-incr.out, gradle-test-inputs.out.
  • exp/44/build-scenarios.out, api-41-to-42.out.
  • tools/build.sh, .java-version, tools/ApiDump.java, core/api.txt, the LangLint registry and check-suppressions.sh.
  • Core 52, server 48 and tbx 21 tests; golden master: 47 exchanges, identical; the release check passes, starting with tools/build.sh --force --trace.
  • ADR-0046 is committed.

Carry forward

  • tools/build.sh for every build; --trace whenever tests start reading new files.
  • core/api.txt reviewed with every change to core.
  • New LangLint rules go into RULES with a test case.