Skip to content

Build error in razor components with switch patterns when first case is a comparison #11999

@legrab

Description

@legrab

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When the first case of a "return x switch" pattern starts with a < comparison character, dotnet fails to build the project.

The character seems to be interpreted as an opener for a xml tag and is thus not tolerated within the code block.
This results in a RZ1006: The code block is missing a closing "}" character. error.

Note that starting with a > character with the same code structure succeeds without issues.

Expected Behavior

The build succeeds without errors.

Steps To Reproduce

  • Create any razor project
  • Add a .razor component with the following code block:
@code {
    private bool SwitchPatternNotBuilding()
    {
        const int myInteger = 42;
        
        return myInteger switch
        {
            < 0 => false, // Razor breaks if this is the first case
            0 => true,
            _ => false
        };
    }
}
  • Build the application
  • Note the many build errors including RZ1006 and CS1513

Exceptions (if any)

The following build output is generated:

0>------- Started building project: Razor.SwitchPattern.Lib

0>SwitchPatternTester.razor(1,1): Error RZ1006 : The code block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
0>SwitchPatternTester.razor(1,7): Error RZ1006 : The code block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
0>SwitchPatternTester.razor(8,13): Error RZ9980 : Unclosed tag '' with no matching end tag.
0>SwitchPatternTester.razor(8,14): Error RZ1025 : The "" element was not closed.  All elements must be either self-closing or have a matching end tag.
0>SwitchPatternTester_razor.g.cs(20,10): Error CS1513 : } expected
0>SwitchPatternTester_razor.g.cs(35,41): Error CS1003 : Syntax error, '=>' expected
0>SwitchPatternTester_razor.g.cs(35,41): Error CS1525 : Invalid expression term ';'
0>SwitchPatternTester_razor.g.cs(35,41): Error CS1003 : Syntax error, ',' expected
0>SwitchPatternTester_razor.g.cs(36,43): Error CS1003 : Syntax error, '=>' expected
0>SwitchPatternTester_razor.g.cs(36,43): Error CS1525 : Invalid expression term ';'
0>SwitchPatternTester_razor.g.cs(36,43): Error CS1003 : Syntax error, ',' expected
0>SwitchPatternTester_razor.g.cs(37,179): Error CS1003 : Syntax error, '=>' expected
0>SwitchPatternTester_razor.g.cs(37,179): Error CS1525 : Invalid expression term ';'
0>SwitchPatternTester_razor.g.cs(37,179): Error CS1003 : Syntax error, ',' expected
0>SwitchPatternTester_razor.g.cs(38,37): Error CS1003 : Syntax error, '=>' expected
0>SwitchPatternTester_razor.g.cs(38,37): Error CS1525 : Invalid expression term ';'
0>SwitchPatternTester_razor.g.cs(38,37): Error CS1003 : Syntax error, ',' expected
0>SwitchPatternTester_razor.g.cs(39,10): Error CS1002 : ; expected
0>SwitchPatternTester_razor.g.cs(42,2): Error CS1513 : } expected
0>SwitchPatternTester_razor.g.cs(35,13): Error CS0246 : The type or namespace name '__builder' could not be found (are you missing a using directive or an assembly reference?)
0>SwitchPatternTester_razor.g.cs(36,13): Error CS0246 : The type or namespace name '__builder' could not be found (are you missing a using directive or an assembly reference?)
0>SwitchPatternTester_razor.g.cs(37,13): Error CS0246 : The type or namespace name '__builder' could not be found (are you missing a using directive or an assembly reference?)
0>SwitchPatternTester_razor.g.cs(38,13): Error CS0246 : The type or namespace name '__builder' could not be found (are you missing a using directive or an assembly reference?)
0>------- Finished building project: Razor.SwitchPattern.Lib. Succeeded: False. Errors: 23. Warnings: 0
Build completed in 00:00:01.105

.NET Version

9.0.203

Anything else?

Also created a sample repository that showcases the error here: https://github.com/legrab/razor.switchpattern

Metadata

Metadata

Assignees

Labels

area-compilerUmbrella for all compiler issues

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions