dotnet/roslyn

RenameSymbolAsync does not rename all instances (bug exists as of v3.10)

sadreck opened this issue · 0 comments

Version Used: v3.10 onwards, including v4.0.1

Steps to Reproduce:

Create a test Console Application:

File: Program.cs

using ToBeRenamed = ConsoleApp1.Folder1.ToBeRenamed;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ToBeRenamed myClass = new ToBeRenamed("hello");
        }
    }
}

File: Folder1\ToBeRenamed.cs

namespace ConsoleApp1.Folder1
{
    internal class ToBeRenamed
    {
        private string myVar2;
        internal ToBeRenamed(string var1)
        {
            myVar2 = var1;
        }
    }
}

Create a Roslyn application

Make sure the Microsoft.CodeAnalysis.CSharp.Workspaces is v3.10 or later. Insert the following after the TODO comment:

Console.WriteLine($"Finished loading solution '{solutionPath}'");
// Everything above here is auto-generated
foreach (Project project in solution.Projects)
{
    foreach (Document document in project.Documents)
    {
        SemanticModel semanticModel = await document.GetSemanticModelAsync();
        SyntaxTree syntaxTree = await document.GetSyntaxTreeAsync();
        IEnumerable<ClassDeclarationSyntax> nodes = syntaxTree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>();
        SyntaxNode node = ((IEnumerable<ClassDeclarationSyntax>)nodes).FirstOrDefault(s => s.Identifier.ToString() == "ToBeRenamed");
        if (node == null)
        {
            continue;
        }
        ISymbol symbol = semanticModel.GetDeclaredSymbol(node);
        solution = await Renamer.RenameSymbolAsync(solution, symbol, "ThisIsTheNewName", solution.Workspace.Options);
    }
}

workspace.TryApplyChanges(solution);

Expected Behavior:

All references to ToBeRenamed should be renamed.

Actual Behavior:

The new <classname> instance is not renamed:

using ThisIsTheNewName = ConsoleApp1.Folder1.ThisIsTheNewName;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ThisIsTheNewName myClass = new ToBeRenamed("hello");
        }
    }
}

Extra Information:

This could be linked with #56940 as the CodeAnalysis version there is v3.11.