Skip to content

Commit d8f6f7f

Browse files
koriymclaude
andcommitted
fix: Suppress script output in JSON mode for clean parsing
Resolves Issue #28 (Issue 1) by making --json output parseable with jq and other JSON tools. Problem: - Script stdout/stderr mixed with JSON output - Broke jq parsing: `./bin/xdebug-profile --json -- php script.php | jq` - Prevented piping to other JSON processing tools - Common error: "parse error: Invalid numeric literal" Solution: - In JSON mode, redirect script output to /dev/null - Keep exit code for error detection - Use passthru() for human mode (show script output) - Use exec() for JSON mode (suppress script output) Benefits: - ✅ Clean JSON output for jq: `... | jq '.["🎯 bottleneck_functions"]'` - ✅ MCP-compatible: pure JSON response - ✅ Tool-friendly: easy to integrate with other tools - ✅ Backward compatible: human mode unchanged Changes: - bin/xdebug-profile: Conditional output suppression in JSON mode - docs/TROUBLESHOOTING.md: Added JSON parsing issue and solution - Updated --help text with clear behavior explanation - Added jq usage example to help text 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ef01bf0 commit d8f6f7f

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

‎bin/xdebug-profile‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function showHelp(string $scriptName): void
2828
echo " ✅ Comprehensive metrics: CPU time, memory, function calls, I/O operations\n";
2929
echo "\n";
3030
echo "CORE OPTIONS:\n";
31-
echo " --json AI-optimized JSON output with comprehensive metrics\n";
31+
echo " --json AI-optimized JSON output (suppresses script output for clean parsing)\n";
3232
echo " --claude Auto-analyze profile data with Claude AI\n";
3333
echo " --context=TEXT Add contextual description for AI analysis\n";
3434
echo " --include-vendor=PATTERNS Include vendor packages in performance analysis\n";
@@ -51,6 +51,9 @@ function showHelp(string $scriptName): void
5151
echo " # 5. Test suite performance optimization\n";
5252
echo " $scriptName --context=\"PHPUnit test suite performance\" -- php vendor/bin/phpunit\n";
5353
echo "\n";
54+
echo " # 6. JSON output for tooling (clean parsing with jq)\n";
55+
echo " $scriptName --json -- php script.php | jq '.\"🎯 bottleneck_functions\"'\n";
56+
echo "\n";
5457
echo "PERFORMANCE ANALYSIS CAPABILITIES:\n";
5558
echo " 🚀 Function Timing: Identify slowest functions with microsecond precision\n";
5659
echo " 💾 Memory Analysis: Track memory usage patterns and identify leaks\n";
@@ -60,7 +63,8 @@ function showHelp(string $scriptName): void
6063
echo "\n";
6164
echo "OUTPUT FORMATS:\n";
6265
echo " Default: Human-readable with proper time/memory units and KCachegrind suggestions\n";
63-
echo " --json: AI-optimized JSON with emoji keys and comprehensive performance metrics\n";
66+
echo " --json: Pure JSON output (script stdout/stderr suppressed for clean parsing with jq)\n";
67+
echo " AI-optimized with emoji keys and comprehensive performance metrics\n";
6468
echo " Schema: https://koriym.github.io/xdebug-mcp/schemas/xdebug-profile.json\n";
6569
echo " Benefits: AI can rank bottlenecks, suggest optimizations, compare before/after\n";
6670
echo "\n";
@@ -158,9 +162,16 @@ if ($xdebugFlag !== '') {
158162
$allArgs = array_merge($xdebugOptions, [$targetFile], $phpArgs);
159163
$cmd = 'php ' . implode(' ', array_map('escapeshellarg', $allArgs));
160164

161-
// Execute with passthru to show output
165+
// Execute command
162166
$exitCode = 0;
163-
passthru($cmd, $exitCode);
167+
if ($jsonOutput) {
168+
// JSON mode: suppress script output for clean JSON parsing
169+
$cmd .= ' > /dev/null 2>&1';
170+
exec($cmd, $output, $exitCode);
171+
} else {
172+
// Human-readable mode: show script output
173+
passthru($cmd, $exitCode);
174+
}
164175

165176
// Generate profile analysis
166177
try {

‎docs/TROUBLESHOOTING.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,24 @@ rm /tmp/trace.*.xt /tmp/cachegrind.out.*
215215

216216
**Issue**: Output not in expected format or missing data
217217

218+
**Common Problem - Script Output Mixing with JSON**:
219+
```bash
220+
# ❌ Before v0.x.x: Script output breaks jq parsing
221+
./bin/xdebug-profile --json -- php script.php | jq '.'
222+
# parse error: Invalid numeric literal at line 1, column 8
223+
224+
# ✅ Now: Clean JSON output (script stdout/stderr automatically suppressed)
225+
./bin/xdebug-profile --json -- php script.php | jq '.["🎯 bottleneck_functions"]'
226+
```
227+
218228
**Format Solutions**:
219229
```bash
220230
# Ensure JSON output for AI processing
221231
./bin/xdebug-profile --json -- php script.php
222232

233+
# Pipe to jq for filtering
234+
./bin/xdebug-profile --json -- php script.php | jq '.["⏱️ execution_time_ms"]'
235+
223236
# Verify schema compliance
224237
./bin/validate-profile-json profile-output.json
225238

0 commit comments

Comments
 (0)