Sub topic 14
Project references in CI
This chapter covers running tsc --build on a continuous integration (CI) server. It assumes you can already build the core and app projects from earlier chapters, that your root tsconfig.json is solution-style, and that you know what a .tsbuildinfo file is. The examples use GitHub Actions, but the ideas carry over to any CI system.
What CI changes
On your machine, build mode is fast because the previous build's outputs are still on disk. A CI runner usually starts from a fresh checkout. There are no dist folders and no .tsbuildinfo files, so every run is a full build.
CI also has different goals from local work:
- A failed type check must stop the pipeline with a non-zero exit code.
- The result must not depend on stale files left by an earlier run.
- Repeated runs should be fast enough that developers do not skip them.
The first two goals are about correctness. The third is about caching. This chapter handles correctness first and adds caching on top.
The baseline CI job
The simplest correct job installs dependencies from a lockfile, then builds from the solution config:
name: typecheck
on:
pull_request:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npx tsc --build --verboseThree details matter here:
npm ciinstalls exactly what the lockfile says and fails ifpackage.jsonand the lockfile disagree. Plainnpm installcan change the TypeScript version between runs.npx tscuses the TypeScript version fromdevDependencies, not a global one. Project references depend on the compiler version, because.tsbuildinfofiles record the version that wrote them.tsc --buildexits with a non-zero code if any project has errors. No extra flag is needed to fail the job.
--verbose makes the log explain each decision, for example Project 'packages/core/tsconfig.json' is out of date because output file ... does not exist. This is useful when you debug why CI is slow.
Why cold runs still pay off
Even with no cache, build mode helps in CI. Each project is checked once, in dependency order. When app is checked, the compiler reads core's .d.ts files instead of re-checking core's source. In a large repository this keeps memory use lower than one giant program, because only one project's source is loaded as a full program at a time.
If one project fails, build mode does not build the projects that depend on it. The log shows which upstream project broke, so you do not get a flood of follow-on errors.
Caching build outputs
To make warm runs fast, restore two things between runs: each project's emitted outputs and its .tsbuildinfo file. With outDir set to dist in each project, both live under packages/*/dist. The default location of the info file is dist/tsconfig.tsbuildinfo, named after the config file.
- uses: actions/cache@v4
with:
path: packages/*/dist
key: tsc-${{ runner.os }}-${{ hashFiles('package-lock.json', 'tsconfig.base.json', 'packages/*/tsconfig.json') }}-${{ github.sha }}
restore-keys: |
tsc-${{ runner.os }}-${{ hashFiles('package-lock.json', 'tsconfig.base.json', 'packages/*/tsconfig.json') }}-
- run: npx tsc --build --verboseThe key has two parts, and the design is deliberate:
- The prefix hashes inputs that should invalidate the whole cache: the lockfile (which pins the TypeScript version), the base config, and the project configs. If any of these change, the old
.tsbuildinfofiles describe a different compiler setup. - The
github.shasuffix makes every run save a new cache entry.restore-keysthen falls back to the most recent entry with the same prefix.
Do not put a hash of the source files in the prefix. A source change would then produce a cache miss, and you would lose the incremental build in exactly the case it exists for.
What happens after a cache restore
A restored cache is not a guarantee that nothing is rebuilt. Build mode decides whether a project is up to date by comparing file timestamps. After a fresh checkout, every source file has a new modification time, so the restored outputs look older than their inputs.
This does not force a full rebuild. The compiler loads the restored .tsbuildinfo, which stores a hash for each source file and the signature of each file's declarations. It finds that the hashes match, skips re-emitting, and reports that it is updating output timestamps. When a file did change, only that file and the files whose types depend on it are re-checked and re-emitted.
A cache hit therefore saves most of the work, but it does not make the run instant. Each project is still loaded and compared. Expect the saving to be largest for the projects that did not change.
You can confirm that the cache works by reading the verbose log. Look for lines such as Updating output timestamps of project for unchanged projects, compared with Building project for changed ones.
Reproducibility checks
A cache speeds up builds but can also hide problems. If a cached output is stale or wrong, CI might pass while a clean build fails. Guard against this in two ways.
- Run a clean build on a schedule, or before releases.
--forceignores all up-to-date checks and rebuilds every project:
``bash npx tsc --build --force ``
- Build on a runner with no cache for the
mainbranch. Pull requests can use the cache, andmainacts as the source of truth.
A third check catches a different problem: outputs committed or generated outside the build. Run git status --porcelain after the build and fail if it prints anything. This finds generated files that were meant to be ignored but are not in .gitignore.
- run: npx tsc --build
- run: test -z "$(git status --porcelain)"Tests and later steps
Build mode only type-checks and emits. Test runners, linters, and bundlers are separate steps. Order the job so that tsc --build runs first. If you run tests against the emitted JavaScript in dist, they then get exactly what was built. If you run tests with a tool that compiles TypeScript itself, the build step still catches type errors that tool might ignore.
If tests live in their own project, as covered in the earlier chapter on tests and tooling, they appear in the solution config's references list. A single tsc --build at the root then covers them. You do not need a second command.
Parallel jobs and partial builds
Large repositories often want to build one project and its dependencies only. You can pass a project path to build mode:
npx tsc --build packages/appThis builds packages/core first, because app references it, and then builds app. It does not touch unrelated projects. This works well for pipelines where each package has its own job. The trade-off is that nothing checks the projects you left out, so keep one job that builds from the root.
Build mode in the TypeScript version you pin runs project builds in dependency order, and a newer compiler may also offer options for parallel project builds. Check the release notes of your version before relying on this. If you need parallelism today, the reliable approach is separate CI jobs for independent branches of the graph.
The diagram shows the order of a cached CI run.
Common CI problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Every run is a full build | Cache key includes a source hash, or the path does not match `outDir` | Hash only config and lockfile; confirm the real `.tsbuildinfo` location with `find packages -name "*.tsbuildinfo"` |
| Passes with cache, fails without | Stale cached outputs | Add a scheduled `--force` build; rebuild on `main` without a cache |
| `.tsbuildinfo` ignored after upgrade | TypeScript version changed | Include the lockfile in the cache key |
| Error: referenced project must have `composite` set | A project in `references` lacks `composite: true` | Enable it in that project's config |
| Local build differs from CI | Different TypeScript version or operating system | Pin the version in `devDependencies`; use `npm ci` |
Exercise
Your CI cache key is tsc-${{ hashFiles('packages/**/src/**') }}. Builds are never faster than a cold run. Why, and how would you change the key?
Show answer
The key changes whenever any source file changes, so almost every commit is a cache miss and no previous .tsbuildinfo files are restored. Hash only inputs that should discard the whole cache, such as the lockfile and the tsconfig files, and add a unique suffix such as the commit SHA. Then use restore-keys with the shared prefix so each run restores the most recent cache and the compiler re-checks only changed files.
Exercise
A pull request passes CI, but the same commit fails on a developer's clean checkout with a type error in core. What are two likely causes, and what check would find the problem?
Show answer
One cause is a stale cached output or .tsbuildinfo that no longer matches the sources. Another is a different TypeScript version between CI and the developer. Running npx tsc --build --force on a runner with no cache, and printing npx tsc --version in both places, shows which one it is.