Sub topic 8
Scoping guidance with path-specific instructions
This chapter assumes you can open and review pull requests on GitHub, write basic Markdown, and read YAML frontmatter. It also assumes you have already written a repository-wide instruction file, covered in the previous chapter.
Why scope instructions at all
A repository-wide file at .github/copilot-instructions.md applies to every file in every pull request. That works for rules that hold everywhere, such as "keep comments short". It works poorly for rules that only make sense in one part of the codebase.
Consider a repository with a TypeScript front end, Python scripts, and SQL migrations. A rule like "flag use of the any type" is meaningless for Python. If you put it in the repository-wide file, it adds noise to every review and takes up space that more useful guidance could use.
Path-specific instruction files solve this. Each one carries guidance for a set of files selected by a glob pattern. Copilot applies the guidance only when the pull request touches matching files.
How a path-specific file is built
Path-specific files live in .github/instructions/. Each file name ends in .instructions.md. The file starts with YAML frontmatter, a block between two lines of ---, that holds an applyTo key. The body is ordinary Markdown.
---
applyTo: "**/*.ts"
---
- Flag use of the `any` type unless a comment explains why it is needed.
- Check that async functions handle rejected promises.Save this as .github/instructions/typescript.instructions.md. The applyTo value is a glob pattern. The pattern **/*.ts matches any .ts file at any depth in the repository.
The file name before .instructions.md is only a label for people. Pick a name that says what the file covers, such as typescript, migrations, or docs.
Writing glob patterns
The pattern is matched against file paths relative to the repository root. These forms cover most needs.
| Pattern | Matches |
|---|---|
| `**/*.ts` | Every `.ts` file in the repository |
| `src/api/**` | Everything under `src/api/` |
| `**/*.test.ts` | TypeScript test files anywhere |
| `db/migrations/*.sql` | SQL files directly inside `db/migrations/` |
A common mistake is writing *.ts and expecting it to match nested files. A single * does not cross directory boundaries, so use **/*.ts when you want every depth.
How the files combine
Path-specific files add to the repository-wide file. They do not replace it. When a pull request changes a file that matches, Copilot has both sets of guidance available. Keep the two layers separate in purpose:
- Put rules that apply everywhere in
.github/copilot-instructions.md. - Put language, framework, or directory rules in a path-specific file.
- Avoid stating the same rule in both places. Duplicates make the files harder to maintain and can conflict when you edit only one.
A worked layout
Suppose the repository has these instruction files:
.github/copilot-instructions.mdwith general rules about short comments and focusing on correctness..github/instructions/typescript.instructions.mdwithapplyTo: "**/*.ts"..github/instructions/migrations.instructions.mdwithapplyTo: "db/migrations/*.sql".
A pull request that changes src/app.ts and db/migrations/0042_add_index.sql is reviewed with the general rules for both files. The TypeScript rules are relevant to src/app.ts, and the migration rules to the SQL file. A pull request that only edits README.md gets the general rules alone.
Common errors
- Wrong location. A file placed outside
.github/instructions/, or one that does not end in.instructions.md, is not picked up as a path-specific file. - Missing or malformed frontmatter. Without a valid
applyTo, the file has no scope. Check that the---lines are present, that the key is spelled exactlyapplyTo, and that the pattern is quoted. Quoting matters because a pattern starting with*is not valid unquoted YAML. - Patterns that are too narrow or too wide. Test a pattern against a real file path before relying on it. A pattern of
**makes the file behave like a repository-wide one. - Vague rules. "Write good code" gives Copilot nothing to check. Prefer a specific, checkable instruction such as "flag SQL migrations that drop a column without a comment explaining the data impact".
- Expecting guarantees. Instructions steer Copilot's review. They do not force a comment to appear, and results can vary between runs.
Exercises
Exercise
You want Copilot to apply a rule about parameterized queries only to Python files under the services/ directory, at any depth. Write the file path you would save it under and the applyTo line for its frontmatter.
Show answer
Save it under .github/instructions/, for example as .github/instructions/python-services.instructions.md. The frontmatter line is applyTo: "services/**/*.py". The file name must end in .instructions.md, and the pattern is quoted.
Exercise
A teammate adds applyTo: *.ts without quotes and finds the file has no effect. Name two things that could be wrong and how to fix each.
Show answer
The unquoted value starting with * is not valid YAML, so quote it. Also, a single * does not match files in subdirectories, so change the pattern to "**/*.ts" if nested files should match.