Debug Code
Debug and Fix a Failing Piece of Code
Get a systematic diagnosis of a bug: likely cause, minimal fix, and the test that proves it.
The Prompt
debug-and-fix-code-issues.md
Act as a senior engineer doing a focused debugging session. Do not rewrite the whole file. Language / framework: {{language}} What the code should do: {{expected_behaviour}} What actually happens: {{actual_behaviour}} Error message or stack trace: {{error}} Relevant code: ``` {{code}} ``` What I already tried: {{attempts}} Respond in this order: 1. Diagnosis: the most likely root cause, and why the symptom matches it 2. Two alternative causes I should rule out, with a one-line check for each 3. Minimal fix as a diff-style snippet, changing as few lines as possible 4. Why the fix works 5. A test that fails before the fix and passes after 6. Any other place in the shown code with the same class of bug If the code provided is insufficient, say exactly what you need before guessing.
Negative prompt
Included when you copy the prompt
Do not rewrite the whole file or invent a new architecture. Do not propose multiple unrelated refactors alongside the fix. Do not invent APIs, types, or library behaviour that are not in the provided code or error. Do not skip the diagnosis and jump straight to a speculative patch. Do not suggest "try restarting" or vague environment checks as the primary answer when the code shows a clear logic bug. Do not omit the failing-then-passing test. Do not claim certainty when the snippet is incomplete - ask for the missing piece instead.
Fill in your details below - this prompt updates as you type, then copy it.
Fill in your details
0 of 6 filled. Preview updates as you type - then copy the finished prompt (negative prompt included when present).
debug-and-fix-code-issues.md
Prompt preview
Act as a senior engineer doing a focused debugging session. Do not rewrite the whole file. Language / framework: {{language}} What the code should do: {{expected_behaviour}} What actually happens: {{actual_behaviour}} Error message or stack trace: {{error}} Relevant code: ``` {{code}} ``` What I already tried: {{attempts}} Respond in this order: 1. Diagnosis: the most likely root cause, and why the symptom matches it 2. Two alternative causes I should rule out, with a one-line check for each 3. Minimal fix as a diff-style snippet, changing as few lines as possible 4. Why the fix works 5. A test that fails before the fix and passes after 6. Any other place in the shown code with the same class of bug If the code provided is insufficient, say exactly what you need before guessing.
Examples
Example input
Language / framework: TypeScript + React 18 Expected behaviour: Fetch user profile once when the component mounts and show the name. Actual behaviour: Network tab shows two identical GET /api/me calls; sometimes the UI flickers. Error: (none) Code: useEffect(() => { let ignore = false; fetch("/api/me") .then((r) => r.json()) .then((data) => { if (!ignore) setUser(data); }); }, [userId]); Attempts: Wrapped fetch in useCallback; still double-fires in Strict Mode.
What you should get
1. Diagnosis: The effect depends on userId but also re-runs under React 18 Strict Mode in development, which mounts → unmounts → remounts. Without an AbortController (or equivalent), both mounts start a fetch. Symptom matches: duplicate GETs, no thrown error. 2. Alternatives to rule out: - Parent remounting the whole tree → log a mount counter in the parent. - userId changing identity every render → log userId before the effect. 3. Minimal fix: ```diff - useEffect(() => { - let ignore = false; - fetch("/api/me") - .then((r) => r.json()) - .then((data) => { if (!ignore) setUser(data); }); - }, [userId]); + useEffect(() => { + const controller = new AbortController(); + fetch(`/api/me?userId=${userId}`, { signal: controller.signal }) + .then((r) => r.json()) + .then((data) => setUser(data)) + .catch((err) => { if (err.name !== "AbortError") throw err; }); + return () => controller.abort(); + }, [userId]); ``` 4. Why it works: Cleanup aborts the in-flight request from the first Strict Mode mount so only the surviving mount's result is applied. 5. Test: Render with a mocked fetch; assert fetch called once after act + cleanup, and aborted request does not call setUser. 6. Same class of bug: Any other effect that writes state from async work without abort/ignore on cleanup.
How to get a good result
Done when
1. Diagnosis: The effect depends on userId but also re-runs under React 18 Strict Mode in development, which mounts → unmounts → remounts. Without an AbortController (or equivalent), both mounts start a fetch. Symptom matches: duplicate GETs, no thrown error. 2. Alternatives to rule out: - Parent remounting the whole tree → log a mount counter in the parent. - userId changing identity every render → log userId before the effect. 3. Minimal fix: ```diff - useEffect(() => { - let ignore = false; - fetch("/api/me") - .then((r) => r.json()) - .then((data) => { if (!ignore) setUser(data); }); - }, [userId]); + useEffect(() => { + const controller = new AbortController(); + fetch(`/api/me?userId=${userId}`, { signal: controller.signal }) + .then((r) => r.json()) + .then((data) => setUser(data)) + .catch((err) => { if (err.name !== "AbortError") throw err; }); + return () => controller.abort(); + }, [userId]); ``` 4. Why it works: Cleanup aborts the in-flight request from the first Strict Mode mount so only the surviving mount's result is applied. 5. Test: Render with a mocked fetch; assert fetch called once after act + cleanup, and aborted request does not call setUser. 6. Same class of bug: Any other effect that writes state from async work without abort/ignore on cleanup.
FAQ
- What do I fill in before I copy this prompt?
- Replace these placeholders with your details: language, expected_behaviour, actual_behaviour, error, code, attempts. Required fields are marked on the page. The prompt text updates as you type.
- Which AI tools is this prompt written for?
- It works best with ChatGPT, Claude, Gemini, Cursor. Copy the filled prompt and paste it into the tool you already use.
- What should a good result look like?
- 1. Diagnosis: The effect depends on userId but also re-runs under React 18 Strict Mode in development, which mounts → unmounts → remounts. Without an AbortController (or equivalent), both mounts start a fetch. Symptom matches: duplicate GETs, no thrown error. 2. Alternatives to rule out: - Parent remounting the whole tree → log a mount counter in the parent. - userId changing identity every render → log userId before the effect. 3. Minimal fix: ```diff - useEffect(() => { - let ignore =…
Best for
- Developers
- Students
- Technical founders
Related Prompts
More prompts like this — same topic and style.

