Roslyn: Rename Type to Match File crashes VS when code only contains `class` keyword

Created on 27 Nov 2019  Â·  4Comments  Â·  Source: dotnet/roslyn

Also tracked by https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1030401

Steps to Reproduce:

  1. In an otherwise empty C# file:

```C#
class$$


2. Remove the `$$` and invoke the lightbulb at that location
3. Pick "Rename type to Program"

**Expected Behavior**: The refactoring shouldn't be offered on this particular incomplete code.
**Actual Behavior**: VS crashes.

**Note: This is particularly important because of the way the LSP is implemented today.** See https://github.com/dotnet/roslyn/issues/40086

ArgumentException: openBraceToken

at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ClassDeclaration(SyntaxList1 attributeLists, SyntaxTokenList modifiers, SyntaxToken keyword, SyntaxToken identifier, TypeParameterListSyntax typeParameterList, BaseListSyntax baseList, SyntaxList1 constraintClauses, SyntaxToken openBraceToken, SyntaxList1 members, SyntaxToken closeBraceToken, SyntaxToken semicolonToken) in C:\src\roslyn\src\Compilers\CSharp\Portable\Generated\Syntax.xml.Main.Generated.cs:line 4333 at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.Update(SyntaxList1 attributeLists, SyntaxTokenList modifiers, SyntaxToken keyword, SyntaxToken identifier, TypeParameterListSyntax typeParameterList, BaseListSyntax baseList, SyntaxList1 constraintClauses, SyntaxToken openBraceToken, SyntaxList1 members, SyntaxToken closeBraceToken, SyntaxToken semicolonToken) in C:\src\roslyn\src\Compilers\CSharp\Portable\Generated\Syntax.xml.Syntax.Generated.cs:line 7788
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitClassDeclaration(ClassDeclarationSyntax node) in C:\src\roslyn\src\Compilers\CSharp\Portable\Generated\Syntax.xml.Main.Generated.cs:line 1739
at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.AcceptTResult in C:\src\roslyn\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 142
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.VisitCompilationUnit(CompilationUnitSyntax node) in C:\src\roslyn\src\Compilers\CSharp\Portable\Generated\Syntax.xml.Main.Generated.cs:line 1703
at Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.AcceptTResult in C:\src\roslyn\src\Compilers\CSharp\Portable\Generated\Syntax.xml.Syntax.Generated.cs:line 6936
at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxRewriter.Visit(SyntaxNode node) in C:\src\roslyn\src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxRewriter.cs:line 39
at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService.RenameRewriter.Visit(SyntaxNode node) in C:\src\roslyn\src\Workspaces\CSharp\Portable\Rename\CSharpRenameRewriterLanguageService.cs:line 171
at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService.AnnotateAndRename(RenameRewriterParameters parameters) in C:\src\roslyn\src\Workspaces\CSharp\Portable\Rename\CSharpRenameRewriterLanguageService.cs:line 45
at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver.Session.d__27.MoveNext() in C:\src\roslyn\src\Workspaces\Core\Portable\Rename\ConflictEngine\ConflictResolver.Session.cs:line 795
```

Area-IDE Bug

Most helpful comment

@jmarolf The rename code is littered with FatalError.ReportUnlessCanceled calls in exception filters. That's why the gold bar didn't have a chance to fire in this case.

All 4 comments

FYI @jinujoseph This is the LSP server exception we talked about today. In addition to fixing the refactoring itself, we should probably guard (all of?) the LSP entry points against exceptions? /cc: @dibarbet

Also tagging @jmarolf — shouldn’t exceptions in the production of the preview window turn into gold bars? Another aspect to look at.

@dpoeschl its possible that there is a hole somewhere. We also crash VS when you build locally instead of showing a gold bar so that we can find these kinds of issues.

@jmarolf The rename code is littered with FatalError.ReportUnlessCanceled calls in exception filters. That's why the gold bar didn't have a chance to fire in this case.

Was this page helpful?
0 / 5 - 0 ratings