Claude Codeによるデバッグ
中級 7 min
デバッグは開発において最も時間のかかる作業の一つであり、Claude Codeはその体験を一変させます。手動でスタックトレースを追跡してconsole.logを追加する代わりに、症状を説明すればClaudeが体系的にログを分析し、問題を再現し、根本原因を特定し、修正を実装します。このガイドでは、単純な型エラーから複雑な競合状態(race condition)まで、あらゆる場面に対応できるAI支援デバッグの体系的アプローチを紹介します。
デバッグエラーログトラブルシューティング
体系的デバッグアプローチ
Effective debugging with Claude Code follows a four-step process: describe symptoms, gather evidence, isolate the cause, and verify the fix. The key difference from manual debugging is that you provide the observations and Claude handles the analysis. This division of labor plays to each party's strengths.
When describing symptoms to Claude, be as specific as possible about what you observe versus what you expect. 'The API returns 500' is less useful than 'the POST /api/orders endpoint returns 500 with error "Cannot read property id of undefined" when the request body includes a discount code, but works fine without one.' The additional context often points Claude directly at the root cause.
Provide Claude with the full error output, including stack traces, relevant log entries, and any recent changes that might be related. Claude excels at correlating information across multiple sources. A stack trace plus a recent git diff plus a failing test output gives Claude enough data to pinpoint the issue in most cases.
# Step 1: Describe the symptom clearly
claude "POST /api/orders returns 500 when the request includes a discountCode field.
Error: TypeError: Cannot read property 'id' of undefined
at applyDiscount (src/services/OrderService.ts:47)
The endpoint works fine without the discountCode field.
This started happening after yesterday's deploy."
# Step 2: Let Claude gather evidence
# Claude will read the relevant files and trace the logic
# Step 3: Claude identifies the root cause
# "The discountCode lookup returns null for expired codes,
# but applyDiscount doesn't check for null before accessing .id"
ログ分析とエラートレース
Claude Code is exceptionally good at analyzing log files and error traces. Where a human might need minutes to scan through hundreds of log lines, Claude can process the entire output and extract the relevant entries instantly. Feed it your application logs, server logs, or build output, and ask it to identify the root cause.
For complex issues that span multiple services, provide logs from each relevant service. Claude can correlate timestamps, request IDs, and error codes across services to trace the path of a failing request. This is particularly valuable for debugging microservice architectures where the root cause in one service manifests as an error in another.
When dealing with intermittent issues, provide Claude with logs from both successful and failing requests. Asking Claude to 'compare these two request logs and identify what differs' often reveals environment-specific factors, race conditions, or data-dependent bugs that are hard to spot manually.
# Feed error logs directly to Claude
claude "analyze this error log and identify the root cause:" < error.log
# Compare successful and failing request logs
claude "here are logs from a successful request and a failing request.
Compare them and identify what causes the failure:
SUCCESS: $(cat logs/success.log)
FAILURE: $(cat logs/failure.log)"
# Analyze build failures
npm run build 2>&1 | claude "analyze these build errors, identify the root cause, and suggest fixes in order of priority"
# Trace a request across services
claude "trace request-id abc123 across these service logs and identify where it fails:
--files gateway.log auth-service.log order-service.log"
問題の再現
Before fixing a bug, you need to reproduce it reliably. Claude Code can help by analyzing the conditions under which the bug occurs and generating minimal reproduction cases. This is especially valuable for bugs that are hard to reproduce locally, such as those triggered by specific data patterns, timing conditions, or edge cases.
Ask Claude to generate a test that reproduces the bug. Start with 'write a test that demonstrates the bug in the applyDiscount function when given an expired discount code.' If Claude writes the test and it passes (meaning the bug is not reproduced), provide additional context about the environment or data conditions. Iterate until the test fails, confirming the reproduction.
For UI bugs, Claude can help create specific application states that trigger the issue. Describe the user flow that leads to the bug, and Claude can generate test fixtures, mock data, or Playwright scripts that set up the exact conditions needed for reproduction.
# Generate a reproduction test
claude "write a test that reproduces this bug:
- OrderService.applyDiscount throws when discountCode is expired
- Expected: return order without discount
- Actual: TypeError on discount.id access
Use our test setup from tests/setup.ts and factories from tests/factories/"
# Generate specific test data
claude "create test fixtures that trigger the edge case:
- user with multiple addresses where default is deleted
- this causes a null reference in getShippingAddress"
# Create a Playwright script for UI reproduction
claude "write a Playwright test that reproduces:
1. Login as test user
2. Add 3 items to cart
3. Apply expired promo code 'SUMMER2024'
4. Click checkout - should show error, but gets blank screen"
修正と検証
Once the root cause is identified and a reproduction test exists, Claude Code can implement the fix and verify it in a single workflow. The reproduction test serves as a built-in verification: when the test passes after the fix, you have confidence the bug is resolved.
Ask Claude to implement the fix and run the reproduction test. Claude will make the code change, execute the test, and report the result. If the test still fails, Claude can iterate on the fix without additional prompting. This tight feedback loop often resolves bugs faster than manual debugging because Claude can try multiple approaches in rapid succession.
After the immediate fix, ask Claude to check for similar patterns elsewhere in the codebase. A bug in one place often indicates the same pattern exists in other locations. Claude can scan the entire codebase for the vulnerable pattern and apply preventive fixes before they become production incidents.
# Fix and verify in one workflow
claude "fix the null reference bug in applyDiscount, then run the reproduction test to verify"
# Check for similar patterns across the codebase
claude "search the entire codebase for other places where we access .id
on a database lookup result without null-checking first.
List all occurrences and fix them."
# Add regression prevention
claude "add a TypeScript strict null check rule and update the OrderService
types so discount can be null, forcing compile-time null checks"
# Generate comprehensive test coverage for the fix
claude "add edge case tests for applyDiscount covering:
- valid discount code
- expired discount code
- non-existent discount code
- already-used discount code
- discount that exceeds order total"
実行プレビュー
Claude Codeによるデバッグ
Claude Codeによるデバッグについて
Claude Codeガイドは、Claude Codeの特定の側面をマスターするための詳細なステップバイステップガイドです。Claude Codeによるデバッグは中級レベルのガイドで、日常のワークフローでClaude Codeを最大限に活用するためのベストプラクティス、実践的なテクニック、実用的なヒントを紹介します。