diff --git a/src/cli/simple-commands/fix-hook-variables.js b/src/cli/simple-commands/fix-hook-variables.js index f7ad7b2c71..51695e5724 100644 --- a/src/cli/simple-commands/fix-hook-variables.js +++ b/src/cli/simple-commands/fix-hook-variables.js @@ -21,7 +21,12 @@ const VARIABLE_SYNTAXES = { 'environment': { pattern: /\$(\w+)/g, example: '$CLAUDE_FILE', - description: 'Environment variable syntax' + description: 'Environment variable syntax (unverified)' + }, + 'jq': { + pattern: null, + example: 'jq parsing of JSON input', + description: 'Official Claude Code approach using jq' }, 'wrapper': { pattern: null, @@ -61,6 +66,26 @@ function transformHookCommand(command, fromSyntax, toSyntax) { }); } + if (fromSyntax === 'legacy' && toSyntax === 'jq') { + // Transform to use jq parsing of JSON input + // Extract the actual command and wrap it with jq parsing + const fileVarMatch = command.match(/\$\{file\}/); + const commandVarMatch = command.match(/\$\{command\}/); + + if (fileVarMatch) { + // Replace ${file} with jq extraction + const baseCommand = command.replace(/\$\{file\}/g, '{}'); + return `cat | jq -r '.tool_input.file_path // .tool_input.path // ""' | xargs -I {} ${baseCommand}`; + } else if (commandVarMatch) { + // Replace ${command} with jq extraction + const baseCommand = command.replace(/\$\{command\}/g, '{}'); + return `cat | jq -r '.tool_input.command // ""' | xargs -I {} ${baseCommand}`; + } + + // Fallback for other variables + return `cat | jq -r '.' | xargs -I {} ${command.replace(/\$\{(\w+)\}/g, '{}')}`; + } + if (toSyntax === 'wrapper') { // Generate wrapper script path const scriptName = command.includes('post-edit') ? 'post-edit-hook.sh' : @@ -302,7 +327,7 @@ export const fixHookVariablesCommandConfig = { usage: 'fix-hook-variables [settings-file...]', options: [ { flag: '--no-backup', description: 'Skip creating backup files' }, - { flag: '--syntax ', description: 'Force specific syntax: environment, wrapper' }, + { flag: '--syntax ', description: 'Force specific syntax: environment, jq, wrapper' }, { flag: '--test', description: 'Create test hook configuration' } ], examples: [ @@ -320,9 +345,13 @@ This command will: • Create wrapper scripts if needed • Backup original settings files -Known working syntaxes: - • Environment variables: $CLAUDE_EDITED_FILE, $CLAUDE_COMMAND - • Wrapper scripts: Intercept and pass variables correctly +Available syntaxes: + • environment: Use environment variables like $CLAUDE_EDITED_FILE (unverified) + • jq: Use official jq JSON parsing approach (recommended) + • wrapper: Create wrapper scripts to handle variables + +Note: The 'jq' syntax is based on official Claude Code documentation and is likely +the most reliable approach for Claude Code 1.0.51+. For more information: https://github.com/ruvnet/claude-flow/issues/249` }; \ No newline at end of file diff --git a/tests/manual/test-all-hook-approaches.json b/tests/manual/test-all-hook-approaches.json new file mode 100644 index 0000000000..b6b0969669 --- /dev/null +++ b/tests/manual/test-all-hook-approaches.json @@ -0,0 +1,27 @@ +{ + "hooks": { + "PostToolUse": [ + { + "matcher": "Write|Edit", + "hooks": [ + { + "type": "command", + "command": "echo \"=== HOOK TEST $(date) ===\" >> .claude/hook-test-all.log" + }, + { + "type": "command", + "command": "echo \"1. ENV VARS - CLAUDE_EDITED_FILE: '$CLAUDE_EDITED_FILE', CLAUDE_FILE: '$CLAUDE_FILE'\" >> .claude/hook-test-all.log" + }, + { + "type": "command", + "command": "echo \"2. ALL ENV VARS:\" >> .claude/hook-test-all.log && env | grep -i claude >> .claude/hook-test-all.log || echo \"No CLAUDE env vars found\" >> .claude/hook-test-all.log" + }, + { + "type": "command", + "command": "echo \"3. STDIN CONTENT:\" >> .claude/hook-test-all.log && cat >> .claude/hook-test-all.log" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/tests/manual/test-env-vars-hook.json b/tests/manual/test-env-vars-hook.json new file mode 100644 index 0000000000..a5f8229b72 --- /dev/null +++ b/tests/manual/test-env-vars-hook.json @@ -0,0 +1,15 @@ +{ + "hooks": { + "PostToolUse": [ + { + "matcher": "Write|Edit", + "hooks": [ + { + "type": "command", + "command": "echo \"ENV TEST - CLAUDE_EDITED_FILE: $CLAUDE_EDITED_FILE, CLAUDE_FILE: $CLAUDE_FILE, FILE: $FILE\" >> .claude/env-test.log" + } + ] + } + ] + } +} \ No newline at end of file diff --git a/tests/manual/test-hook-variables.sh b/tests/manual/test-hook-variables.sh new file mode 100755 index 0000000000..685235c9b1 --- /dev/null +++ b/tests/manual/test-hook-variables.sh @@ -0,0 +1,113 @@ +#!/bin/bash +# Test script for Claude Code hook variables +# This script helps determine which variable approach works with your Claude Code version + +echo "🧪 Claude Code Hook Variable Test" +echo "=================================" +echo "" +echo "This script will help you test which hook variable approach works with your Claude Code version." +echo "" + +# Check Claude Code version +echo "📌 Claude Code Version:" +claude --version 2>/dev/null || echo "Claude Code not found in PATH" +echo "" + +# Clean up previous test logs +rm -f .claude/hook-test-*.log + +# Test 1: Environment Variables +echo "Test 1: Environment Variables Approach" +echo "--------------------------------------" +cat > .claude/test-env-vars.json << 'EOF' +{ + "hooks": { + "PostToolUse": [ + { + "matcher": "Write", + "hooks": [ + { + "type": "command", + "command": "echo \"[ENV_VAR_TEST] File: $CLAUDE_EDITED_FILE\" >> .claude/hook-test-env.log" + } + ] + } + ] + } +} +EOF +echo "✅ Created .claude/test-env-vars.json" +echo "" + +# Test 2: JQ JSON Parsing +echo "Test 2: JQ JSON Parsing Approach" +echo "--------------------------------" +cat > .claude/test-jq.json << 'EOF' +{ + "hooks": { + "PostToolUse": [ + { + "matcher": "Write", + "hooks": [ + { + "type": "command", + "command": "cat | jq -r '\"[JQ_TEST] File: \\(.tool_input.file_path // .tool_input.path // \\\"NONE\\\")\"' >> .claude/hook-test-jq.log 2>&1 || echo \"[JQ_TEST] Error parsing JSON\" >> .claude/hook-test-jq.log" + } + ] + } + ] + } +} +EOF +echo "✅ Created .claude/test-jq.json" +echo "" + +# Test 3: Comprehensive Test +echo "Test 3: Comprehensive Test (All Approaches)" +echo "------------------------------------------" +cat > .claude/test-comprehensive.json << 'EOF' +{ + "hooks": { + "PostToolUse": [ + { + "matcher": "Write", + "hooks": [ + { + "type": "command", + "command": "echo \"=== COMPREHENSIVE TEST $(date) ===\" >> .claude/hook-test-comprehensive.log" + }, + { + "type": "command", + "command": "echo \"ENV: CLAUDE_EDITED_FILE='$CLAUDE_EDITED_FILE'\" >> .claude/hook-test-comprehensive.log" + }, + { + "type": "command", + "command": "echo \"STDIN:\" >> .claude/hook-test-comprehensive.log && cat >> .claude/hook-test-comprehensive.log" + } + ] + } + ] + } +} +EOF +echo "✅ Created .claude/test-comprehensive.json" +echo "" + +echo "📋 Instructions:" +echo "1. Copy one of the test files to .claude/settings.json:" +echo " cp .claude/test-env-vars.json .claude/settings.json" +echo "" +echo "2. Open Claude Code and create a test file:" +echo " claude -c \"Create a file test.txt with content: Hello World\"" +echo "" +echo "3. Check the corresponding log file:" +echo " - Environment vars: .claude/hook-test-env.log" +echo " - JQ parsing: .claude/hook-test-jq.log" +echo " - Comprehensive: .claude/hook-test-comprehensive.log" +echo "" +echo "4. Report which approach worked at:" +echo " https://github.com/ruvnet/claude-flow/issues/249" +echo "" +echo "🔍 To check results after testing:" +echo " ls -la .claude/hook-test-*.log" +echo " cat .claude/hook-test-*.log" \ No newline at end of file diff --git a/tests/manual/test-jq-hook.json b/tests/manual/test-jq-hook.json new file mode 100644 index 0000000000..420f69e55f --- /dev/null +++ b/tests/manual/test-jq-hook.json @@ -0,0 +1,15 @@ +{ + "hooks": { + "PostToolUse": [ + { + "matcher": "Write|Edit", + "hooks": [ + { + "type": "command", + "command": "echo \"JQ TEST - Input: $(cat)\" >> .claude/jq-test.log && cat | jq -r '\"File: \\(.tool_input.file_path // .tool_input.path // .file_path // \"NONE\")\"' >> .claude/jq-test.log" + } + ] + } + ] + } +} \ No newline at end of file