Skip to content

Latest commit

 

History

History
102 lines (82 loc) · 3.12 KB

File metadata and controls

102 lines (82 loc) · 3.12 KB

Claude Code Crash Analysis Update

Discovery Summary

After testing the crash reproduction, I've found:

  1. CLI Mode Behavior: The claude CLI with --print mode doesn't actually crash with the problematic pattern. It generates the tool call but doesn't execute it (files aren't created).

  2. The Core Pattern: The crash is triggered by this specific combination:

    cat > file.ts << 'EOF'
    console.log(`${variable?.method()}`);
    EOF

Minimal Crash Pattern

The Absolute Minimum That Should Crash (in interactive mode):

cat > /tmp/x.ts << 'EOF'
console.log(`${x?.toString()}`);
EOF

Why It Crashes

  • Claude Code's parser encounters ${x?.toString()} inside a heredoc
  • The parser incorrectly interprets this as bash variable substitution
  • Even though the heredoc uses quoted delimiter ('EOF'), which should prevent substitution
  • The error "Bad substitution: x?.toString" is a bash-style error message

Key Insights

1. Emoji is NOT Required

The emoji (📝) appears to be coincidental. The crash is purely about the template literal pattern with optional chaining.

2. The Exact Trigger Pattern

`${variable?.method()}`  // This exact pattern inside heredoc

3. What Does NOT Crash

  • ${variable.method()} - No optional chaining
  • ${variable?.property} - No method call
  • ${variable || 'default'} - No optional chaining
  • variable?.method() - Outside template literal

Reproduction Differences

Interactive Mode (Crashes)

When using Claude Code interactively and it executes:

cat > /tmp/file.ts << 'EOF'
const x = 42;
console.log(`Result: ${x?.toString()}`);
EOF

Result: CRASH with "Bad substitution: x?.toString"

CLI Print Mode (No Crash)

When using claude --print < prompt.txt:

  • Tool call is generated correctly
  • No actual execution occurs
  • No crash happens
  • This explains why our CLI tests didn't reproduce it

Root Cause

The crash occurs in the execution phase, not the generation phase. The minified code at:

file:///Users/alexanderriccio/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code/cli.js:80:79195

Contains a parser/executor that mishandles the template literal pattern within heredocs.

Testing Recommendations

To Reproduce in Interactive Mode

  1. Start Claude Code interactively
  2. Ask it to create a TypeScript file using the exact pattern
  3. Observe the crash

To Test Fix

After any potential fix, verify these all work:

# Test 1: Basic optional chaining in template
cat > /tmp/test1.ts << 'EOF'
console.log(`${value?.toString()}`);
EOF

# Test 2: With multiple optional chains
cat > /tmp/test2.ts << 'EOF'
console.log(`${a?.b()} and ${c?.d()}`);
EOF

# Test 3: Complex nested case
cat > /tmp/test3.ts << 'EOF'
const result = `Data: ${obj?.method()?.substring(0, 10)}`;
EOF

Workaround Confirmation

Until fixed, these workarounds are confirmed:

  1. Extract before template: const str = x?.toString(); console.log(\Value: ${str}`);`
  2. Ternary operator: console.log(\Value: ${x ? x.toString() : 'undefined'}`);`
  3. Concatenation: console.log("Value: " + x?.toString());