Aller au contenu

Leçon 8 · Chat and Codebase Context

Codebase Search and Ignore Files

How Cursor finds code, how .cursorignore and default ignores work, how negation patterns behave, and why ignore files are not a complete security barrier.

When you do not attach files yourself, the agent has to find them. It can search by file name, folder structure and text. The documentation describes a fast regular expression search called Instant Grep, and says its index is built and queried on your own machine, without uploading file paths or code to build it.

Keep two things apart. Building a search index is one thing. When the agent opens a matching file, that content can be sent to a model as part of a request. Ignore files help you control what can be reached.

Help search succeed

Search works better in a well-organized project:

  • Use distinct, descriptive names. Ten files called utils are hard to tell apart.
  • Keep related code together.
  • Add a short README that names the main folders.
  • Give exact error strings or symbol names when you know them: "Find where ERR_PAYMENT_DECLINED is raised."

.cursorignore

Create a file named .cursorignore in the project root. It uses .gitignore syntax. The documentation says it blocks access for the agent, for Tab and for @ mentions.

TEXT
# secrets
.env
.env.*
*.pem
*.key

# data you must not share
data/customers/
exports/

# generated noise
coverage/

The documentation says files listed in .gitignore are ignored automatically, along with a built-in list. That list includes lock files, .env* files, version control folders, dependency and cache folders such as node_modules/, and many binary and media types. You can also add global patterns in your user settings; that list is empty by default.

A second file, .cursorindexingignore, also appears in the documentation. Check the current page for its exact effect before you rely on it.

Limits you must know

An ignore file is not a complete security barrier. The documentation says that the terminal and MCP server tools used by the agent cannot be blocked from code covered by .cursorignore. A command such as cat .env runs in the terminal. It also says complete protection is not guaranteed, because model behavior is unpredictable.

So use layers:

  1. Do not keep production secrets in the project folder.
  2. Use test credentials with limited rights in development.
  3. Control which commands may run, with run modes.
  4. Use .cursorignore as one more layer.

The documentation also says the terminal companion product does not read these ignore files. If you use both, check each one's settings.

Pattern rules

Pattern Meaning
* Any characters except /
** Any characters, including /
? One character
!pattern Include again
# text Comment

Negation has a well-known trap, the same as in Git. If a folder is excluded, the tool does not look inside it, so a later ! line cannot bring back one file. For example, public/* followed by !public/assets/style.css does not work, because public/assets is excluded. The documented workaround is to exclude the nested folder's contents (public/assets/*) and then re-include the file.

You can test patterns with Git, which the documentation suggests: git check-ignore -v path/to/file.

See the matching logic

This stand-in implements a very simplified version of ignore matching. It is not the product's matcher and does not follow all .gitignore rules. Its purpose is to show that a later matching line wins.

Python Stand-in: simplified ignore matching

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

An .env.example file with fake values can show the AI which settings exist without showing real ones. Because .env* files are ignored by default, you need a ! line if you want it visible.

Audit for secrets

An ignore file cannot protect a key that someone pasted into config.py. Look for secrets in the code itself.

Python A small secret spot check

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This catches only obvious cases; it misses the short token on the last line. Use a dedicated secret scanner for real projects, and rotate any real key that has been committed. See removing secrets from history.

When the agent cannot find a file

The troubleshooting guide suggests checking .cursorignore and .gitignore, running the "Reindex" command from the command palette, and attaching the file directly with @.

Decide what belongs in the workspace

Before writing patterns, sort your project's sensitive material into three groups:

Group Example Best handling
Must never be reachable Production keys, real customer exports Keep it outside the project and the machine's reach
Should not be read by AI features Local development settings Ignore file, test values only, limited commands
Fine to share but noisy Generated reports, build output Ignore file, to keep search focused

The first group is the one that people get wrong. If a file would cause serious harm when shared, a pattern in a text file is not enough protection.

After you change an ignore file, test it. Check your patterns with Git where the rules match, and try to attach an ignored file with @ in a practice project that holds only fake values. Review ignore files when you add new folders, because a new backups/ directory is not covered by yesterday's list.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is true about .cursorignore and the terminal?

    The documentation says terminal and MCP tools cannot be blocked this way, so you need other layers.
  2. 2. In ignore syntax, what does a line starting with ! do?

    Negation re-includes a path, with limits when its parent folder is excluded.
  3. 3. Where does the documentation say Instant Grep builds its index?

    The index is local, although opened file content can still go to a model.
  4. 4. A real key was committed last month. What matters most?

    Ignore rules do not undo exposure. Replace the key.

Summary

  • The agent searches names and text; a clear structure helps.
  • .cursorignore uses .gitignore syntax and affects the agent, Tab and @.
  • Many sensitive and noisy files are ignored by default.
  • Ignore files are one layer only; terminal and MCP tools are not blocked.

Next: How the Cursor Agent Works

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.