Sub topic 14
Validating the file and fixing errors
This chapter assumes you can write a CODEOWNERS file with owners and path patterns. It covers how GitHub reports mistakes in the file, which mistakes are common, and how to fix them.
Why errors matter
GitHub does not reject a CODEOWNERS file with errors. It skips each invalid line and applies the rest. A typo can therefore leave a path unowned, with no review requested for it and no code owner approval required.
Finding errors
GitHub shows errors in two places.
- Web UI. Open the CODEOWNERS file in the repository. A banner lists each problem with its line number.
- REST API. The
codeowners/errorsendpoint returns the same list. This suits CI checks and scripts.
gh api "repos/OWNER/REPO/codeowners/errors?ref=main" \
--jq '.errors[] | "line \(.line): \(.message)"'An empty errors array means every line parsed. The ref parameter selects the branch or commit to check. Use it to validate a pull request branch before you merge.
Common errors
| Problem | Example | Fix |
|---|---|---|
| Owner does not exist | `@exmaple-org/web-team` | Correct the spelling. |
| Team lacks write access | `@example-org/readers` | Give the team write access to the repository. |
| Missing `@` or wrong team format | `example-org/web-team` | Write `@org/team`. |
| Email not linked to an account | `[email protected]` | Use an email that a GitHub account has verified, or use a username. |
| Unsupported pattern syntax | `!docs/`, `[a-c].js`, `\#file` | Rewrite with supported patterns. |
| File too large | Over 3 MB | Merge overlapping patterns, or use broader directory patterns. |
Owners must have write access to the repository. GitHub treats a user or team without it as invalid, even if the name is spelled correctly. A team must also be visible to the repository, and secret teams are not eligible.
Negation with !, character ranges with [ ], and escaping a leading # with \ all work in .gitignore but not in CODEOWNERS. This surprises people who know .gitignore patterns.
A worked fix
This file has three problems:
# .github/CODEOWNERS
* @example-org/maintainers
!/docs/ @example-org/docs-team
/api/ example-org/backend
/scripts/ @octocta- Line 2 uses
!, which is unsupported. - Line 3 has no
@on the team. - Line 4 names a user that does not exist.
The corrected version:
# .github/CODEOWNERS
* @example-org/maintainers
/docs/ @example-org/docs-team
/api/ @example-org/backend
/scripts/ @octocatIf you meant to exclude a path from a broader rule, don't use !. Add a later line with the path and no owners. Chapter 6 covers this.
A repeatable workflow
Exercise
A CODEOWNERS file has 40 valid lines and one line that names a misspelled team. GitHub shows an error banner. Do the other 40 lines still take effect? What is the practical risk of the bad line?
Show answer
Yes. GitHub skips only the invalid line and applies the rest. The risk is that paths covered only by the bad line lose their owner, so no review is requested for them and code owner approval is not required there.
Exercise
Which command checks the CODEOWNERS file on a branch named ownership-update without merging it?
Show answer
gh api "repos/OWNER/REPO/codeowners/errors?ref=ownership-update". An empty errors array means the file parsed cleanly.