Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roslyn Tokenizer #11086

Merged
merged 44 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
46698be
Rename existing C# tokenizer to have a legacy prefix
333fred Jun 20, 2024
b3f0a12
Duplicate existing tokenizer and add a flag to control whether to use…
333fred Jun 20, 2024
14f71e2
Remove roslyn package pinning
333fred Jul 18, 2024
b3f055a
Legacy->Native
333fred Jul 29, 2024
b9c924b
Roslyn Tokenizer Infrastructure (#10676)
333fred Jul 29, 2024
4346076
Merge remote-tracking branch 'upstream/main' into merge-main
333fred Jul 29, 2024
a35513a
Merge main (#10679)
333fred Jul 29, 2024
894a9ba
Remove unused ITokenizer interface, make tokenizers disposable.
333fred Aug 1, 2024
a15d7cc
Remove unneeded common states
333fred Aug 1, 2024
3a8fd07
Allow setting the next tokenizer state after a razor comment body is …
333fred Aug 1, 2024
60ac8d1
Plumb the Roslyn tokenizer through more tests
333fred Aug 1, 2024
73aa717
Nullable enable the tokenizer
333fred Aug 1, 2024
dcef8ed
Add consolidated syntax kinds. Since there aren't consumers that care…
333fred Aug 1, 2024
5749a8e
Give better error messages when syntax tree verification fails.
333fred Aug 1, 2024
2823f64
The big change: use the roslyn tokenizer for tokenizing C#.
333fred Aug 1, 2024
c25ac20
Update SyntaxKinds to the new consolidated kinds in tests.
333fred Aug 1, 2024
34ab111
Increase test coverage of a few scenarios, and bulk up existing tests.
333fred Aug 1, 2024
abd6aba
Skip test for now, give a better exception.
333fred Aug 1, 2024
acf7a60
Name change
333fred Aug 1, 2024
640d377
Better support giving errors on raw string literals, increase test co…
333fred Aug 2, 2024
3155200
PR feedback.
333fred Aug 2, 2024
1a0d57d
Convert to the correct type
333fred Aug 2, 2024
aa24f69
Additional commenting and PR feedback. I've simplified the reset loop…
333fred Aug 6, 2024
5b7e927
More feedback
333fred Aug 9, 2024
91bbfde
Use the roslyn tokenizer (#10702)
333fred Aug 12, 2024
711bda8
Merge remote-tracking branch 'upstream/main' into features/roslyn-tok…
333fred Aug 12, 2024
8bf616a
Merge main to tokenizer branch (#10724)
333fred Aug 12, 2024
fef2d1c
Plumb CSharpParseOptions to the tokenizer (#10733)
333fred Aug 13, 2024
35871e3
Merge remote-tracking branch 'upstream/main' into merge-main
333fred Aug 22, 2024
647126b
Merge main to tokenizer branch (#10781)
333fred Aug 22, 2024
d7d4932
Merge remote-tracking branch 'upstream/main' into features/roslyn-tok…
333fred Sep 10, 2024
0ea44ba
Reimplement IDispoasable in test clases.
333fred Sep 10, 2024
87dafaf
Merge main to tokenizer branch (#10872)
333fred Sep 12, 2024
07b168a
Support directive tokenization (#10979)
333fred Oct 11, 2024
99c61dd
Enable the new tokenizer for tooling tests (#11010)
333fred Oct 15, 2024
2f584c9
Use the code component for correct test output
333fred Oct 21, 2024
7598fbf
Reset isOnlyWhitespaceOnLine in scenario revealed in IDE test
333fred Oct 22, 2024
dfe65f9
Add new tests for switch case (#11063)
333fred Oct 22, 2024
2aac41c
Merge remote-tracking branch 'upstream/main' into merge-main
333fred Oct 23, 2024
7b81af5
One more baseline update
333fred Oct 23, 2024
c724539
Merge main (#11075)
333fred Oct 23, 2024
c20c1cd
Final lexer changes (#11078)
333fred Oct 24, 2024
6169824
Merge remote-tracking branch 'upstream/main' into merge-main
333fred Oct 24, 2024
927d6a9
Merge main to tokenizer branch (#11085)
333fred Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
56 changes: 56 additions & 0 deletions docs/Compiler Breaking Changes - DotNet 9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This document lists known breaking changes in Razor after .NET 9 all the way to .NET 10.

## Preprocessor directive parsing breaks

***Introduced in VS 17.13p1 and .NET 9.0.200***

A new lexing mode was introduced for understanding the C# sections in razor files that brings increased compatibility with how C# is natively lexed. However, this
also brings some breaking changes to the Razor compiler's understanding of C# preprocessing directives, which previously did not work consistently. Directives are
now required to start at the beginning of a line in Razor files (only whitespace is allowed before them). Additionally, disabled sections are now properly disabled
by the Razor compiler when `#if` preprocessor blocks are considered inactive.

### Preprocessor blocks are required to start at the beginning of a line

```razor
@{ #if DEBUG /* Previously allowed, now triggers RZ1043 */ }
<div>test</div>
@{ #endif /* Previously allowed, now triggers RZ1043 */ }
```

To fix, move the directives to a new line. Only whitespace is allowed before the directive.

```razor
@{
#if DEBUG /* This is allowed */
}
<div>test</div>
@{
#endif /* This is allowed */
}
```

### Disabled blocks are now considered properly in the Razor compiler

Disabled blocks are now considered completely disabled by the Razor compiler, and no attempt to understand the block is made. When combined with the previous break,
this means that if an `#else`, `#elif`, or `#endif` was not at the start of a line (modulo whitespace), a larger section of the file will be considered disabled than
in older versions of the Razor compiler. To help diagnose potential breaks here, the Razor compiler will scan disabled text sections for potential misplaced preprocessor
directives and report a warning if one is encountered.

```razor
@{
#if false
}

This area is now properly considered disabled by the razor compiler, and no attempt to understand it as either C# or HTML is made. This
can cause changes to how the output is rendered from previous versions of the Razor compiler.

@{ #else
In previous versions of the Razor compiler, this directive would have been picked up. It is no longer picked up because it is not at
the start of a line. The Razor compiler will report a warning, RZ1044, to help diagnose any potential breaks in this area.
}

@{
#endif
}
```

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X;
Expand Down Expand Up @@ -156,6 +157,7 @@ private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers)
return RazorProjectEngine.Create(b =>
{
b.Features.Add(new TestTagHelperFeature(tagHelpers));
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));
b.Features.Add(new RazorPageDocumentClassifierPass());
b.Features.Add(new MvcViewDocumentClassifierPass());
}).Engine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;
using static Microsoft.AspNetCore.Razor.Language.CommonMetadata;

Expand Down Expand Up @@ -219,6 +221,7 @@ private static RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers)
b.Features.Add(new MvcViewDocumentClassifierPass());

b.Features.Add(new TestTagHelperFeature(tagHelpers));
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));
}).Engine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#nullable disable

using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X;
Expand Down Expand Up @@ -180,6 +181,7 @@ private RazorEngine CreateEngine()
// Notice we're not registering the InjectDirective.Pass here so we can run it on demand.
b.AddDirective(InjectDirective.Directive);
b.AddDirective(ModelDirective.Directive);
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));

b.Features.Add(new RazorPageDocumentClassifierPass());
b.Features.Add(new MvcViewDocumentClassifierPass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X;
Expand Down Expand Up @@ -154,6 +156,7 @@ private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers)
return RazorProjectEngine.Create(b =>
{
b.Features.Add(new TestTagHelperFeature(tagHelpers));
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));
b.Features.Add(new RazorPageDocumentClassifierPass());
b.Features.Add(new MvcViewDocumentClassifierPass());
}).Engine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Collections.Immutable;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X;
Expand Down Expand Up @@ -128,6 +130,7 @@ private RazorEngine CreateEngine()
return RazorProjectEngine.Create(b =>
{
PageDirective.Register(b);
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));
}).Engine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;
using static Microsoft.AspNetCore.Razor.Language.CommonMetadata;

Expand Down Expand Up @@ -217,8 +219,8 @@ private static RazorProjectEngine CreateProjectEngine(params TagHelperDescriptor
return RazorProjectEngine.Create(b =>
{
b.Features.Add(new MvcViewDocumentClassifierPass());

b.Features.Add(new TestTagHelperFeature(tagHelpers));
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Text;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions;
Expand Down Expand Up @@ -154,6 +156,7 @@ private RazorEngine CreateEngine(params TagHelperDescriptor[] tagHelpers)
return RazorProjectEngine.Create(b =>
{
b.Features.Add(new TestTagHelperFeature(tagHelpers));
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));
b.Features.Add(new RazorPageDocumentClassifierPass());
b.Features.Add(new MvcViewDocumentClassifierPass());
}).Engine;
Expand All @@ -171,7 +174,7 @@ private DocumentIntermediateNode CreateIRDocument(RazorEngine engine, RazorCodeD
}
}

return codeDocument.GetDocumentIntermediateNode();
return codeDocument.GetDocumentIntermediateNode();
}

private TagHelperIntermediateNode FindTagHelperNode(IntermediateNode node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Collections.Immutable;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions;
Expand Down Expand Up @@ -128,6 +130,7 @@ private RazorEngine CreateEngine()
return RazorProjectEngine.Create(b =>
{
PageDirective.Register(b);
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));
}).Engine;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.NET.Sdk.Razor.SourceGenerators;
using Xunit;
using static Microsoft.AspNetCore.Razor.Language.CommonMetadata;

Expand Down Expand Up @@ -217,8 +219,8 @@ private RazorProjectEngine CreateProjectEngine(params TagHelperDescriptor[] tagH
return RazorProjectEngine.Create(b =>
{
b.Features.Add(new MvcViewDocumentClassifierPass());

b.Features.Add(new TestTagHelperFeature(tagHelpers));
b.Features.Add(new ConfigureRazorParserOptions(useRoslynTokenizer: true, CSharpParseOptions.Default));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.AspNetCore.Razor.Language.Legacy;

public class CSharpAutoCompleteTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true)
public class CSharpAutoCompleteTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true, useLegacyTokenizer: true)
{
[Fact]
public void FunctionsDirectiveAutoCompleteAtEOF()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.AspNetCore.Razor.Language.Legacy;

public class CSharpBlockTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true)
public class CSharpBlockTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true, useLegacyTokenizer: true)
{
[Fact]
public void CSharpBlock_SingleLineControlFlowStatement_Error()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Microsoft.AspNetCore.Razor.Language.Legacy;

public class CSharpErrorTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true)
public class CSharpErrorTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true, useLegacyTokenizer: true)
{
[Fact]
public void HandlesQuotesAfterTransition()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.AspNetCore.Razor.Language.Legacy;

public class CSharpExplicitExpressionTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true)
public class CSharpExplicitExpressionTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true, useLegacyTokenizer: true)
{
[Fact]
public void ShouldOutputZeroLengthCodeSpanIfExplicitExpressionIsEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.AspNetCore.Razor.Language.Legacy;

public class CSharpFunctionsTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true)
public class CSharpFunctionsTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true, useLegacyTokenizer: true)
{
[Fact]
public void Functions_SingleLineControlFlowStatement_Error()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.AspNetCore.Razor.Language.Legacy;

public class CSharpImplicitExpressionTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true)
public class CSharpImplicitExpressionTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true, useLegacyTokenizer: true)
{
[Fact]
public void ParsesNullConditionalOperatorImplicitExpression_Bracket1()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Microsoft.AspNetCore.Razor.Language.Legacy;

public class CSharpNestedStatementsTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true)
public class CSharpNestedStatementsTest() : ParserTestBase(layer: TestProject.Layer.Compiler, validateSpanEditHandlers: true, useLegacyTokenizer: true)
{
[Fact]
public void NestedSimpleStatement()
Expand Down
Loading