123DevOps··8 min read

Monorepo vs polyrepo: Nx, Turborepo, Bazel, and when each approach makes sense

The pros and cons of putting everything in one repository or spreading it across many, what Nx, Turborepo, Bazel, and native workspaces bring, how to run only affected projects in CI, and how to decide and migrate painlessly.

Development

Every company that grows past half a dozen services ends up having the same debate: do we keep each project in its own repository or put everything in one? It sounds like a question about folder layout, but the answer changes CI, code review, how internal libraries are versioned, and even how teams talk to each other.

There is no universal winner, only the approach that fits your teams, your shared code, and your investment in tooling.

Quick definitions

  • Polyrepo: each service, library, or application lives in its own Git repository, with independent pipelines, versioning, and permissions.
  • Monorepo: several projects — sometimes the entire company — live in a single repository, usually organized into folders such as apps/ and packages/.

A monorepo is not a monolith. A monorepo can hold dozens of microservices that are deployed completely independently; what changes is where the code lives, not how it runs.

Pros and cons side by side

Criterion Monorepo Polyrepo
Code sharing Import straight from the neighboring folder, no package publishing Requires publishing and versioning libraries in a registry
Atomic changes A single PR changes the library and all of its consumers Coordinated change across several PRs and releases
Tooling Needs tooling for task graphs, caching, and affected detection Standard tools work with no tweaks
CI time Explodes if everything always runs; drops sharply with caching and affected detection Each pipeline is small, but there are many to maintain
Access control Read access is usually all or nothing for the repository Fine-grained permissions per repository
Ownership Must be made explicit (CODEOWNERS) Natural: the repo owner owns the code
Repository size Keeps growing; clone and git status get slow Small, fast repositories

Some very large tech companies use giant monorepos. The best-documented case is Google’s, described in a 2016 article in Communications of the ACM, which runs on an in-house version control system. On the Git side, tools such as scalar, now shipped with Git itself, were created precisely to handle very large repositories. Those companies dedicate entire teams to build tooling; copying the structure without the investment usually goes wrong.

Tooling: from native workspaces to build systems

Tool Ecosystem What it brings
npm, pnpm, and Yarn workspaces JavaScript/TypeScript Multiple packages in one repo, local links between them, a single lockfile
Go workspaces (go.work) Go Develop several modules together without replace in go.mod
Cargo workspaces Rust Crates sharing one Cargo.lock and one target directory
Turborepo JavaScript/TypeScript Task graph, local and remote caching, change-based filters
Nx JS/TS, with plugins for other languages Project graph, affected, remote caching, code generators
Bazel Multi-language Hermetic, reproducible builds, remote caching and execution, very high scale

Native workspaces

They are the first step. They resolve dependencies between local packages, but they do not know what needs to be rebuilt. For many small teams, that is enough.

Turborepo and Nx

They add three ideas. The task graph: if web depends on ui, the ui build runs first. Caching: if a task’s inputs have not changed, its result is reused instead of being executed again — and with a remote cache, that applies to the whole team and to CI. And affected detection: by comparing against main, only the projects that changed and the ones that depend on them run.

Bazel

It grew out of Google’s internal build tool. Each folder declares targets in BUILD files, with explicit inputs and dependencies, which enables hermetic builds and distributed execution. The power is enormous, and so is the learning curve; it makes the most sense in large, multi-language repositories.

Hands-on example: pnpm workspaces with Turborepo

A typical layout:

.
├── apps/
│   ├── web/
│   └── api/
├── packages/
│   ├── ui/
│   └── config/
├── package.json
├── pnpm-workspace.yaml
└── turbo.json

pnpm-workspace.yaml tells pnpm where the packages are:

packages:
  - "apps/*"
  - "packages/*"

An app consumes the local library through the workspace: protocol, without publishing anything:

{
  "name": "@your-org/web",
  "private": true,
  "dependencies": {
    "@your-org/ui": "workspace:*"
  }
}

And turbo.json defines the tasks. In current versions (2.x) the key is tasks; 1.x versions used pipeline, so watch out when copying older examples. Turborepo 2.x also requires the packageManager field in the root package.json.

{
  "tasks": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**"]
    },
    "test": {
      "dependsOn": ["^build"]
    },
    "lint": {},
    "dev": {
      "cache": false,
      "persistent": true
    }
  }
}

^build means “run the build of my dependencies first.” In practice:

pnpm turbo run build                             # everything, honoring the graph and the cache
pnpm turbo run build --filter=@your-org/web      # only the web app and what it needs
pnpm turbo run test --filter="...[origin/main]"  # changed since main + whatever depends on it
npx nx affected -t test --base=origin/main       # the Nx equivalent

CI in a monorepo: run only what changed

The classic mistake is plugging the monorepo into the same old pipeline and watching CI time multiply. Three techniques solve most of the problem, and we cover several of them in detail in how to cut the pipeline from 40 to 10 minutes:

  1. Path filters so the pipeline does not fire when only documentation changed.
  2. Affected detection so only impacted projects get tested.
  3. Remote caching so CI reuses results that another job or a developer already produced.
name: ci
on:
  pull_request:
    paths-ignore:
      - "docs/**"
      - "**/*.md"

jobs:
  build-test:
    runs-on: ubuntu-latest
    env:
      TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}   # remote cache, no secret in the code
      TURBO_TEAM: ${{ vars.TURBO_TEAM }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0   # full history to compare against the base branch
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm turbo run lint test build --filter="...[origin/${{ github.base_ref }}]"

fetch-depth: 0 is essential: without history, the tool cannot compare against the base branch and ends up running everything — or nothing.

Ownership and permissions

In a monorepo, “who owns this code?” has to be written down. The CODEOWNERS file (supported by GitHub and GitLab) maps paths to teams and, with branch protection requiring code owner approval, every PR automatically pulls in the right reviewers — a natural complement to code review that works.

# .github/CODEOWNERS — the last matching rule takes precedence
*                    @your-org/platform
/apps/web/           @your-org/web-team
/apps/api/           @your-org/payments-team
/packages/ui/        @your-org/design-system
/.github/workflows/  @your-org/platform

Keep in mind that Git has no per-folder read permissions: anyone who can clone sees everything. If part of the code truly needs to be restricted, it belongs in a separate repository.

Big repository? Sparse checkout and partial clone

As the repo grows, nobody needs to download everything. A partial clone defers downloading file contents, and sparse checkout keeps only the folders you work on in your working tree:

git clone --filter=blob:none --sparse https://github.com/your-org/monorepo.git
cd monorepo
git sparse-checkout set apps/web packages/ui
git sparse-checkout list

How to decide

Scenario Tends to favor
Few teams, lots of shared code, same language Monorepo
Frequent changes that span a service and a library Monorepo
Autonomous teams, different stacks, little code in common Polyrepo
Code with strong access restriction requirements Polyrepo (at least for that part)
Nobody available to look after build tooling Polyrepo or a small monorepo with workspaces only

A monorepo pairs very well with trunk-based development: short-lived branches and continuous integration avoid conflicts in a repository with many contributors. It also makes it easier to offer golden paths, since templates, lint configs, and pipelines live in one place.

Migration tips

  • Start small: first bring together the projects that change together most often, not the entire company.
  • Preserve history with git subtree add --prefix=apps/api <repo-url> main, which brings the old repository’s commits into a subfolder.
  • Set up caching and affected detection before moving the second team in; otherwise, the first impression will be a slow CI.
  • Define CODEOWNERS on day one and archive the old repositories as read-only.

Checklist

  • Decision based on teams, shared code, and capacity to maintain tooling
  • Native workspaces configured (pnpm-workspace.yaml, go.work, or Cargo.toml)
  • Task graph and caching with Turborepo, Nx, or Bazel
  • CI running only affected projects, with fetch-depth: 0 and remote caching
  • CODEOWNERS covering every folder and enforced by branch protection
  • Access-restricted code kept outside the monorepo
  • Sparse checkout documented for people who work on parts of the repository

Monorepo or polyrepo is a decision about how people collaborate; the tooling just makes that decision sustainable.

Get updates on new posts and stay in the loop!