The fastest way to fix a bug in Cursor is to hand it three things: the error (reference @Terminal and @Lint Errors so it reads the live output), the code (@Files or @Codebase), and expected-vs-actual. Then let Agent reproduce the bug with a failing test, trace it to the root cause, apply the smallest safe fix, and re-run the tests to confirm.

Use Ask mode when you only want a diagnosis or an explanation, and Agent mode when you want the fix applied and verified across files. New to this? Start with the best Cursor prompts, go deeper on Cursor Agent mode prompts, and pair these with prompts for writing tests.

Advertisement

Diagnose errors

Five prompts to turn a red terminal into a root cause. Point Cursor at the live error state with @Terminal and @Lint Errors instead of pasting screenshots.

1. Diagnose from @Terminal and @Lint Errors

Ask mode. Diagnose the current error — don't edit anything yet.

Read @Terminal and @Lint Errors for the failing command and diagnostics.
Relevant code: @Files [path/to/file]

Do this:
- State the single most likely root cause first, then 2 alternates.
- Point to the exact file and line where it originates.
- Tell me the one-line fix and where it goes.
- Say what you'd still need to see to be sure.

Why it works: Referencing @Terminal and @Lint Errors lets Cursor read the real error state, so it diagnoses your actual failure instead of a paraphrase of it.

2. Root-Cause a Stack Trace

Walk this stack trace to the root cause. Ask mode — explain, don't edit.

<trace>
[paste the full stack trace, or reference @Terminal]
</trace>

Do this:
- Read the trace bottom to top and identify the frame where MY code is at fault (not library internals).
- Explain what call path led there and why it threw.
- Name the underlying cause, not just the line that crashed.
- Give the fix and one nearby thing worth checking.

Best for: Long traces — telling Cursor to find the first frame in your own code cuts through library noise.

3. Explain This Error in Plain English

Explain this error message like I've never seen it before. Ask mode.

<error>
[paste the error, or reference @Terminal / @Lint Errors]
</error>
Code it points at: @Files [path/to/file]

Do this:
- Translate the error into plain language: what it literally means.
- Explain why it's happening in THIS code specifically.
- List the 2-3 usual causes of this error, ranked for my case.
- Tell me the standard fix for each.

Why it works: Separating "what the error means" from "why it's happening here" teaches you the concept and pins it to your code at the same time.

4. Ask Agent to Reproduce It

Agent mode. Reproduce this bug before doing anything else.

Symptom: [what's wrong]. Steps: [how to trigger it].
Expected: [what should happen]. Actual: [what happens].
Reference @Terminal for the current output.

Do this:
- Write a small failing test (or run the repro steps) and confirm you actually see the bug.
- Stop and show me the reproduction. Don't attempt a fix yet.
- Tell me what the failing case proves about where the bug lives.

Best for: Locking the bug down first — a confirmed reproduction stops Agent from guessing at a fix that never addressed the real problem.

5. Add Logging to Narrow It Down

Agent mode. Help me narrow down where this goes wrong by adding logging.

The value is wrong by the time it reaches [point]. Code: @Files [path]

Do this:
- Add temporary log lines at the key steps so I can see where the data first becomes wrong.
- Label each log clearly and don't change any logic.
- Tell me exactly which command to run and what to look for in @Terminal.
- After I paste the output back, tell me the narrowed-down cause and remove the logs.

Why it works: Instrumenting the path shows where a value first goes bad, which is faster than reasoning about the whole flow at once.

Runtime & logic bugs

Five prompts for code that runs but produces the wrong result. The key signal is always expected-vs-actual — give Cursor both.

6. Expected vs Actual

This runs without crashing but produces the wrong result. Find out why.

Code: @Files [path/to/file]
Input: [the input I gave it].
Expected: [what it should return / do].
Actual: [what it actually returns / does].

Do this (Ask mode first):
- Trace the input through the code and show me where the actual diverges from the expected.
- Name the root cause.
- Give the smallest safe fix.
- Suggest one assertion or test that would have caught this.

Best for: Silent wrong answers — a concrete input plus expected-vs-actual gives Cursor a target to trace toward.

7. Wrong Output Value

The output value is wrong and I can't see why.

Code: @Files [path]
For input [X], it returns [wrong value] but should return [right value].

Do this:
- Compute the expected value by hand, step by step, and compare against what the code does at each step.
- Point to the exact line where the calculation goes wrong.
- Explain the mistake (wrong operator, bad rounding, unit mismatch, etc.).
- Fix only that line and confirm the input above now yields the right value.

Why it works: Asking Cursor to compute the answer by hand and compare step by step catches the exact line where the math or logic drifts.

8. Off-by-One / Edge Case

This works for normal input but fails on edges. Find the boundary bug.

Code: @Files [path]
It breaks when: [empty input / first or last element / single item / zero / max].

Do this:
- Check the loop bounds, index math, and range/slice logic for off-by-one errors.
- List every boundary the code should handle and whether it does.
- Give the fix.
- Add a quick test covering the edge above and re-run it to confirm it passes.

Best for: Boundary bugs — naming the failing edge focuses Cursor on index math instead of the happy path.

9. Async / Race Condition

I think this is an async or race-condition bug — it works sometimes.

Code: @Files [path]
Symptom: [e.g. stale data, missing await, out-of-order updates, double fire].

Do this (Ask mode):
- Trace the async flow: what's awaited, what isn't, and what runs concurrently.
- Identify any missing await, unhandled promise, or shared state written from two paths.
- Explain the exact interleaving that produces the bug.
- Give the fix that makes ordering deterministic, and note any others I should check.

Why it works: Asking for the specific interleaving that triggers the bug forces a real analysis of the concurrency, not a guess about a missing await.

10. State Not Updating in the UI

My UI isn't updating when the data changes. Find why.

Framework: [React / Vue / Svelte / etc.]. Component: @Files [path]
Expected: [what should re-render / show]. Actual: [what's stale or missing].

Do this:
- Check for state mutated in place instead of replaced, wrong or missing dependency arrays, stale closures, or keys that don't change.
- Point to the exact cause in this component.
- Give the idiomatic fix for this framework.
- Tell me how to verify the UI now updates.

Best for: "Why won't it re-render" bugs — the common causes are framework-specific, so naming the framework gets a precise answer.

Advertisement

Failing tests

Four prompts for red tests. Reference @Terminal for the failure output, and always make Cursor decide whether the test or the code is wrong before it edits.

11. Decide Test-vs-Code

A test is failing and I don't know if the test or the code is wrong. Ask mode.

Test: @Files [path/to/test]
Code under test: @Files [path/to/code]
Failure output: @Terminal

Do this:
- Tell me whether the bug is in the test or the code, and how you know.
- If the test is asserting the wrong thing, explain the correct expected behavior.
- If the code is wrong, name the root cause.
- Don't change anything yet — just give me the verdict and the reasoning.

Why it works: Forcing the verdict first stops Cursor from "fixing" a test that was correctly catching a real bug.

12. Fix a Failing Test

Agent mode. This test is failing because the code is wrong. Fix the code.

Test: @Files [path/to/test]
Code: @Files [path/to/code]
Failure: @Terminal

Do this:
- Make the smallest change to the code that makes this test pass for the right reason.
- Do not weaken or delete the test to make it green.
- Don't touch unrelated files.
- Re-run the test and confirm it passes; show me the diff of what you changed.

Best for: A known-good test catching a real bug — the "don't weaken the test" rule keeps the fix honest.

13. Fix a Flaky Test

This test passes sometimes and fails others. Make it deterministic.

Test: @Files [path/to/test]
Code it exercises: @Files [path]

Do this (Ask mode first):
- List the usual flakiness causes that could apply here: timing, test ordering, shared state, randomness, real clock/timezone, network.
- Rank which is most likely given this test.
- Then in Agent mode, apply the fix that removes the nondeterminism (fake timers, seed, isolate state, mock the network).
- Run the test several times to show it's now stable.

Why it works: A ranked checklist of flakiness causes narrows an intermittent failure far faster than re-running it and hoping.

14. Fix a Whole Failing Suite with Agent

Agent mode. Get the whole test suite green.

Run the tests, read @Terminal, and use @Codebase to find causes across files.

Do this:
- Run the suite and list every failing test grouped by likely shared cause.
- Outline your plan before editing — which failures come from one root cause vs. many.
- Fix the code (not the tests) one group at a time, re-running after each.
- Ask before deleting or skipping any test.
- Finish only when the full suite passes; summarize what was broken and why.

Best for: A suite broken by one regression — grouping failures by shared cause turns twenty red tests into one or two real fixes.

Type & build errors

Four prompts for the errors that stop you compiling. Reference @Lint Errors and @Terminal so Cursor reads the exact diagnostics.

15. Fix a TypeScript Type Error

Fix this TypeScript error properly — no "any", no @ts-ignore.

Error: @Lint Errors (or paste it below)
<error>
[paste the TS error if not using @Lint Errors]
</error>
Code: @Files [path]

Do this:
- Explain what the type error is actually telling me.
- Fix it by making the types correct, not by silencing the checker.
- If the type reveals a real bug (a value that can be undefined/null), fix that too.
- Keep runtime behavior identical and show the diff.

Why it works: Banning any and @ts-ignore up front forces a real type fix instead of hiding the error the compiler was right about.

16. Fix a Build / Compile Failure

Agent mode. My build is failing. Diagnose and fix it.

Read @Terminal for the full build output.
Build command: [e.g. npm run build].

Do this:
- Identify the first real error in the output (ignore downstream noise it caused).
- Explain what's failing and why.
- Apply the fix — config, code, or dependency as appropriate.
- Re-run the build and confirm it completes clean. Don't touch unrelated config.

Best for: Noisy build logs — telling Cursor to find the first real error stops it chasing cascade failures.

17. Resolve a Dependency / Version Conflict

Ask mode. I have a dependency/version conflict. Help me resolve it safely.

Error: @Terminal
Package manager: [npm / pnpm / yarn / pip / etc.].
Relevant manifest: @Files [package.json / lockfile / requirements]

Do this:
- Explain which packages conflict and why (peer dep, incompatible ranges, transitive pin).
- Give me the safest resolution: which version to pin/upgrade and the exact command.
- Warn me about any breaking changes that upgrade introduces.
- List what I should test after applying it before I trust the fix.

Why it works: Version conflicts have real trade-offs, so getting the reasoning and the risks in Ask mode beats letting Agent bump packages blindly.

18. Fix Import / Module-Resolution Errors

Fix this "cannot find module" / import-resolution error.

Error: @Lint Errors / @Terminal
Failing import is in: @Files [path]

Do this:
- Tell me whether it's a wrong path, a missing package, a misconfigured alias/paths, or an ESM/CommonJS mismatch.
- Check the tsconfig/jsconfig paths and the build config for the alias if one is used.
- Give the exact fix (correct path, install command, or config change).
- Confirm the import resolves and nothing else broke.

Best for: The "it's right there, why can't it find it" class of error — Cursor checks aliases and config, not just the path.

Advertisement

Performance

Three prompts for code that's correct but slow. Give Cursor the scale and the current timing so it optimizes the right thing.

19. Find the Bottleneck

Ask mode. Find why this is slow before I change anything.

Code: @Files [path]
Context: it handles [scale, e.g. 100k rows / N requests] and takes [current time].

Do this:
- Identify the likely bottleneck(s) with reasoning about time/space complexity.
- Rank the fixes by impact vs. effort.
- Give the single highest-impact change as concrete code.
- Warn me about any correctness or memory trade-off it introduces.

Why it works: Reasoning about complexity before touching code targets the real hot path instead of micro-optimizing a line that doesn't matter.

20. Fix an N+1 Query

I think this has an N+1 query problem. Find and fix it.

ORM/DB layer: [Prisma / ActiveRecord / SQLAlchemy / raw SQL].
Code: @Files [path]

Do this:
- Show me where a query runs inside a loop, causing one query per row.
- Rewrite it to batch/eager-load/join so it's a fixed number of queries.
- Keep the returned data shape identical.
- Estimate the query-count reduction and note anything to verify (correct joins, no duplicates).

Best for: Slow list/index endpoints — naming the ORM lets Cursor use the right eager-loading idiom for your stack.

21. Reduce Re-renders / Memory

This is re-rendering too often / using too much memory. Fix it.

Framework/runtime: [React / etc.]. Code: @Files [path]
Symptom: [janky UI / growing memory / repeated expensive work].

Do this:
- Identify what's causing the extra renders or retained memory (new object/function identities each render, missing memoization, unbounded caches, leaked listeners/subscriptions).
- Apply the targeted fix without changing behavior.
- Make sure you're not over-memoizing things that don't need it.
- Tell me how to confirm the improvement.

Why it works: Naming both the cause and the anti-pattern ("don't over-memoize") gets a proportionate fix instead of scattering memo everywhere.

Prevent recurrence

Three prompts to make sure the same bug can't come back. A fix isn't done until there's a test that would fail on the old code.

22. Add a Regression Test for the Bug

Agent mode. We just fixed a bug — now lock it in with a regression test.

The bug was: [describe it]. The fix was: [describe it, or reference @Git for the diff].
Code: @Files [path]

Do this:
- Write a test that reproduces the ORIGINAL bug — it must fail on the old code and pass on the fixed code.
- Verify this by mentally (or actually) reverting the fix: the test should go red.
- Name the test after the behavior it protects.
- Run it and confirm it passes now. Don't touch unrelated tests.

Why it works: A test that fails on the old code is proof the fix is real and a guard so the bug can't silently return.

23. Add Validation / Guardrails

This bug happened because bad input got in unchecked. Add guardrails.

Code: @Files [path]
The bad input that caused it: [describe it].

Do this:
- Add validation at the boundary where this input enters (function arg, API payload, env var).
- Fail fast with a clear error instead of letting the bad value propagate.
- Don't over-validate deep internals that a boundary check already covers.
- Add a test for the rejected-input case and re-run it.

Best for: Bugs caused by bad data — validating at the boundary stops the same class of input from ever reaching the logic again.

24. Write a Rule So Agent Avoids the Pattern

We keep hitting this bug. Write a Cursor rule so Agent stops introducing it.

The recurring mistake: [describe the pattern, e.g. "forgetting to await DB calls" or "mutating state directly"].

Do this:
- Draft a .cursor/rules/[name].mdc file that tells Cursor to avoid this pattern and what to do instead.
- Scope it with the right glob (e.g. matches [src/**/*.ts]) and set it to Auto Attached.
- Keep it short and specific — one clear do/don't, with a tiny example.
- Explain where to save it so it applies to every future Ask/Agent/inline run.

Why it works: A .cursor/rules file gets injected into every future run, so a fix you make once becomes a standing guardrail Cursor applies automatically. See more of these in the Agent mode prompts.

The through-line for all 24: give Cursor the error, the code, and expected-vs-actual, make Agent reproduce before it fixes, and require it to re-run the tests to confirm. For the full toolkit, keep the best Cursor prompts handy and pair debugging with solid test-writing prompts.

Frequently Asked Questions

What's the best Cursor mode for debugging?

Use Agent mode for anything you want fixed end to end — it can read @Terminal and @Lint Errors, edit across files, run your tests, read the output, and iterate until they pass. Use Ask mode when you only want a diagnosis or an explanation without any edits, for example to understand a stack trace or decide whether the test or the code is wrong before you commit to a fix.

Should I paste the full stack trace?

Yes. Paste the complete error and stack trace, or reference @Terminal and @Lint Errors so Cursor reads them directly. The trace names the file, line, and call path, which is the strongest signal for pinpointing the root cause. Trimming it to the top line throws away the context that tells Cursor where the failure actually originated.

How does Cursor use @Terminal and @Lint Errors?

@Terminal pulls your recent terminal output — including the failing command and its error — straight into the chat, and @Lint Errors pulls the current diagnostics from the editor. Referencing them means you don't have to copy and paste; Cursor reads the live error state, and in Agent mode it can re-run the command and re-check lint after a fix to confirm the error is gone.

How do I make Agent reproduce the bug before fixing it?

Tell it to. Add an instruction like "reproduce first with a failing test, confirm you see the bug, then fix it and re-run the test to prove it passes." Making reproduction the first acceptance criterion stops Agent from guessing at a fix, and the failing-then-passing test is proof the change actually addressed the reported behavior.

How do I fix a bug that spans many files?

Use Agent mode with @Codebase so it can search the whole repo, and give it the entry point — the error, the @Terminal output, or the @Git diff where the regression appeared. Ask it to trace the call path across files and outline a plan before editing. Reviewing the per-file diff before you accept keeps a multi-file fix from touching things it shouldn't.

How do I stop Cursor fixing symptoms instead of the root cause?

Ask for the root cause explicitly and forbid band-aids: "find the underlying cause, don't just silence the symptom, don't swallow the error or loosen types to make it pass." Have it explain why the bug happens before it edits, and require a regression test that would fail on the old code. That forces a real fix rather than a catch block that hides the problem.

Advertisement