Sub topic 2
Where the CODEOWNERS file lives
This chapter assumes you can create branches and pull requests, and that you know your repository's default branch. It covers the places GitHub looks for a CODEOWNERS file and the mistakes that make the file silently do nothing.
The three supported locations
GitHub reads a file named exactly CODEOWNERS. It has no extension, and the name is case-sensitive. It can live in one of three places:
| Location | Path |
|---|---|
| `.github` directory | `.github/CODEOWNERS` |
| Repository root | `CODEOWNERS` |
| Docs directory | `docs/CODEOWNERS` |
Most teams use .github/. It keeps repository metadata together with workflows and issue templates, and it keeps the root uncluttered.
Lookup order
GitHub checks the locations in this order: .github/, then the root, then docs/. It uses the first file it finds and ignores the others completely. The files are not merged.
Suppose a repository has this layout:
.github/CODEOWNERS <- used
CODEOWNERS <- ignored
docs/CODEOWNERS <- ignoredOnly the rules in .github/CODEOWNERS take effect. The other two files give no error and no warning. They just never apply.
Which branch is read
GitHub reads CODEOWNERS from the pull request's base branch, which is the branch you plan to merge into. A new CODEOWNERS file on your feature branch does nothing for that pull request until it is merged into the base branch. A later chapter covers this in detail.
Common errors
- Duplicate files. Someone adds
CODEOWNERSat the root while.github/CODEOWNERSalready exists. The new file is never read. Keep one file and delete the extras. - Wrong name.
CODEOWNERS.txt,codeownersandCODEOWNERare not recognized. No rules load, and you get no obvious failure. - Wrong directory. A file in
.github/workflows/orsrc/is not a supported location. - File too large. A CODEOWNERS file over 3 MB is not loaded. Group rules by directory instead of listing every file.
- Edited on the wrong branch. The change exists on a feature branch only, so the default branch still uses the old rules.
You can confirm which file GitHub treats as active. Open the repository's file view for the base branch and look in the three locations in lookup order. The first one that exists is the one in effect.
git ls-tree -r --name-only origin/main | grep -E '(^|/)CODEOWNERS$'This lists every file named CODEOWNERS on origin/main. Only those at the root, in .github/ and in docs/ matter, and the first of them in lookup order is the active one.
Exercise
A repository has CODEOWNERS at the root and docs/CODEOWNERS. A contributor adds .github/CODEOWNERS with different rules on a feature branch, and the pull request targets main. Which file applies to that pull request, and why?
Show answer
The root CODEOWNERS applies. GitHub reads the file from the base branch (main), and the new .github/ file exists only on the feature branch. Once the .github/CODEOWNERS file is merged into main, it takes priority, and the root and docs/ files are ignored.