Sub topic 7
Building project graphs with tsc --build
This chapter assumes you can read a tsconfig.json, know that composite: true turns on incremental builds and project references, and have a monorepo or multi-package layout in mind. The previous chapter showed how to declare references. This one covers how tsc --build (build mode) uses them: what it does, how it decides what to skip, and what goes wrong.
Why build mode exists
tsc -p packages/app compiles one project. If app references core, the compiler does not build core for you. It reads core's declaration output (.d.ts files) and fails if that output is missing or stale.
tsc --build (short form tsc -b) works on a graph of projects instead:
- It reads the starting project's
referencesand follows them recursively. - It sorts the projects so every project is built after the projects it references.
- For each project, it decides whether the project is up to date. It builds the project only if it is not.
Because a consumer reads its dependencies' .d.ts files, tsc does not re-check dependency source on every run. That is the saving. Combined with the per-project .tsbuildinfo from the incremental option, an edit in one package costs roughly the check time of that package plus any dependents whose inputs changed.
A worked layout
This layout has three packages and a root "solution" config:
tsconfig.json
packages/
core/ tsconfig.json src/index.ts
utils/ tsconfig.json src/index.ts
app/ tsconfig.json src/index.tsThe root config compiles nothing itself. Its empty files array makes it a pure entry point, so tsc -b at the repository root builds everything.
{
"files": [],
"references": [
{ "path": "packages/core" },
{ "path": "packages/utils" },
{ "path": "packages/app" }
]
}core has no references:
{
"compilerOptions": {
"composite": true,
"declaration": true,
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}utils depends on core:
{
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"references": [{ "path": "../core" }],
"include": ["src"]
}app references both core and utils. Reference every project you import from directly. Do not rely on a transitive reference to cover it.
{
"compilerOptions": {
"composite": true,
"rootDir": "src",
"outDir": "dist"
},
"references": [{ "path": "../core" }, { "path": "../utils" }],
"include": ["src"]
}Run the build from the root:
tsc --build --verboseThe first run builds core, utils, and app in that order. The second run with no edits should do almost nothing. The verbose output is approximate, and the wording varies between TypeScript versions:
Projects in this build:
* packages/core/tsconfig.json
* packages/utils/tsconfig.json
* packages/app/tsconfig.json
* tsconfig.json
Project 'packages/core/tsconfig.json' is up to date because newest input 'packages/core/src/index.ts' is older than output 'packages/core/dist/index.js'
...How tsc decides a project is out of date
For each project, build mode compares inputs against outputs. A project is rebuilt when any of these holds:
- An output file or the
.tsbuildinfofile does not exist. - An input source file is newer than the oldest output.
- The project's tsconfig (or a config it extends) changed after the outputs were written.
- A referenced project was rebuilt and its declaration output changed.
The last rule matters most for speed. If you edit a function body in core and its public types do not change, core rebuilds, but its .d.ts output is identical. Build mode then sees that the dependents' inputs are effectively unchanged. It updates their output timestamps without re-checking them, and with --verbose you will see a message that the project is up to date with .d.ts files from its dependencies. If you change an exported type, every dependent is re-checked.
This is also why isolatedDeclarations and explicit annotations help: stable, explicit declaration output means fewer downstream rebuilds.
Build flags
Build mode accepts a small set of its own flags. Most other compiler options come from each project's tsconfig, not from the command line.
| Flag | Effect |
|---|---|
| `--verbose` | Explains why each project is skipped or rebuilt. |
| `--dry` | Prints what would be built without writing files. |
| `--force` | Treats every project as out of date and rebuilds all of them. |
| `--clean` | Deletes the outputs of the listed projects, including `.tsbuildinfo`. |
| `--watch` (`-w`) | Rebuilds affected projects when files change. |
| `--stopOnBuildErrors` | Stops at the first project with errors (TypeScript 5.6 and later). |
You can pass a directory or a config file path as the target. Without one, tsc -b uses tsconfig.json in the current directory. Pass several targets to build several roots.
# Preview, then build only what app needs
tsc -b packages/app --dry
tsc -b packages/app --verbose
# Remove all outputs for the graph, then rebuild from scratch
tsc -b --clean
tsc -bBuilding packages/app builds core and utils first, because they are reachable from it. Building packages/core alone does not build app.
Behavior on errors
If core has a type error, what happens to utils and app depends on your TypeScript version.
- In versions before 5.6, a project with errors blocked every project that depended on it. Build mode did not emit for it, and dependents were skipped. You saw only the first failure.
- From 5.6, build mode keeps going and reports errors from independent branches of the graph. Use
--stopOnBuildErrorsif you want the old behavior, for example to fail fast in CI.
Check your installed version with tsc -v before you rely on either behavior.
Common errors
These are the errors you will see most often with project references:
- TS6306: Referenced project must have setting "composite": true. Add
composite: trueto the referenced project. - TS6307: File is not listed within the file list of project. A composite project must list every source file through
includeorfiles. Imports that reach outside the listed set cause this error. Widenincludeor fix the import. - TS6305: Output file has not been built from source file. You ran
tsc -pon a consumer before building its dependencies. Runtsc -binstead. - TS6202: Project references may not form a circular graph. Extract the shared code into a third project that both sides reference.
- TS6059: File is not under rootDir. Set
rootDirto the folder that contains all of the project's sources, or move the file.
A related setup problem is that imports between packages must resolve to the referenced project's output. With npm workspaces, the package's main or exports should point into dist, and the consumer lists the package in references. Otherwise the import may resolve to the source and bypass the .d.ts shortcut.
Editors and watch mode
In a referenced project, the editor's language service may resolve imports to the source of the referenced project instead of its built .d.ts. You then get instant go-to-definition without building first. If you prefer to see exactly what the build sees, set disableSourceOfProjectReferenceRedirect: true in the consuming project's compilerOptions. In large graphs this can also reduce editor memory use, at the cost of needing built output.
For development loops, tsc -b -w watches the whole graph. A change in core rebuilds core, then dependents whose inputs changed. A change in app rebuilds only app.
Clean builds and CI
tsc -b --clean removes outputs, which is useful when you suspect stale files, for example after renaming a source file whose old .js and .d.ts linger. In CI the cached state is the .tsbuildinfo files and the dist folders. Restore both together. Restoring only one leaves the check comparing against files that are not there, so everything rebuilds. The CI chapter covers cache keys in detail.
Exercise
Your root tsconfig.json has "files": [] and three references. A teammate runs tsc -p packages/app after editing an exported interface in packages/core, and it passes. Later tsc -b fails in app. What happened, and what command should the teammate have run?
Show answer
tsc -p does not follow references or rebuild them. It type-checked app against the stale core .d.ts files, which still had the old interface. tsc -b rebuilt core, produced a changed .d.ts, and re-checked app against the new types, which exposed the error. The teammate should have run tsc -b packages/app (or tsc -b at the root), which builds core first and then app.
Exercise
You change only a function body in core, with no change to any exported type. Which projects does tsc -b --verbose rebuild, and which does it skip? Why does an exported type change give a different result?
Show answer
core rebuilds because its input is newer than its outputs. Its .d.ts output stays the same, so utils and app are reported as up to date with the .d.ts files from their dependencies, and their timestamps are updated without re-checking. If you change an exported type, the .d.ts output changes, so utils and app have changed inputs and are re-checked and rebuilt.
Exercise
What does tsc -b --dry do, and which flag would you use to force all projects to rebuild?
Show answer
--dry prints which projects would be built without writing any files. --force treats every project as out of date and rebuilds all of them.