Sub topic 4
What the .tsbuildinfo file stores and where it lives
This chapter assumes you can read a tsconfig.json, run tsc -p . from a terminal, and know that the compiler has a parse, bind, check and emit pipeline. You should also have read the previous chapter, which turned on the incremental option. Here you look at what incremental writes to disk, how tsc uses it, and how to control where it goes.
What the file is
When incremental or composite is on, tsc writes a project state file after each run. The file has a .tsbuildinfo extension and contains JSON. On the next run, tsc reads it first. It compares the current files against what was recorded and does work only for the files that differ.
The file is a cache, not a build output you ship. You can delete it at any time. The only cost is that the next run behaves like a first run.
What it stores
The exact layout is an internal detail and changes between TypeScript releases. The categories of data are stable enough to reason about:
| Data | What it is used for |
|---|---|
| Compiler version | A file written by a different TypeScript version is ignored. |
| Compiler options | Options that affect output or checking. If they change, the cached state is not trusted. |
| File list | Every source file and library file in the program, including `lib.*.d.ts` files. |
| File version | A hash of each file's text, used to detect edits. |
| File signature | A hash of each file's exported API shape, used to decide whether dependents need rechecking. |
| Reference map | Which file imports or references which. This is the dependency graph. |
| Cached diagnostics | The semantic errors found for each file, so unchanged files do not need to be rechecked to report them. |
| Pending emit | Files that were checked but not yet written, for example after a `--noEmit` run or a failed emit. |
The file version and the file signature do different jobs, and the second one saves most of the time.
The version answers "did the text change?" It changes on any edit, including a comment.
The signature answers "did what other files can see change?" It is derived from the file's declaration output. Renaming a local variable inside a function body changes the version but not the signature.
An abbreviated example
This excerpt shows the shape of the data. A real file is larger and its field names differ between versions, so treat it as an illustration and not a format to parse.
{
"fileNames": ["../node_modules/typescript/lib/lib.es2022.d.ts", "../src/math.ts", "../src/app.ts"],
"fileInfos": [
{ "version": "a1f3...", "signature": "a1f3..." },
{ "version": "9c2e...", "signature": "77b0..." },
{ "version": "5d41...", "signature": "5d41..." }
],
"referencedMap": { "../src/app.ts": ["../src/math.ts"] },
"semanticDiagnosticsPerFile": [1, 2],
"options": { "strict": true, "outDir": "../dist", "incremental": true },
"version": "5.5.4"
}Two details are worth noticing. File paths are stored relative to the .tsbuildinfo file, which is why a cached copy keeps working when the repository sits at a different absolute path on another machine. The referencedMap is what lets tsc find, for a changed file, only the files that depend on it.
How tsc uses it
On startup tsc loads the file and hashes the current source files. It then follows these steps:
- It compares each file's current hash with the stored version.
- It re-parses and re-checks the changed files.
- For each changed file it computes a new signature and compares it with the stored one.
- If the signature is unchanged, dependents are left alone. If it changed, the files listed as dependents in the reference map are queued for rechecking, and so on through their dependents.
- It emits only the files that changed or became affected, then writes an updated
.tsbuildinfo.
This explains a pattern you will see when you measure. Editing a function body in a widely imported file is cheap. Adding or changing an exported type in that same file can trigger a recheck of many files.
Where it lives
You can set the location yourself, or let tsc pick one. The rules, in the order tsc applies them, are:
- If
tsBuildInfoFileis set, that path is used. - If
outFileis set, the file sits next to the bundle, named after it. - Otherwise the file is named after the config file, with the
.tsbuildinfoextension. Fortsconfig.jsonthat givestsconfig.tsbuildinfo. It goes insideoutDirwhen one is set, and beside the config file when not.
So a project with "outDir": "./dist" and no explicit path typically ends up with dist/tsconfig.tsbuildinfo.
The tsBuildInfoFile option is only valid together with incremental or composite. Setting it alone is an error. This is the configuration from the topic plan:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"outDir": "./dist",
"incremental": true,
"tsBuildInfoFile": "./.cache/app.tsbuildinfo"
},
"include": ["src"]
}Here the state goes to .cache/app.tsbuildinfo, outside the output folder.
Why you might move it
- Clean builds. Many projects delete
distbefore a build. If the buildinfo lives indist, it is deleted too. Thentschas no record of what it emitted and rebuilds everything. Worse, a buildinfo that survives while its outputs are deleted can maketscbelieve those outputs still exist and skip writing them. Keeping the two together, or deleting both, avoids that. - Several configs in one folder. Two configs such as
tsconfig.jsonandtsconfig.test.jsonproduce different default names, so they do not clash. If two configs both write to the same explicit path, each run overwrites the other's state and neither benefits. - Cache directories. CI systems cache specific folders. A dedicated path like
.cache/is easier to save and restore than a file buried insidedist.
Seeing it work
Run the build twice with extended diagnostics, as in the first chapters, and compare the numbers:
tsc -p . --extendedDiagnostics
# the first run creates .cache/app.tsbuildinfo
ls -l .cache/app.tsbuildinfo
# the second run reads it: check time should drop
tsc -p . --extendedDiagnosticsOn the second run, look at the "Check time" and "I/O Read" lines. If nothing changed, tsc should report close to zero check time and write few or no files. Now change only a comment in one file and run again. The file is re-parsed and re-checked, but its signature is unchanged, so its dependents are not.
The next step is to export a new function from a shared module and run again. The signature changes, so the importing files appear in the recheck.
Edge cases and gotchas
- Version changes invalidate the file. Upgrading TypeScript means the first run after the upgrade is a full build. Cached files from an older compiler are not reused.
- Option changes invalidate it too. Switching
strict,target, ormoduleforces rechecking. A build matrix that runs the same config with different flags on a shared path will keep discarding the cache. - Edits to
node_modulestypes. Files undernode_modulesare tracked by hash like any other. Updating a dependency that ships.d.tsfiles changes those hashes and can ripple into your own files. - Timestamps versus hashes. Inside one project,
tscrelies on content hashes, so a fresh checkout with new modification times does not by itself defeat the cache. Build mode (tsc --build) additionally compares input and output timestamps to decide whether a whole project is up to date. After a fresh checkout, sources look newer than cached outputs, so a project may be treated as out of date. It then runs and uses the.tsbuildinfoto avoid most of the work. The later CI chapter returns to this. --noEmitis allowed. Withincremental, a--noEmitrun still writes the buildinfo and records which files are pending emit. A later run that does emit will write them.- Corrupt or hand-edited files. If you suspect stale state, delete the file and rebuild. Do not edit it by hand.
Summary of the decisions
Keep the default location when you have one config and a plain outDir. Set tsBuildInfoFile when you build several configs from one folder, when you clean the output directory often, or when you want a single folder to cache in CI. Never rely on the file's internal fields in scripts, because the format is not a public contract.
Exercise
A project sets "incremental": true and "outDir": "dist", with no tsBuildInfoFile. A script runs rm -rf dist before every tsc -p .. Builds are not faster than before incremental was enabled. Why, and what are two ways to fix it?
Show answer
By default the buildinfo is written inside dist (for example dist/tsconfig.tsbuildinfo), so rm -rf dist deletes it every time and tsc always starts from scratch. Fix it by setting tsBuildInfoFile to a path outside dist, such as ./.cache/app.tsbuildinfo, so the state survives, or by no longer deleting dist before each build. If the cache survives while dist is wiped, remember that tsc may skip emitting files it believes it already wrote, so delete the buildinfo together with the outputs whenever you want a clean rebuild.
Exercise
You edit only a comment inside a function body in a file that 40 other files import. Its exported types are unchanged. Which files does tsc recheck on the next incremental run, and which stored value tells it so?
Show answer
Only the edited file. Its version hash changed, so it is re-parsed and rechecked, but its recomputed signature (the hash of its exported API) matches the stored one. Because the signature is unchanged, the reference map's 40 dependents are not rechecked.