[10.0.300] RZ1021 falsely raised for HTML tags inside `@if` blocks
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
Sorry, this should have been filed at dotnet/roslyn
## Summary
After upgrading from .NET SDK 10.0.203 → 10.0.300, the Razor source generator falsely reports `RZ1021: Markup in a code block must start with a tag and all start tags must be matched with end tags` for perfectly valid markup. The generated `.g.cs` is corrupted: the parser eats the leading ``).
- The tag name must be **4 or more characters** (3-char names like `
`, ``, `
`, ``, `` work fine).
- The element's line must be **tab-indented or have no indentation**. Space indentation works.
## Minimal repro
`Views/Home/Repro.cshtml`:
```cshtml
@if (true)
{
}
```
(All indentation is tabs.) Build with `dotnet build`:
```
Views/Home/Repro.cshtml(4,5): error RZ1021: Markup in a code block must start with a tag and all start tags must be matched with end tags. Do not use unclosed tags like "
". Instead use self-closing tags like "
".
```
Followed by ~10 cascading CS errors in the generated `_Repro_cshtml.g.cs`.
### What makes it succeed
UPDATE: Spoilered, because @Magehernan encountered this in a scenario that did not match my findings.
- It occurred for simple `
- It occurred without an outer `
Any one of the following changes makes the build succeed:
1. Replace tab indentation with spaces.
2. Use a tag name with ≤ 3 characters (`
`, ``, `
`, ``, ``).
3. Remove the outer `
4. Disable the source generator: `false`.
### Tag name length matrix (with tab indent, inside `
| Tag | Result |
|---|---|
| ``, ``, `` | OK |
| ``, `` | **RZ1021** |
| `
`, ``, `
`, ``, `` | OK |
| ``, ``, ``, ``, ``, ``, ``, `` | **RZ1021** |
| Tag with hyphen (``) | OK |
So this is *not* about void elements specifically (otherwise `
` and `` would behave the same); the discriminator looks like raw tag-name length combined with leading whitespace classification.
### Inspecting the generated file
With `EmitCompilerGeneratedFiles=true`, the generated `_Repro_cshtml.g.cs` shows the parser bailed early — it emits `` types that conflict with every other Razor file in the assembly. Because the cascading CS errors land in `AspNetCoreGeneratedDocument`, a single bad `.cshtml` makes every other view in the project fail to compile (CS0101, CS0102 for `ModelExpressionProvider`/`Url`/`Component`/`Json`/`Html`).
## Environment
- SDK: `.NET SDK 10.0.300` (commit `caa81fa497`)
- OS: Debian 13 (Linux x64)
- Previously working: 10.0.203
- Affected project: `Microsoft.NET.Sdk.Web`, `net10.0`
- Reproduces in both `Debug` and `Release` configurations.
- Reproduces without any tag helper imports (removing `@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers` does not fix it).
## Impact
This silently broke a real production project after a system-wide SDK update. The error message (`Do not use unclosed tags like "
"`) is actively misleading because the actual markup is well-formed self-closed ``, and `
` itself is one of the tags that does *not* trigger it. A single offending file cascades into 100+ CS errors across the project, making it hard to identify the root cause without inspecting the generated source.
## Workaround
```xml
false
```
Contributor guide
Assessment
This issue has not been assessed yet.