Skip to content

Fix XAML diagnostics false positives on valid markup - #403

Open
Chiara Mooney (chiaramooney) wants to merge 4 commits into
mainfrom
chiaramooney-xaml-diagnostics-issue-354
Open

Chiara Mooney (chiaramooney) wants to merge 4 commits into
mainfrom
chiaramooney-xaml-diagnostics-issue-354

Conversation

@chiaramooney

@chiaramooney Chiara Mooney (chiaramooney) commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

The XAML language server reported errors and warnings on markup that the WinUI XAML compiler accepts. Each case is now silent, matching what dotnet build does:

  • Case-variant enum values such as Visibility="collapsed" or Mode=oneway (x:Bind and Binding) are no longer WXAML0012 / WXAML0022 / WXAML0025 errors. A lowercase Mode=twoway still triggers the read-only TwoWay check (WXAML0044).
  • WMC1510 (Native AOT warning for an untyped {Binding} in a DataTemplate) now appears only when the project's evaluated EnableBindingDiagnostics is true. The WinUI targets turn that on for IsAotCompatible or PublishAot, so the editor follows the compiler, including explicit overrides.
  • ToString(), Equals(), GetHashCode(), GetType() on class or struct x:Bind receivers are no longer flagged WXAML0011, even when the class declares its own overloads such as ToString(string) or Equals(MyType). Interface receivers are still flagged because the compiler rejects them.
  • ValueTuple elements Item1 to Item7 and Rest resolve in x:Bind paths (no more WXAML0005), including elements explicitly named Item1. Item8 and later are still flagged because they are not real ValueTuple fields. Named elements such as Pair.Count are now flagged too, because the compiler only sees the underlying ItemN fields; the message names the matching ItemN and the replace quick fix offers it.

Usage Example

<StackPanel>
  <Button Visibility="collapsed" />                                <!-- was WXAML0012 -->
  <TextBlock Text="{x:Bind ViewModel.Name, Mode=oneway}" />          <!-- was WXAML0022 -->
  <TextBlock Text="{x:Bind ViewModel.Selected.ToString()}" />        <!-- was WXAML0011 -->
  <TextBlock Text="{x:Bind ViewModel.Pair.Item2}" />                 <!-- was WXAML0005 -->
  <ListView>
    <ListView.ItemTemplate>
      <!-- WMC1510 now only in projects with IsAotCompatible or PublishAot -->
      <DataTemplate><TextBlock Text="{Binding Name}" /></DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</StackPanel>

Related Issue

Fixes #354

Type of Change

  • 🐛 Bug fix
  • 🧪 Test update

Checklist

  • New tests added for new functionality (if applicable)
  • Ran .\scripts\build-vsce.ps1 locally
  • Tested locally on Windows (if applicable)

Screenshots / Demo

Not applicable. This changes which diagnostics the language server reports; the observed results are under Additional Notes.

Additional Notes

Validation, run locally on the PR head:

  • dotnet test server/test/WinUiXaml.LanguageServer.Tests -c Debug: 1150 passed.
  • dotnet test server/test/WinUiXaml.Workspace.Tests -c Debug (type system, binding-diagnostics evaluation and resolution, WithClassName, unbuilt-reference tests): 81 passed.
  • node server/test/lsp-smoke/smoke.mjs (after dotnet restore test\fixtures\xaml\fixture\SmokeFixture.csproj): passed. The non-AOT fixture now must not report WMC1510.
  • .\scripts\build-vsce.ps1 -Package: compile, lint, unit tests and VSIX packaging passed, including the Native AOT server for each architecture.
  • Real WinUI builds (Windows App SDK 2.2) of a page containing every case above build cleanly. With IsAotCompatible=true, the compiler reports WMC1510, and dotnet msbuild -getProperty:EnableBindingDiagnostics returns true (empty otherwise). The same builds confirmed the compiler rejects Item8 on an 8-element tuple, named tuple elements, and ToString() on an IDisposable-typed receiver, all of which the server flags.

Limitations:

  • No automated end-to-end test covers an AOT project reporting WMC1510; the WinUI targets deriving EnableBindingDiagnostics from IsAotCompatible was verified manually.
  • With AOT on, the compiler also warns on a page-level {Binding} outside any template. The server has never reported that case and this PR does not add it.
  • The Unicode x:Name cases from the issue were already fixed on main; this PR only adds regression test cases for them.
  • Severity of build-breaking diagnostics is tracked separately in [Bug]: Build-breaking XAML errors are published as Warnings #355.

AI Description

This section is auto-generated by AI when the PR is opened or updated. To opt out, delete this entire section including the marker comments.

Accept case-insensitive enum values, inherited object methods and ValueTuple ItemN fields in x:Bind, and report WMC1510 only when the WinUI compiler's EnableBindingDiagnostics is on.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Accept only Item1-Item7 tuple fields, skip object methods on interface receivers, and let object methods through when only an overload with a different parameter count shares the name.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@chiaramooney Chiara Mooney (chiaramooney) added agent-preparing Agent work in progress (feedback, validation, or CI) intellisense labels Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

VSIX Build

Artifact Baseline Current Delta
VS Code Extension (VSIX) 72.55 MB 72.56 MB 📈 +11.8 KB (+0.02%)

Updated 2026-12-01 21:27:22 UTC · commit adf20c2 · workflow run

@chiaramooney Chiara Mooney (chiaramooney) added ready-for-review Agent work and checks complete; awaiting human review and removed agent-preparing Agent work in progress (feedback, validation, or CI) labels Nov 30, 2026
The WinUI compiler only sees real ValueTuple fields, so explicit element names such as Count fail the build. Exclude them from bindable members.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@chiaramooney Chiara Mooney (chiaramooney) added agent-preparing Agent work in progress (feedback, validation, or CI) ready-for-review Agent work and checks complete; awaiting human review and removed ready-for-review Agent work and checks complete; awaiting human review agent-preparing Agent work in progress (feedback, validation, or CI) labels Nov 30, 2026
@Jaylyn-Barbee

Copy link
Copy Markdown
Contributor

PR review

Summary: 0 critical, 0 high, 5 medium, 1 low. Security, docs, packaging and alternative-solution checks found no issues. A second model (GPT) independently confirmed M3 and found no missed critical or high issues.

ID Location Area Issue
M1 server/src/WinUiXaml.Workspace/RoslynProjectWorkspace.cs:221-233 test coverage The setting is only tested through MsBuildCli.Evaluate. The production build path and the workspace/resolution hand-off have no test.
M2 server/src/WinUiXaml.Workspace/XamlResolution.cs:85-99 test coverage No test checks that WithClassName keeps BindingDiagnosticsEnabled.
M3 server/src/WinUiXaml.Workspace/XamlTypeSystem.cs:1066-1071 correctness New false positive: (int Item1, int Item2) Pair with {x:Bind Pair.Item1} is now flagged WXAML0005.
M4 server/src/WinUiXaml.Workspace/XamlTypeSystem.cs:1070-1071 editor UX Named tuple elements now get a generic "not a member" error with no explanation or fix.
M5 server/test/lsp-smoke/smoke-core-scenarios.mjs:1252-1255 test coverage No end-to-end test shows an AOT project still reports WMC1510.
L1 server/src/WinUiXaml.Workspace/XamlTypeSystem.cs:1142-1157 test coverage GetType() is now accepted, and the same-arity hiding rule has no test.

M3: tuple elements named with their default name are rejected (correctness, high confidence)

A tuple element named with its default name (Item1) counts as explicitly named, so the new filter drops the real ValueTuple field.

  • The filter is member is IFieldSymbol { IsExplicitlyNamedTupleElement: true }. In Roslyn 5.0.0 (TupleTypeSymbol.cs L626-682), a field is implicitly declared only when providedName != defaultName. When the given name is Item1, Roslyn creates one field, marks it explicitly named, and creates no separate Item1. Before this PR that field passed the filter, and the WinUI compiler sees the real Item1 field.
  • Suggested fix: member is IFieldSymbol { IsExplicitlyNamedTupleElement: true } && !IsValueTupleItemField(member), plus an (int Item1, int Item2) case in BindPath_ResolvesValueTupleElements.

M1: production MSBuild path and plumbing untested (test coverage, high confidence)

Production reads the setting from the combined build-and-evaluation MSBuild call (MsBuildCli.cs around L364/L403). EvaluationReportsWinUiBindingDiagnosticsSetting only covers Evaluate, and nothing asserts the value on RoslynProjectWorkspace or XamlResolution.

  • Suggested fix: Follow the existing RoslynProjectWorkspaceTests pattern. Load a project with <EnableBindingDiagnostics>true</EnableBindingDiagnostics>, then assert workspace.BindingDiagnosticsEnabled and the value on the resulting XamlResolution.

M2: WithClassName preservation untested (test coverage, high confidence)

The flag is the last argument in a long list of positional optional parameters. If it were dropped, AOT projects would lose WMC1510 after the first XML-only edit, and no test would fail.

  • Suggested fix: UnbuiltProjectReferenceDetectionTests already guards this path for UnresolvedProjectReferences. Add the same check for BindingDiagnosticsEnabled.

M4: unhelpful message for named tuple elements (editor UX, medium confidence)

Pair.Count now reports "'Count' is not a member of 'ValueTuple' bound by x:Bind." That is confusing because Count is valid C#, and the "did you mean" suggestion is likely empty since Count is not close to Item1.

  • Suggested fix: In ReportUnresolvedBindMember (XamlValidator.BindPaths.cs:314), detect named tuple elements, emit a specific message (for example "'Count' is a C# tuple element name that x:Bind cannot see; use 'Item1'"), and suggest CorrespondingTupleField.Name so the existing replace quick fix applies.

M5: no end-to-end positive WMC1510 test (test coverage, high confidence)

The only test that expects WMC1510 calls the validator directly with bindingDiagnosticsEnabled: true. The path from IsAotCompatible through the WinUI targets, MSBuild and the language server is never exercised, so a regression that turns the warning off for every AOT user would pass CI.

  • Suggested fix: Add a smoke scenario (or AOT variant of SmokeFixture) with <IsAotCompatible>true</IsAotCompatible> that expects exactly one WMC1510 for the untyped template {Binding}.

L1: GetType() and same-arity hiding untested (test coverage, medium confidence)

The System.Object fallback also accepts GetType(). The WinUI compiler's method lookup has no GetType exclusion, so this looks consistent, but it was not verified against a real build. The "same parameter count hides the object method" rule also has no test (FormatOnly.ToString(string) only covers the different-arity case).

  • Suggested fix: Add a Child.GetType() case and a receiver with a same-arity overload such as Equals(Child).

@chiaramooney Chiara Mooney (chiaramooney) added agent-preparing Agent work in progress (feedback, validation, or CI) and removed ready-for-review Agent work and checks complete; awaiting human review labels Dec 1, 2026
Accept tuple elements explicitly named ItemN, stop hiding object methods behind same-arity overloads (only overrides hide them), and point named tuple elements at their ItemN field with a quick-fix suggestion. Add coverage that the binding-diagnostics setting survives the production load path and WithClassName.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@chiaramooney

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in adf20c2, apart from M5:

  • M3: Fixed. Elements explicitly named Item1 to Item7 are real ValueTuple fields, so they are kept. Added (int Item1, int Item2) to BindPath_ResolvesValueTupleElements. A real WinUI build confirms {x:Bind Pair.Item1} compiles.
  • L1: This was a bug, not just missing coverage. A real build accepts Same.Equals(Model.Name) when the class declares Equals(SameArity), so the compiler does not hide object methods behind same-arity overloads. Only an override (same parameter types) now hides them. Added the Equals overload case and a GetType() case; the real build accepts GetType() too.
  • M4: Named elements now report "'Label' is a C# tuple element name that x:Bind cannot see. Use 'Item2'." The diagnostic suggests the ItemN name, so the existing replace quick fix offers it.
  • M2: Added WithClassNameKeepsBindingDiagnosticsSetting, next to the existing UnresolvedProjectReferences check.
  • M1: Added ResolutionCarriesTheProjectsBindingDiagnosticsSetting. It loads a project with EnableBindingDiagnostics=true through XamlProjectResolver.ResolveAsync, which uses the production evaluate-and-compile path, and asserts the value on XamlResolution.
  • M5: Not added in this PR. M1 now covers the MSBuild-to-resolution path. What remains untested end to end is the WinUI targets deriving EnableBindingDiagnostics from IsAotCompatible. That was verified manually with a real build and -getProperty, but it needs an AOT smoke scenario, which we're leaving out of this PR.

Locally: LanguageServer tests 1150 passed, targeted Workspace tests 81 passed, LSP smoke passed.

@chiaramooney Chiara Mooney (chiaramooney) added ready-for-review Agent work and checks complete; awaiting human review and removed agent-preparing Agent work in progress (feedback, validation, or CI) labels Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

intellisense ready-for-review Agent work and checks complete; awaiting human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: XAML diagnostics false positives on valid markup

2 participants