# Monorepo

## What it is

A monorepo stores several related projects in one version-control repository. It
may contain an API, web app, shared library and jobs that can each build or
deploy separately. A contributor can change a library and its consumers in the
same commit. The repository gives people and automation a common view of the
versions that belong together.

The build graph matters more than the number of directories. It identifies which
projects depend on a shared component and which outputs can reasonably be built,
tested or released separately. A build tool can use declared dependencies and
file inputs to find the parts affected by a change. A change to a shared library
might need several suites. A change to one application's documentation might
need only its own checks.

A monorepo also needs ownership. Teams can own individual projects, common types
or deployment configuration. Some rules apply across the repository. Other rules
are useful only for one runtime or service. Document who can maintain each kind
of code and how its consumers participate in changes. That makes the shared
history useful without requiring everybody to understand all of it.

## Why it matters

Consider a developer changing a type shared by a mobile client, API and
scheduled job. A single commit can carry the coordinated source changes and
their tests. A reviewer can see the new type and which consumers it affects
together. The resulting artefacts may still reach users at different times. An
older mobile app can remain installed while the API has already changed.
Compatibility across deployed versions remains a separate requirement.

The monorepo also changes how a team pays for checks. Running every build on
every file change can consume the time saved by sharing the source. Declaring
dependencies lets the team test the affected parts sooner. That declaration
needs evidence. A tool that bases its decision on a directory name alone can
miss changes to shared schemas, config or scripts that generate a file consumed
elsewhere.

Cross-project changes can make standards easier to distribute when the same
tools and rules apply. The group also needs clear handling for projects that
have different requirements. A small library might have a different test runner
from a web app. Document common entry points and the ownership of changes to a
shared toolchain. The usefulness of the shared repository comes from being able
to predict which projects and artefacts a change will affect.

## What it looks like in practice

Imagine an application stack with a database schema, a generated client and two
frontends. The repository carries separate build targets. A declared dependency
connects both frontends to their client generator and the generated client to a
schema input. Change a field in the schema and verify that the automation checks
each affected consumer. Change a help page in one frontend and verify that its
build and tests run without needlessly rebuilding the other application.

Treat deployment as its own graph. A source change may require preparing both
frontends for a new API representation several releases before the feature
becomes visible. Record which commit and inputs produce each deployable. When an
automated skip decides there is nothing to publish, its evidence should include
the artefact and its actual dependencies. A shared Git SHA alone cannot tell you
which site's output changed.

Engineer's
[repository guard-engine case study](/portfolio/built-a-268-check-guard-engine-for-the-repo/)
describes validating boundaries that standard tools did not know about,
including routes, migrations and translations. That kind of guard can help a
shared repository express what projects expect from a common change. The
illustrative build graph keeps checks tied to measured inputs and gives a
reviewer a way to trace an affected target.

Also test cache ownership. A build can legitimately reuse an output only for the
inputs its key covers. Change a generator, configured tool version or shared
file and check the selected build again. If the old output still passes the
cache check, investigate that dependency. A useful test compares both the
requested target and the produced artefact.

## Where it goes wrong

A shared directory can become a place where every project imports whatever it
needs from another project. The dependency graph then inherits implementation
details people never agreed to support. Give shared code a defined public
boundary. Review how changes propagate and make tests for the actual consumer
contracts. Where stability matters, demonstrate a revision that can be adopted
in more than one release window.

Treat shared build changes explicitly. Updating a compiler, dependency lockfile
or code generator can affect targets that have no modified source files of their
own. Give those tool inputs paths in the build graph or define the group whose
outputs they invalidate. Include a fixture that changes just the shared tool
version and observes the artefacts rebuilt. A fast affected-target calculation
has value only if its decisions remain correct for these less obvious changes.

Ownership can become uncertain when several projects edit the same
configuration. Identify who reviews a shared change and who can diagnose its
effect. Use a narrow check for the project that owns the configuration and an
integration check where it affects more than one product. An explicit shared
owner makes the project easier to operate when something breaks.

Finally, measure the experience of making a small change. Observe check time,
affected targets, cache results and whether a project can release its own work.
Some changes deserve broad coverage and others have a small impact. A monorepo
is useful when maintainers can explain and test that difference without
depending on a full rebuild to reveal the result.

## Further reading

- [Google's single-repository engineering paper](https://research.google/pubs/why-google-stores-billions-of-lines-of-code-in-a-single-repository/).
  Describes Google's arrangement and the tooling that supports it.
- [The Nx build mental model](https://nx.dev/docs/concepts/mental-model).
  Explains project graphs, tasks and caching.
- [Bazel dependencies](https://bazel.build/basics/dependencies). Describes
  declared and actual dependencies in builds.

<https://engineer.company/glossary/monorepo/>
