Skip to content

Rename Feature Corrupts Unsaved Files in VS2017 15.7.1  #4927

@Zagrophyte

Description

@Zagrophyte

When you rename a symbol that exists in another file that is open with pending changes, the rename operation is computed using the character positions of the unsaved file. This means if you've added any lines or changed target symbol positions in the target files, the replace operation will overwrite any text that existed at the old offsets.

UPDATE: While writing this issue I have now also encountered the crashing issues when renaming symbols added and used within the same unsaved file. (seems related to #4655 and #2204).

Repro steps

  1. Open a source file which contains a symbol that is defined in another source file.
    image

  2. Add a few lines of code above where that symbol is used. DO NOT SAVE YET
    image

  3. Go to the definition of that symbol in the other file (F12). and use the Rename feature (Ctrl+R+R) to rename that symbol.
    image

  4. The Rename operation overwrites code above where the actual symbols were used and saves the file, corrupting the source code.
    image

Bonus Content

Interestingly, I tried it again with Preview Changes enabled, and the resulting preview window actually shows the corruption:
image

Repro Solution ZIP

VS2017.15.7.1-BlindRenameOffsetIssue.zip

Expected behavior

Rename feature properly renames usages in open / modified files.

Actual behavior

Rename feature uses old buffer to calculate rename operations, overwriting current buffer and saving the file.

Known workarounds

Only to save all open files before performing any rename operations.

Related information

Provide any related information

  • Operating system
    • Windows 10, Version 1709 (16299.309)
  • Branch
    • 15.7.1 public release
  • .NET Runtime, CoreCLR or Mono Version
    • .NET Framework 4.7.1
  • Editing Tools (e.g. Visual Studio Version)
    • Visual Studio 15.7.1
  • Indications of severity
    • This issue and the related Rename->VSCrash issues can cause data loss.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-LangService-APIImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions