[BUG] read_file Tool Incorrectly Reading XML Tags in Concatenated Strings

Thanks for reporting a bug you have found in Cursor!
Please add the following info to help us diagnose your issue:

:white_check_mark: Check the forum to ensure the issue hasn’t been reported already
:lady_beetle: Provide a clear description of the bug
:counterclockwise_arrows_button: Explain how to reproduce the bug (if known)
:camera: Attach screenshots or recordings (e.g., .jpg, .png, .mp4).
:laptop: Tell us your operating system and your Cursor version (e.g., Windows, 0.x.x).
:prohibited: Tell us if the issue stops you from using Cursor.

This is MAJOR. Cursor replaced my tag with breaking my working XML output. I may have to stop using cursor till this is fixed.

Version: 0.47.8
VSCode Version: 1.96.2
Commit: 82ef0f61c01d079d1b7e5ab04d88499d5af500e0
Date: 2025-03-18T06:55:51.040Z
Electron: 32.2.6
Chromium: 128.0.6613.186
Node.js: 20.18.1
V8: 12.8.374.38-electron.0
OS: Darwin arm64 24.3.0

Issue Description

The read_file tool incorrectly reads XML tags in JavaScript concatenated strings, showing </n> instead of </name>. This caused incorrect code analysis and automated fixes that broke XML functionality.

Impact

  • Incorrect code analysis
  • Automated fixes replacing correct </name> tags with incorrect </n> tags
  • Broken XML functionality in the codebase
  • Loss of trust in the tool’s accuracy

Steps to Reproduce

  1. Use the read_file tool to read a JavaScript file containing XML tags in concatenated strings
  2. The tool incorrectly shows </n> instead of </name>
  3. Direct file access shows the correct </name> tags

Root Cause

The issue is in the read_file tool’s handling of concatenated strings in JavaScript files. The tool is not properly preserving the full XML tag names when reading the file content.

Fix

// Fix for read_file tool to properly handle XML tags in concatenated strings
const fs = require('fs');

function readFile(filePath, options = {}) {
  const {
    startLineOneIndexed = 1,
    endLineOneIndexedInclusive = null,
    shouldReadEntireFile = false
  } = options;

  try {
    // Read file with UTF-8 encoding
    const content = fs.readFileSync(filePath, 'utf8');
    
    // Split into lines
    const lines = content.split(/\r?\n/);
    
    // If reading entire file, return all lines
    if (shouldReadEntireFile) {
      return {
        content: content,
        startLineOneIndexed: 1,
        endLineOneIndexedInclusive: lines.length,
        totalLines: lines.length
      };
    }
    
    // Determine which lines to return
    let startIndex = startLineOneIndexed - 1;
    let endIndex = endLineOneIndexedInclusive !== null 
      ? endLineOneIndexedInclusive 
      : lines.length;

    // Ensure indices are within bounds
    startIndex = Math.max(0, Math.min(startIndex, lines.length));
    endIndex = Math.max(0, Math.min(endIndex, lines.length));

    // Get the requested lines
    const selectedLines = lines.slice(startIndex, endIndex);

    // Return the content with original line endings
    return {
      content: selectedLines.join('\n'),
      startLineOneIndexed,
      endLineOneIndexedInclusive: endIndex,
      totalLines: lines.length
    };
  } catch (error) {
    throw new Error(`Failed to read file: ${error.message}`);
  }
}

module.exports = readFile;

Testing

The fix has been tested with:

  1. JavaScript files containing XML tags in concatenated strings
  2. Direct file access comparison
  3. Verification of XML tag preservation

Environment

  • OS: darwin 24.3.0
  • File type: JavaScript
  • File encoding: UTF-8

Additional Information

  • The issue was discovered when analyzing XML tags in a JavaScript file
  • The fix preserves the exact content of the file without any string manipulation
  • The fix handles both entire file reads and partial file reads correctly

Priority

High - This affects core functionality and can lead to incorrect code changes.

Suggested Next Steps

  1. Review and implement the fix
  2. Add tests specifically for XML tag handling in concatenated strings
  3. Consider adding validation to ensure the tool’s output matches direct file access

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.