Skip to content

script_apply_edits corrupts indentation and inserts blank lines when replacing method signatures #956

@raddue

Description

@raddue

Bug

script_apply_edits introduces whitespace corruption when replacing a method signature line: the method declaration loses its leading indentation (flushed to column 0) and an extra blank line is inserted before it. The tool returns success: true despite the file having broken formatting.

This was observed on two separate files in the same session, with identical corruption patterns, suggesting a systematic issue in how the tool handles line replacement near method declarations.

Reproduction

Context: Two PlayMode test files needed method signature renames and assertion text changes.

File 1: ThemeBootstrapAcceptanceTests.cs

File before edit (exact region, lines 85-89):

        }

        [UnityTest]
        public IEnumerator Start_WithNoPersistedTheme_AppliesDefaultDarkTheme()
        {

Edit intent: Rename method from AppliesDefaultDarkTheme to AppliesDefaultOutrunTheme, update assertion text from "theme-dark" to "theme-outrun".

File after script_apply_edits (lines 85-91):

        }

        
        [UnityTest]
        
public IEnumerator Start_WithNoPersistedTheme_AppliesDefaultOutrunTheme()
        {

Two issues:

  1. Extra blank line inserted before [UnityTest] (line 87 is now empty)
  2. Method declaration lost indentation -- public IEnumerator is at column 0 instead of column 8 (8 spaces), and a blank line was inserted between [UnityTest] and the method declaration

File 2: RiftTokenGateTests.cs

File before edit (exact region, lines 98-103):

        }

        [UnityTest]
        public IEnumerator Gate_TryGetValue_ReadsInheritedVarValues()
        {
            // Arrange

File after script_apply_edits (lines 98-105):

        }

        
        [UnityTest]
        
public IEnumerator Gate_TryGetValue_ReadsInheritedVarValues()
        {
            // Arrange

Identical corruption pattern: extra blank line before attribute, method declaration flushed to column 0 with blank line between attribute and method.

Expected Behavior

The replacement should preserve the indentation of the original line. If the original method signature was at 8 spaces of indentation, the replacement should also be at 8 spaces. No extra blank lines should be inserted around the edit region.

Impact

  • Code compiles and runs correctly (C# ignores leading whitespace), so validate does not catch it
  • However, the formatting corruption is visible in diffs and requires manual cleanup
  • Since it silently corrupts formatting on success: true, users may not notice until code review

Workaround

  • Read the file after any script_apply_edits call and fix whitespace with an external editor tool
  • Use apply_text_edits with explicit old/new text matching instead (preserves formatting more reliably)

Environment

  • MCP For Unity v9.5.3-beta.1
  • Unity 6000.3.9f1
  • Transport: SSE
  • Client: Claude Code (claude-opus-4-6)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions