Sub topic 8
How .tsbuildinfo enables incremental builds
This chapter assumes you have the core and app projects from the earlier chapters. core is a composite project, and app references it. You should also be comfortable running tsc and reading tsconfig.json options. Here you will learn what a .tsbuildinfo file stores, how tsc uses it to skip work, and how to keep it from causing stale or surprising builds.
What the file is
A .tsbuildinfo file is a JSON cache that tsc writes after a successful compile of one project. It records what the compiler learned last time, so the next run can ask "what changed?" and not "what exists?".
Setting composite: true turns on incremental, and incremental is the option that writes this file. You can also set incremental: true alone on a standalone project to get the same speedup without references.
The file holds, roughly:
- The list of source files in the program, each with a version, which is a hash of its contents.
- For each file, a signature, which is a hash of the file's public shape (its declaration output).
- The import graph: which file references which.
- The compiler options that affect output.
- Cached diagnostics for files that had errors.
- A list of files whose emit is still pending.
You never edit this file, and its format changes between TypeScript versions. When the compiler version or relevant options differ from what the file records, tsc discards the cache and does a full build of that project.
Where the file goes
The default location depends on outDir, rootDir, and the config file's name. The exact result surprises people. With rootDir: "src" and outDir: "dist", the file can land next to the tsconfig.json and not inside dist. Pin it with tsBuildInfoFile so the location is predictable:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/core.tsbuildinfo"
},
"include": ["src"]
}Putting the file inside outDir means one rule covers cleanup and caching: delete dist and you delete both the outputs and the cache that describes them. That keeps the two from disagreeing. A cache that says "emit is current" while the emitted files are gone is a common source of confusing builds.
Add the outputs to .gitignore:
dist/
*.tsbuildinfoDo not commit .tsbuildinfo. It contains hashes and paths that differ between machines and TypeScript versions, and committing it produces noisy diffs and merge conflicts.
Two levels of "up to date"
Incremental work happens at two levels, and it helps to keep them separate.
- Between projects,
tsc --builddecides whether a project needs to be built at all. - Inside a project, the
.tsbuildinfodecides which files need rechecking and re-emitting.
At the first level, build mode compares file timestamps. It looks at the newest input (source files, the tsconfig.json, and the declaration outputs of referenced projects) and the oldest output. If every output is newer than every input, the project is up to date and is skipped.
At the second level, the project is out of date, so tsc loads the .tsbuildinfo, hashes the current source files, and compares them to the recorded versions. Only changed files, and files affected by those changes, are rechecked and re-emitted.
Signatures decide how far a change spreads
The signature is the important part. A file's signature is a hash of what other files can see: its exported types, function signatures, and so on. Function bodies, local variables, and comments do not contribute.
That gives two kinds of edit:
- An implementation-only edit changes the file's version but not its signature. Only that file is re-emitted. Files that import it keep their cached results.
- A signature edit changes the signature. Every file that depends on it must be rechecked, because the types they rely on moved.
Across a project boundary, the same logic applies to declaration files. If core's emitted .d.ts files come out byte-identical after an implementation-only edit, app has nothing new to check. In recent TypeScript versions, build mode notices that the upstream declarations did not change. It then only touches the timestamps of app's outputs and does not recompile.
Watching it work
Start from a clean, built state, then try both kinds of edit. Run these from the repository root:
# First build: everything is compiled and .tsbuildinfo files are written
tsc --build --verbose
# Second build with no edits: both projects reported up to date
tsc --build --verboseNow change only the body of greet in packages/core/src/greet.ts:
export function greet(name: string): string {
return `Hello, ${name}.`;
}tsc --build --verbosecore is out of date and rebuilds. Its .d.ts output is unchanged, so app is reported as up to date, perhaps with a note that only its output timestamps were updated.
Next, change the signature by adding an optional parameter:
export function greet(name: string, punctuation = "!"): string {
return `Hello, ${name}${punctuation}`;
}tsc --build --verboseThis time core's declaration output differs, so app is checked against the new declarations. The code still compiles because the new parameter is optional. If you had made it required, app would fail at main.ts with an error about the missing argument.
The --dry --verbose combination from the build mode chapter prints the same decisions without compiling anything. It is the quickest way to ask why a project is or is not being rebuilt.
Failure modes and edge cases
Stale cache after changing tools. The .tsbuildinfo records the compiler version and relevant options. Upgrading TypeScript normally invalidates it automatically. If you suspect corruption, such as a build that reports no errors while you can see one in your editor, delete the file or run tsc --build --force.
Outputs deleted but cache kept. If you delete dist/*.js but keep a .tsbuildinfo stored outside dist, an incremental build can believe the emit is done. Keep both in outDir, as shown above, or use tsc --build --clean, which removes the outputs and the build info that build mode knows about.
Changed files with unchanged contents. Versions are content hashes, so touching a file or switching branches back and forth does not force rechecking of files whose text is identical. Timestamps still cause the project-level out-of-date decision, but the file-level work stays small.
Shared build info. Each project needs its own .tsbuildinfo path. Two projects that point tsBuildInfoFile at the same file overwrite each other and cause full rebuilds on every run.
noEmit and builds. A composite project must emit declarations, and build mode needs emitted output to judge freshness. Setting noEmit on a referenced project defeats the mechanism. Use a separate, non-composite config for type-only checks.
Summary of what each piece does
| Item | What it stores or decides | Scope |
|---|---|---|
| Timestamps | Whether a project is skipped entirely | Between projects |
| File versions | Which files changed on disk | Inside a project |
| Signatures | Whether changes spread to importers | Inside and across projects |
| `.tsbuildinfo` | Holds versions, signatures, graph, diagnostics | One per project |
| `--force` / `--clean` | Ignores or removes cached state | Whole build |
Exercise
You edit only a comment and a local variable name inside a function body in packages/core/src/greet.ts, then run tsc --build --verbose. Which project is recompiled, and why is app not rechecked?
Show answer
core is recompiled, because its source file changed and its outputs are now older than that input. The change does not alter the signature of the file, since signatures cover only the exported shape. The .d.ts output of core is identical, so app has no new declarations to check. Build mode treats app as up to date and, at most, updates the timestamps of its outputs.
Exercise
Two projects in the same repository both set tsBuildInfoFile to ./build.tsbuildinfo. What goes wrong, and what is the fix?
Show answer
Each build overwrites the other project's cache. The recorded versions and signatures belong to the wrong project, so incremental data is discarded and both projects are fully rebuilt on every run. The fix is to give each project its own path, preferably inside its own outDir, for example dist/core.tsbuildinfo and dist/app.tsbuildinfo.