Cursor Rule Globs: How to Scope Rules to the Right Files
You wrote a rule about React Server Components, and Cursor applied it to your Express route handlers. Or you wrote a rule about test files and it never showed up in a single test. Both failures come from the same place: the pattern in the rule's frontmatter did not match the files you thought it did. The cost is quiet. The model either gets instructions that contradict the file it is editing, or it never sees the instruction at all.
Cursor rule globs are the file patterns in an .mdc rule's frontmatter that tell Cursor to attach that rule only when matching files are part of the conversation. This post explains how globs behave, how to write patterns that hold up as a repo grows, and how to check that a rule is firing when it should.
What are Cursor rule globs and when do they run?
A glob is a path pattern such as src/components/**/*.tsx stored in the globs field of a rule file inside .cursor/rules/. When a file matching the pattern is in context, Cursor attaches the rule automatically. This is the "auto attached" behaviour, and it is separate from rules that are always on, rules the model chooses by reading a description, and rules you pull in by hand with an @ mention. The Cursor rules documentation describes these rule types and the frontmatter fields.
The important detail is the trigger. A glob does not look at what you typed in the prompt. It looks at which files are attached or referenced in the conversation. If you ask a vague question with no file open, a glob-scoped rule has nothing to match against, and it stays out.
That makes globs the cheapest way to keep context lean. A rule that only loads for app/api/**/route.ts costs nothing when you are editing a CSS file.
Why does a glob-scoped rule not apply?
The most common reason is that no matching file is in context when you ask the question. The pattern can be perfect and the rule will still not load, because attachment is driven by files, not by topic.
The second most common reason is a pattern that is subtly wrong. Three mistakes account for most of what you will see:
- Missing the recursive wildcard. A pattern like
components/*.tsxmatches files directly insidecomponents, not files in nested folders. Usecomponents/**/*.tsxto reach every depth. - Wrong root. Patterns are relative to the project root. If your app lives in
apps/webin a monorepo, a pattern starting withsrc/will match nothing. - Wrong extension. A rule for React components that only lists
*.tsxmisses.jsxfiles, and a rule for TypeScript that lists only*.tsmisses.tsx.
A fourth cause is the frontmatter itself. If the file has no description and alwaysApply is false, the glob is the only way the rule can load. A typo in the field name means the rule never loads under any condition. Create one rule through Cursor's own rule UI and compare its frontmatter to your hand-written files, because the exact list syntax for multiple patterns is the sort of detail that differs between versions.
How do you write a glob that matches only what you mean?
Start from the convention you want to enforce, then write the narrowest pattern that covers every file where that convention applies. Narrow patterns fail loudly, because you notice the rule is missing. Broad patterns fail silently, because the rule loads everywhere and competes with other instructions.
Here is a reference table of patterns that cover common Next.js and TypeScript layouts. Treat the paths as examples and adjust them to your own folder structure.
| Pattern | Matches | Good for |
|---|---|---|
app/**/page.tsx | Every App Router page file at any depth | Data fetching and metadata conventions |
app/api/**/route.ts | Every route handler | Validation, error shape, auth checks |
components/**/*.tsx | Components in any subfolder | Naming, props typing, styling approach |
**/*.test.ts | Unit test files anywhere in the repo | Test structure and mocking rules |
prisma/** | Everything under the prisma folder | Schema and migration conventions |
One opinion worth stating: split rules by file role, not by technology. A single rule called "React and Next.js" with a pattern of **/*.tsx will load for pages, components, tests and layouts alike, so it has to be vague enough to be true for all of them. Four small rules, each attached to one role, can each be specific enough to be useful.
How do you stop two glob rules from contradicting each other?
Overlap is legal, so you have to design around it. If components/**/*.tsx and **/*.tsx both match a file, both rules attach, and if they disagree the model has to pick one. It will not tell you it made a choice.
The safest approach is to make overlap additive. A broad rule sets a baseline, such as "use named exports", and a narrow rule adds to it without restating or reversing it. Never write a narrow rule that says the opposite of a broad one. If you need an exception, change the broad pattern so it no longer matches the exception.
Overlap also has a budget cost. Every attached rule consumes context, and the more rules that stack on one file, the less weight each one carries. If you find five rules attaching to a single component file, the patterns are too wide, or the rules should be merged.
How do you set up and verify glob-scoped rules?
Follow these steps for each rule, and keep them in order, because the check at the end catches the mistakes in the earlier ones.
- Write the rule's single purpose in one sentence, such as "route handlers return a typed error object".
- Pick the folder and extension where that purpose applies and write the narrowest pattern that covers them.
- Run a file listing in your terminal with the same pattern shape and confirm it returns the files you expected and nothing else.
- Open one matching file in Cursor, start a chat with that file attached, and ask the model to list the rules it has loaded.
- Open one non-matching file, repeat the question, and confirm the rule is absent.
Steps four and five are the part most people skip. Without the negative test, a pattern that matches everything looks identical to a pattern that matches correctly.
Is writing and maintaining all these patterns worth it?
For a handful of rules, yes, and you should do it by hand. The cost shows up when you want fifty rules across several stacks, each with its own tested pattern and its own frontmatter, in both the older .cursorrules style and the newer .mdc format. At that point the writing is the easy part and the verification is the slow part.
That is the gap the Cursor Rules Mega-Pack from BuyCoded is built for. According to its product page, it contains 1,057 rules across 20 stacks in both Cursor formats, so you start from rules that already have scoping decided and adjust the patterns to your folder layout instead of writing them from zero.
The honest objection is that a purchased pack does not know your repo. That is true, and it is why you should still run the five-step check above on any rule you adopt. A pack saves you the drafting, not the verification. If your project has an unusual layout, expect to edit the patterns, which is typically a one-line change per rule.
What should you do next?
Pick the three rules you rely on most, rewrite each with one narrow glob, and run the match and no-match test on each. If you would rather start from a library of rules than draft them, browse the BuyCoded catalog, open the Cursor Rules Mega-Pack page to see the stacks it covers, and check the details against your own project before you buy.
Frequently asked questions
What is a glob in a Cursor rule?
A glob in a Cursor rule is a file path pattern, such as components/**/*.tsx, written in the globs field of the rule's frontmatter. When a file matching the pattern is attached to the conversation, Cursor loads that rule automatically. Files that do not match leave the rule out, which keeps unrelated instructions out of the model's context.
Why is my Cursor rule with globs not being applied?
The usual cause is that no matching file is in the conversation, because globs trigger on attached files, not on your prompt text. Other causes are a pattern missing the recursive double-star wildcard, a pattern written relative to the wrong folder in a monorepo, or a typo in the frontmatter field name.
Can two Cursor rules match the same file?
Yes, both rules attach when both patterns match the file. Cursor does not stop one from loading because another already did. Keep overlapping rules additive, so a narrow rule extends a broad one without contradicting it, because the model will resolve a conflict silently and you will not be told which instruction won.
How do I check which Cursor rules are loaded?
Open a file that should match, start a chat with that file attached, and ask the model to list the rules it has loaded for the request. Then repeat with a file that should not match. Seeing the rule present in the first case and absent in the second proves the glob works in both directions.
Should I use alwaysApply or globs for a rule?
Use globs when a rule only matters for certain files, and use alwaysApply only for conventions that hold across the entire repository, such as commit style or naming. Always-on rules consume context on every request, so each one you convert to a narrow glob frees space for the instructions that actually apply.