Skip to content
看累了听个音乐吧

4.2 修复 Bug

4.2 修复 Bug

修 bug 是 Claude Code 最擅长的事之一

原因很简单:修 bug 是一个有明确输入(报错/问题描述)、明确输出(测试通过)的任务,非常适合 Agentic Loop 的工作方式。

但效果好不好,很大程度上取决于你给它的上下文够不够


情况一:有明确报错信息

这是最理想的情况。直接把报错信息粘进去:

I'm getting this error when I run the test suite:

TypeError: Cannot read properties of undefined (reading 'id')
    at UserService.getProfile (src/services/UserService.ts:47:23)
    at async ProfileController.show (src/controllers/ProfileController.ts:23:18)

the error happens when fetching a profile for a user who has
never logged in before. fix it and verify the fix with tests.

注意 prompt 里做了几件事:

  1. 粘贴了完整的报错和调用栈——让它知道错误在哪里
  2. 描述了复现条件——"从未登录过的用户"
  3. 明确了验收标准——"用测试验证"

Claude Code 会根据调用栈找到 UserService.ts 第 47 行,读取前后逻辑,判断为什么 user 可能是 undefined,修复,然后跑测试确认。


情况二:没有报错,只有"行为不对"

这类 bug 更难描述,需要你说清楚"期望行为"和"实际行为":

there's a bug in the shopping cart: when a user adds the same item
twice, the quantity should increase by 1, but instead a duplicate
item is added.

expected: cart shows item with quantity 2
actual: cart shows two separate items with quantity 1 each

the relevant code is probably in src/store/cartSlice.ts.
reproduce the issue, find the root cause, and fix it.

关键结构:

  • 是什么行为(adding the same item twice)
  • 期望什么结果(quantity increases)
  • 实际得到什么(duplicate items)
  • 可能在哪里(给它一个方向,不是必须的但能省时间)
  • 要求它自己复现(不只是猜,要找到根因)

真实演示:在 express 项目里修一个 bug

我们继续用 express 仓库来演示。假设我们发现一个问题:路由匹配对于带有多余斜杠的路径行为不一致。

I noticed that Express routes behave inconsistently with trailing slashes.
For example, a route defined as /users/:id matches /users/123 but not
/users/123/.

look at the router implementation in lib/router/ and check if this
is expected behavior or a bug. if it's a bug, propose a fix.
if it's by design, explain why.

这个 prompt 的好处是它没有假设"一定是 bug"——让 Claude Code 先判断,再决定怎么处理。有时候"行为不符合你的预期"其实是 by design,强行改反而会破坏其他东西。


修完了,你要做的事

Claude Code 修完 bug 之后,不要无脑点 Accept

你需要:

1. 看 diff,理解它改了什么

explain the changes you made and why this fixes the issue.
are there any edge cases this fix might not handle?

2. 确认测试真的覆盖了这个 bug

does the test you wrote actually reproduce the original bug before the fix?
run the test against the unfixed version to confirm it fails,
then verify it passes after the fix.

这个验证步骤很重要——AI 有时候会写一个"通过但没有真正测试这个 bug"的测试。

3. 想想有没有相关的隐患

are there similar patterns elsewhere in the codebase that might
have the same bug? do a quick scan.

一条反直觉的建议

遇到 bug 的时候,很多人的第一反应是:直接告诉 Claude Code "帮我修这个 bug"。

但有时候更好的做法是先问:

before fixing anything, analyze this bug:
what is the root cause? what are my options for fixing it?
what are the tradeoffs of each approach?

让它先分析,再动手——尤其是在复杂系统里,第一个想到的修法往往不是最好的修法。


常见 bug 场景的 prompt 模板

异步/Promise 问题:

I'm getting an unhandled promise rejection in production logs:
[paste error]
the code is in [file path]. trace the async flow and fix the issue.
make sure error handling is added at the right level.

性能问题:

this API endpoint is taking 3+ seconds to respond.
the endpoint is in [file path].
profile it, identify the bottleneck, and suggest optimizations.
don't change behavior, only improve performance.

类型错误(TypeScript):

TypeScript is throwing this error: [paste error]
I don't want to just suppress it with 'as any' or '@ts-ignore'.
find the real cause and fix the types properly.

基于 CC BY-NC-SA 4.0 协议发布