Sub topic 14
Project References in CI
Assume you know basic TypeScript, tsconfig.json, npm scripts, and shell commands. This chapter focuses on running referenced projects in continuous integration (CI), an automated system that checks every change.
CI should verify the same dependency graph that developers use locally. A project-reference build does this with tsc --build. The command reads the root solution configuration, builds referenced projects in order, and reuses valid build information when possible.
A reproducible CI build
Start CI from a clean checkout. Install the exact dependency versions from the lockfile, then invoke the root build:
set -euo pipefail
npm ci
npx tsc --build --pretty falsenpm ci removes the existing dependency directory and installs exactly what the lockfile specifies. The --pretty false option keeps compiler output suitable for CI logs.
A root tsconfig.json should list the projects that CI must build:
{
"files": [],
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/app" }
]
}The referenced projects must have compatible compiler versions and valid composite settings. If app references core, build mode checks core first. It then type-checks app against core's declaration output.
| CI goal | Typical command | Result |
|---|---|---|
| Normal verification | `npx tsc --build --pretty false` | Builds changed projects and dependencies |
| Full rebuild | `npx tsc --build --force --pretty false` | Ignores previous build status |
| Inspect decisions | `npx tsc --build --dry --verbose` | Reports what build mode would do |
Clean builds and caching
A clean CI runner usually has no .tsbuildinfo files or compiled output. That makes each run independent and easy to reproduce. This is the safest default for release validation.
Large repositories can cache build state to reduce duration. A cache may include .tsbuildinfo files and project output directories, but its key must change when the build environment changes. Include at least the operating system, TypeScript version, lockfile hash, and relevant compiler configuration in the key.
Do not use tsc --build --clean as a substitute for a clean checkout unless the projects have already been configured correctly. The command deletes outputs described by the project configurations. A disposable CI workspace is simpler and avoids preserving unexpected files.
A useful CI sequence
A practical pipeline separates dependency installation, compilation, tests, and packaging:
set -euo pipefail
npm ci
npx tsc --build --pretty false
npm test
npm run packageCompilation comes before tests so that missing declarations, invalid references, and type errors fail quickly. If tests use a separate TypeScript project, add that project to the solution references or build it explicitly. Otherwise, the main build may not check test-only files.
Build order in the pipeline
The dependency graph determines order. CI does not need separate commands for core and app; the root solution describes the graph.
A cycle such as core referencing app while app references core cannot be ordered. Break the cycle by moving shared types or functions into a lower-level project.
Exercise
Why should CI run tsc --build from the solution-style root instead of compiling only the application project?
Show answer
The root build follows every declared reference, builds dependencies first, and checks that the complete project graph is valid.