x

Guides4 min read

A practical guide to monorepo builds

HS
Hana Sato

Solutions Engineer

Monorepos make sharing code easy and building it hard. At ten packages, you can rebuild everything on each change and barely notice. At 200, the same approach can leave a one-line fix stuck behind an hour of unrelated work.

Here's the setup we recommend after helping hundreds of teams migrate. The order matters, because each step depends on the one before it.

Start with the task graph

Shipr reads your workspace and builds a graph of every package and the tasks that depend on it. Only tasks downstream of a change are run.

$ shipr graph --affected
  @acme/ui        (changed)
  └─ @acme/web    (affected)
  └─ @acme/docs   (affected)
  3 of 214 packages will build

In this example, someone changed the shared UI package. The web app and docs site both use it, so they build too. The other 211 packages can't be affected, so Shipr skips them.

Declare every dependency

The graph is only as accurate as your package files. If a package imports code it doesn't list as a dependency, the import may still work, because another package happened to install that code. The graph can't see the connection, though, so a change can skip a build that needed to run.

Fix these hidden dependencies before you rely on affected-only builds. Look for:

  • Imports that aren't listed in that package's own package.json.
  • Relative paths that reach into another package's source folder.
  • Scripts that read files from elsewhere in the repository.
  • Shared config at the root that affects builds but isn't listed as an input.

Add a lint rule for the first two as soon as you've cleaned them up. Otherwise they'll come back, usually in a hurry and just before a release.

Describe inputs and outputs

Next, tell Shipr what each task needs and what it produces. Inputs decide when a task has to run again. Outputs are the files Shipr restores when it doesn't.

# shipr.toml
[tasks.build]
depends_on = ["^build"]   # build dependencies first
inputs = ["src/**", "package.json", "tsconfig.json"]
outputs = ["dist/**"]

[tasks.test]
depends_on = ["build"]
inputs = ["src/**", "tests/**"]

The ^ means "this task in every package I depend on". Without it, build would refer to the task in the same package. Mixing up the two is a common reason for builds that pass on a laptop, where old output is still on disk, and fail on a clean machine.

Start with broad inputs and narrow them later. Inputs that are too broad cost you a few extra builds. Inputs that are too narrow can give you an out-of-date result, which takes much longer to track down.

Turn on remote caching

With a shared cache, the second person to build a commit, whether a teammate or CI, reuses the first person's results. Affected-only builds and caching solve different problems. The first skips packages your change can't reach. The second skips work that someone has already done. Large repositories need both.

Remote caching is available on Pro and Team. Before you turn it on for everyone, run the same commit on two clean machines and compare the output. If the results differ, look for timestamps, random values or absolute paths in the generated files.

Split the packages that slow everyone down

Once the graph is accurate, it will show you where your structure is working against you. The usual culprit is a package called something like shared or utils that nearly everything imports. Each edit to it rebuilds most of the repository.

Split it by purpose, so date formatting and API types don't live together. You don't need dozens of tiny packages, since each one adds its own overhead. Aim to keep code that changes often away from code that everything depends on.

If every change affects everything, you have one big package with a lot of folders.

Measure, then optimise

It's easy to spend a week speeding up a task that hardly anyone waits for. Look at your own build data before choosing what to fix:

  • Track p50 and p95 build times, not averages.
  • Watch cache hit rate per package.
  • Find the longest chain of dependent tasks. Extra runners can't make a build shorter than that chain.
  • Split slow test suites across parallel runners.

Roll it out in stages

You don't have to change everything at once. Fix hidden dependencies first, then run affected-only builds alongside your full build for a couple of weeks. Whenever the full build catches a failure that the affected-only build missed, find the missing dependency and add it.

When the two agree consistently, use affected-only builds for pull requests. Keep a scheduled full build without the cache as a safety net. It will catch the occasional gap in your graph without making everyone wait for it on every push.