Skip to content

Add render-tree-builder-call baseline for component codegen tests - #84162

Merged
chsienki merged 2 commits into
dotnet:features/sonicfrom
chsienki:sonic/builder-call-baseline
Jun 17, 2026
Merged

Add render-tree-builder-call baseline for component codegen tests#84162
chsienki merged 2 commits into
dotnet:features/sonicfrom
chsienki:sonic/builder-call-baseline

Conversation

@chsienki

@chsienki chsienki commented Jun 17, 2026

Copy link
Copy Markdown
Member

Component codegen tests pin the emitted C# against .codegen.cs baselines. That text shifts whenever the generated members are reorganized -- for example when a component is split across multiple partial documents -- so a .codegen.cs diff can be large even when the component renders identically, and on its own is a poor signal for whether the render behavior changed.

This adds a baseline that projects the generated C# down to the render operations it performs. BuilderCallDumper parses the emitted code and extracts every __builder.<op>(sequence, ...) call -- OpenElement, AddAttribute, AddContent, OpenComponent, AddComponentParameter, ... -- and writes them ordered by sequence number to a .builder.txt baseline next to the existing codegen baselines. Calls without a sequence number (CloseElement, SetKey, AddNamedEvent) are positional bookkeeping and are omitted.

The sequence number is the compile-time, source-position identity Razor assigns each render operation. Ordering by it yields a projection that is invariant under cosmetic reorganization of the generated members -- which partial half a member lands in, line-pragma layout, method extraction, whitespace -- yet still trips on a real change: a different element name, a dropped attribute, a renumbered fragment. The projection is computed from the generated source text, so it is deterministic and runs on both net472 and net10.0.

ComponentCodeGenerationTestBase runs the check from its CompileToAssembly shadow, so every test calling CompileToAssembly(generated) picks it up with no per-test change. A test can opt out with assertBuilderBaseline: false. Components that emit no builder calls get no baseline.

No production code changes -- this is test infrastructure only.

Microsoft Reviewers: Open in CodeFlow

Component codegen tests pin the emitted C# against `.codegen.cs` baselines. That
text shifts whenever the generated members are reorganized -- for example when a
component is split across multiple partial documents -- so a `.codegen.cs` diff
can be large even when the component renders identically, and on its own is a poor
signal for whether the render behavior changed.

Add a baseline that projects the generated C# down to the render operations it
performs. `BuilderCallDumper` parses the emitted code and extracts every
`__builder.<op>(sequence, ...)` call -- OpenElement, AddAttribute, AddContent,
OpenComponent, AddComponentParameter, ... -- and writes them ordered by sequence
number to a `.builder.txt` baseline next to the existing codegen baselines. Calls
without a sequence number (CloseElement, SetKey, AddNamedEvent) are positional
bookkeeping and are omitted.

The sequence number is the compile-time, source-position identity Razor assigns
each render operation. Ordering by it yields a projection that is invariant under
cosmetic reorganization of the generated members -- which partial half a member
lands in, line-pragma layout, method extraction, whitespace -- yet still trips on
a real change: a different element name, a dropped attribute, a renumbered
fragment. The projection is computed from the generated source text, so it is
deterministic and runs on both net472 and net10.0.

`ComponentCodeGenerationTestBase` runs the check from its `CompileToAssembly`
shadow, so every test calling `CompileToAssembly(generated)` picks it up with no
per-test change. A test can opt out with `assertBuilderBaseline: false`.
Components that emit no builder calls get no baseline.

Generate baselines with:
  #define GENERATE_BASELINES in
  src/Razor/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/GenerateBaselines.cs
  dotnet test src/Razor/src/Compiler/Microsoft.AspNetCore.Razor.Language/test --filter FullyQualifiedName~ComponentCodeGenerationTestBase

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 17, 2026 01:04
@chsienki
chsienki requested a review from a team as a code owner June 17, 2026 01:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review this pull request because it exceeds the maximum number of files (300). Try reducing the number of changed files and requesting a review from Copilot again.

@chsienki
chsienki enabled auto-merge (squash) June 17, 2026 02:23
CSharpSyntaxTree.ParseText(string) is banned by RS0030 in this repo; the
SourceText overload is the approved entry point. Also drop the
runtime-frame comparison framing from the type's <remarks> so it
describes only what the projection is.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants