dotnet / dotnet/roslyn

Small syntax error in razor markup invalidates entire source file

Open
#85,299 8 comments 0 reactions 0 assignees View on GitHub
Area-Razor Area-Razor-Compiler Feature Request
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

When making a small syntax mistake in a .razor file, a large number of errors are generated, and the root problem isn't always the first in the list so it's very difficult to tell where the problem lies.

As an example, I just edited an interpolated string in a line of code in a function, below;

string firstLine = $"{productsAdded} product{productsAdded.Pluralise()} added to quote."}";

I was pasting the line from elsewhere and stupidly left an erroneous }" at the end.

This generated a total of 59 errors in the razor file, mainly down to the overall structure being messed up so lots of 'Type or namespace definition, or end-of-file expected' errors, and 'The name '_somePrivateVariable' does not exist in the current context', all fair enough, but not very helpful.

The errors do not appear immediately, razor/cshtml validation has always been a little sluggish (even on an i9 processor with 64Gb RAM), so it can be a few edits later before they all appear if I'm typing quickly; and if I make one change to a file, save and close the tab before the errors appear I can't even use undo to revert a few changes to find the problem that way.

I've been meaning to report this for a long time but lazily assumed it can't just be me having the problem and that somebody else would and it'd be fixed at some point.

I only cite one example but this has been a problem for a long time with Blazor source files, in several projects, over numerous releases of Visual Studio and .Net and on both my dev laptop and desktop (on the current Windows install and the one I had before I completely reinstalled on a new SSD), and not just for unterminated strings; so not specific to my machine, project, a particular combination of NuGet libraries, or version of VS/Blazor/Net etc.

The problem is worse when it's the cshtml markup that is broken, this example above is just the one that got me to report the problem.

### Expected Behavior

I appreciate that this typo breaks the structure of the code and so causes lots of things to be invalid, but when editing a C# class, for example, it copes with this sort of typo a lot better, identifies and flags it immediately, and only reports the root problem and perhaps a handful of consequential errors, rather than invalidating the entire file and generating dozens of errors.

I would expect the razor file interpreter/validator to do much the same, identify the root cause of the problem, ideally more quickly than it does at present, and report that error and maybe a handful of others that it causes but localise the problem at least to the function it is in, not generate 58 consequential errors throughout the file with the root cause in the middle somewhere; ideally with the root problem reported first in the list.

### Steps To Reproduce

In a razor file of any decent complexity, declare an interpolated string with a typo at the end like the below;

string breakTheStructure = $"Blah blah"}";

One thing to note, having found and fixed my typo, and re-created it for this report, the typo is now reported as the first error in the list with a bang-on correct 'Unterminated string literal' error, but it was not when I originally caused the problem.

### Exceptions (if any)

_No response_

### .NET Version

9.0

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.