Skip to content

Format of .cshtml fails on Visual Studio Version 17.14.0 Preview 1.0 #11506

@johanbenschop

Description

@johanbenschop

While watching the ASP.NET Community Standup - Razor tooling update and Hot Reload on Twitch earlier it was said the new Visual Studio 17.4 preview had a new CSHTML formatter, so I went through some random files from a project where the formatter has been questionable in the past.

I found one .cshtml file where Visual Studio refuses to format it.

I've copied the file in question to this gist here: https://gist.github.com/johanbenschop/0603aefb408e0718cd46250d7728b904

There was also an exception logged in the Razor Logger Output:

[CLaSP] [20:04:00.7286864] [null]: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: lineNumber at Microsoft.CodeAnalysis.Text.CompositeText.CompositeTextLineInfo.get_Item(Int32 lineNumber) at Microsoft.CodeAnalysis.Razor.Formatting.New.CSharpFormattingPass.d__3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.CodeAnalysis.Razor.Formatting.RazorFormattingService.d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Razor.LanguageServer.Formatting.DocumentFormattingEndpoint.d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem1.<StartRequestAsync>d__172.MoveNext()

Playing the game of process of elimination suggests that something between line 69 and 84 causes the issue. Playing this game some more I've found that it's the new object initialization within calling a partial. Changing this to model="null" it formats just fine.

<partial name="~/Views/Shared/_TestimonialRow.cshtml"
         model="new DefaultTitleContentAreaViewModel
		   {
			   Title = Model.CurrentPage.TestimonialsTitle,
			   ContentArea = Model.CurrentPage.TestimonialsContentArea,
			   ChildCssClass = string.Empty
		   }" />

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions