Sub topic 8
Scoping guidance with path-specific instructions
Repository-wide instructions apply to every pull request. That is a problem when your rules only make sense for part of the code. A rule about TypeScript promises is noise on a Python file. A rule about database migrations is noise on a README. Path-specific instruction files let you attach guidance to the files it applies to.
This chapter assumes you already know the following:
- You can request a Copilot review on a pull request.
- You have written a
.github/copilot-instructions.mdfile. - You can read basic Markdown and YAML frontmatter.
- You know what a glob pattern is, such as
*.ts.
What a path-specific instruction file is
A path-specific instruction file is a Markdown file with a short YAML header. The header names the files the guidance covers. The body holds the rules, written the same way as in the repository-wide file.
Three details define these files:
- They live in the
.github/instructions/directory. - Their names end in
.instructions.md, for exampletypescript.instructions.md. - Their header contains an
applyTofield with one or more glob patterns.
When Copilot reviews a pull request, it checks which files the pull request changed. For each changed file, it includes the instruction files whose applyTo patterns match that file's path. Files that do not match are ignored for that file.
A first example
This file limits TypeScript guidance to .ts files anywhere in the repository. Save it as .github/instructions/typescript.instructions.md.
---
applyTo: "**/*.ts"
---
- Flag use of the `any` type unless a comment explains why it is needed.
- Check that async functions handle rejected promises.The frontmatter sits between two lines of three hyphens and must be the first thing in the file. The applyTo value is quoted because patterns contain *, which YAML can otherwise misread.
Writing `applyTo` patterns
The applyTo value is a glob pattern, matched against file paths relative to the repository root. You can give several patterns in one string, separated by commas.
| Pattern | Matches |
|---|---|
| `*` | All files in the repository root only |
| `**` or `**/*` | All files in all directories |
| `*.py` | `.py` files in the repository root only |
| `**/*.py` | `.py` files in any directory |
| `src/*.py` | `.py` files directly inside `src/` |
| `src/**/*.py` | `.py` files anywhere below `src/` |
| `**/migrations/**/*.sql` | `.sql` files below any directory named `migrations` |
| `**/*.ts,**/*.tsx` | TypeScript and TSX files in any directory |
The most frequent mistake is writing *.ts and expecting it to match nested files. A single * does not cross directory boundaries, so *.ts only matches .ts files at the root. Use **/*.ts for "any .ts file".
---
applyTo: "**/*.test.ts,**/*.spec.ts"
---
- Check that each test asserts something observable, not only that the code runs.
- Flag tests that depend on the current date or on test execution order.
- Do not comment on naming style in tests.This file covers two naming conventions for tests in one place, using a comma-separated list.
How scoped and repository-wide files combine
Path-specific files add to the repository-wide file. They do not replace it. For a changed file, Copilot can draw on .github/copilot-instructions.md and on every path-specific file whose pattern matches.
Because the sets add together, two practical consequences follow:
- Put rules that apply everywhere in the repository-wide file. Put only the extra rules for a part of the code in a scoped file.
- If a repository-wide rule and a scoped rule contradict each other, you cannot predict which one Copilot follows. Remove the conflict instead of relying on order.
A file can also match more than one scoped file. A path such as src/api/users.test.ts matches both the TypeScript file and the test file shown above. Copilot receives both sets of rules. That is usually what you want, but check the combined rules for contradictions.
A worked layout
Suppose a repository has a TypeScript API, SQL migrations, and Markdown documentation. A workable layout looks like this:
.github/
copilot-instructions.md
instructions/
typescript.instructions.md
tests.instructions.md
migrations.instructions.md
docs.instructions.mdThe repository-wide file holds the rules that hold everywhere, such as "keep each comment short and say why the change is needed". Each scoped file covers one area.
---
applyTo: "**/migrations/**/*.sql"
---
- Flag any statement that drops a column or table without a prior deprecation step.
- Flag new indexes on large tables that are not created concurrently.
- Check that every migration has a matching rollback or a note explaining why it cannot be reversed.A pull request that changes a migration and a TypeScript file now gets database-specific scrutiny on the first file and any-type scrutiny on the second. The reviewer does not see the migration rules while looking at the TypeScript.
Excluding a file from code review
The same instruction files can also guide other Copilot features, such as the Copilot cloud coding agent. If a file contains guidance that suits one feature but not another, add excludeAgent to the frontmatter.
---
applyTo: "**/*.py"
excludeAgent: "code-review"
---
- When you write new Python modules, add type hints to every function signature.
- Run the formatter before committing.With excludeAgent: "code-review", the file is not used for Copilot code review. The value "coding-agent" does the opposite and hides the file from the coding agent. Omit the field to make the file available to both. This is useful for instructions that tell an agent how to write or run code, which a reviewer cannot act on.
Writing rules that work as review guidance
Scoping tells Copilot where to look. The wording of each rule still decides whether the comments are useful. The following habits help:
- State one checkable condition per bullet. "Flag any
fetchcall without error handling" is checkable. "Write robust network code" is not. - Say what to leave alone, as in "Do not comment on naming style in tests". This reduces low-value comments.
- Do not copy rules your linter or formatter already enforces. Tools catch those deterministically and for free.
- Avoid instructions that change the output format or tell Copilot to behave in some unrelated way. Review comments follow a fixed format that instruction files do not override.
- Keep each file short. Copilot code review reads only the first 4,000 characters of an instruction file, so rules placed after that point may be ignored. Put the most important rules first, and split a long file into several scoped files.
Common errors
Most problems with scoped files come from a small set of causes.
| Symptom | Likely cause | Fix |
|---|---|---|
| The rules never show up in comments | The file name does not end in `.instructions.md`, or the file is outside `.github/instructions/` | Rename it or move it |
| Rules apply to root files only | A pattern like `*.ts` was used instead of `**/*.ts` | Use `**/` to cross directories |
| The YAML header is shown as text or ignored | The frontmatter is not the first thing in the file, or has a stray character | Start the file with `---` on line one |
| Rules at the end of a long file are never applied | The file is longer than 4,000 characters | Split it, and move key rules to the top |
| Contradictory comments on one file | Two matching files give opposite advice | Edit the files so only one states the rule |
| A new rule has no effect on the current pull request | The instruction change is not on the base branch yet | Merge it, then review a new pull request |
Two of these are worth a closer look. A pattern that is too broad, such as **, turns a scoped file into a second repository-wide file. This is acceptable for a deliberate reason, but a scoped file that matches everything defeats the purpose of splitting. A pattern that is too narrow fails silently. Copilot gives no warning that a file matched nothing, so you only discover it when expected comments are missing.
Checking that a scoped file works
You cannot run a validator over applyTo. Instead, test the way you would test any behavior:
- Write a rule with an easy trigger, such as "Flag any TODO comment in this file type".
- Open a throwaway pull request, targeting a branch that already contains the instruction file, with a change that triggers it.
- Request a Copilot review and look for the comment.
- Open a second change in a file that should not match, and confirm the rule does not appear.
- Replace the test rule with the real guidance.
Run each check more than once if the result is unclear. Copilot's reviews are not deterministic, so a missing comment on a single run does not prove the pattern is wrong. A rule that fires on some runs and not others usually means the wording is vague, not that the scope is broken.
When to split and when to keep one file
Scoped files are not always better. Each extra file is one more place to look when a comment seems wrong.
- Keep a rule in the repository-wide file when it applies to most of the code.
- Create a scoped file when a group of rules applies to a clear subset: a language, a directory, a file type, or a test convention.
- Merge scoped files that always match the same files, because splitting them adds maintenance work without changing behavior.
- Review the instructions folder when code is reorganized. A directory rename can leave a pattern like
legacy/**matching nothing.
Exercise
A repository has .github/instructions/api.instructions.md with applyTo: "src/*.ts". A pull request changes src/routes/users.ts, and the API rules never appear in Copilot's comments. What is wrong, and what pattern fixes it?
Show answer
A single * does not cross directory boundaries, so src/*.ts only matches .ts files directly inside src/. The changed file is in src/routes/, so it does not match. Use src/**/*.ts to match .ts files at any depth below src/.
Exercise
You add a new path-specific file for SQL migrations in a pull request, and in the same pull request you change a migration that should trigger the new rules. You request a Copilot review and see no migration comments. Give one likely reason, and say how you would test the file properly.
Show answer
Copilot uses instruction files from the base branch of the pull request, and the new file exists only on the feature branch, so it does not guide that review. Merge the instruction file into the base branch first. Then open a new pull request with a migration change that should trigger a rule and request a review. Repeat the review once if the result is unclear, since reviews are not deterministic.