Repositories and monorepos¶
How to split code into repositories, structure a monorepo, assign ownership of code, and protect the branch that everyone integrates into. Integrating into trunk at least daily (TEST-3) and automated tests for dependency rules (TEST-4) are in Testing and delivery; team boundaries and Conway's law are in Principles; release branches and tags are in Versioning and releases. For workflow files on GitHub, also follow GitHub Actions.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 8 Software Engineering at Google, chapter 16: Version Control and Branch Management: a single source of truth, the One-Version Rule, why to avoid long-lived branches, release branches, and monorepos.
- 8 Patterns for Managing Source Code Branches (Martin Fowler): mainline, healthy branch, integration and release patterns, and how Git-flow, GitHub Flow and trunk-based development combine them.
- 8 Trunk-based development (DORA): the research-backed practices (three or fewer active branches, merging into trunk at least daily, no code freezes), common pitfalls, and how to measure them.
- 7 Why Google Stores Billions of Lines of Code in a Single Repository (Potvin and Levenberg, Communications of the ACM, 2016): the benefits and costs of a monorepo at very large scale, including the tooling it needs.
- 6 monorepo.tools (Nx): what a monorepo is and isn't, the costs of many separate repositories, and a comparison of monorepo tools. Written by a tool vendor.
Key rules¶
Repository layout¶
- 7
REPO-1Keep one version of each dependency across the code you build together (the One-Version Rule), whatever the repository layout. Teams adding a dependency on an internal library or third-party package shouldn't have to choose a version; several versions lead to diamond dependencies and wasted effort. (Software Engineering at Google, The "One-Version" Rule, Monorepos) - 7
REPO-2In a monorepo, make each project a package with declared dependencies and a public entry point, and don't import another project's internal files. If you're writing../to reach into another package, depend on the package instead, and check the allowed dependencies between projects in CI, asTEST-4asks. (monorepo.tools, What is a monorepo?, Turborepo, Structuring a repository, Nx, Enforce module boundaries) - 6
REPO-3Choose between one repository and several on purpose, and record the decision in an ADR: a monorepo suits projects that change together and share code, but needs build tooling that scales with it. Separate repositories are easier to scale and give teams autonomy, but sharing code and changing several projects at once then need published versions and coordinated upgrades. (Software Engineering at Google, Monorepos, Potvin and Levenberg, Why Google Stores Billions of Lines of Code in a Single Repository, monorepo.tools, The polyrepo tax) - 6
REPO-4In a monorepo, let CI build and test only the projects a change affects, with a tool that knows the project graph, and cache task results. (Nx, Run only tasks affected by a PR, Turborepo,run --affected)
Branches, ownership and review¶
- 8
REPO-5Protect the default branch: merge only through pull requests with passing status checks, block force pushes and deletion, and don't let administrators bypass the rules. Require at least one approving review when the project has more than one active maintainer. (GitHub, About protected branches, GitHub, About rulesets, GitLab, Branch rules, OpenSSF Scorecard, Branch-Protection) - 6
REPO-6Give every part of the repository an owner in aCODEOWNERSfile, including the file itself, and require review from code owners on the default branch.GHA-9asks the same for workflow files. (GitHub, About code owners, GitLab, Code Owners) - 6
REPO-7Keep changes small and respond to review requests within one business day, so that branches stay short-lived. A slow, heavy review process makes developers batch up changes, which undermines trunk-based development. (Google, Speed of code reviews, Google, Small CLs, DORA, Trunk-based development) - 5
REPO-8When many people merge into one busy branch, use a merge queue, so that each change is tested together with the changes merged ahead of it before it lands. (GitHub, Managing a merge queue, GitLab, Merge trains)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 Nx
enforce-module-boundariesrule (JavaScript and TypeScript): a lint rule that checks imports between projects against tag-based constraints, and flags circular dependencies between projects. Partly checksREPO-2. - 6 OpenSSF Scorecard checks: Branch-Protection scores the default and release branches' protection settings, and Code-Review checks that recent changes were reviewed before they merged; the highest Branch-Protection tiers also require review from code owners. Checks
REPO-5, and partlyREPO-6. - 6 dependency-cruiser (JavaScript and TypeScript):
forbiddenrules with group matching stop one package from importing another's internals, and fail CI on violations. Partly checksREPO-2. - 6 Import Linter independence contracts (Python): fail when modules that should be independent import each other, even indirectly. Partly checks
REPO-2. - 5 Syncpack
lint(JavaScript): fails CI when packages in a monorepo use different versions of the same dependency. Sherif'smultiple-dependency-versionsrule does the same. Partly checksREPO-1. - 4
turbo boundaries(experimental): reports imports of files outside a package's directory and of packages it doesn't declare as dependencies, and checks tag rules. Partly checksREPO-2.
Tutorials and further reading¶
- 6 Crafting your repository (Turborepo): a guide, meant to be read in order, to structuring a JavaScript monorepo, managing dependencies, creating internal packages, and setting up tasks, caching and CI.
- 6 Workspace (pnpm): setting up a multi-package workspace, and the
workspace:protocol that links internal packages from the repository instead of a registry. - 5 Improving monorepo performance (GitLab): why large repositories get slow in Git, and how to keep clones and CI fast.