Skip to content

Roslyn analyzer: warn when UseIconFonts() is not called on MauiAppBuilder #2

@jfversluis

Description

@jfversluis

Problem

When a consumer references an icon font package and uses glyphs in XAML/C#, but forgets to call .UseIconFonts() (or the per-font .UseBootstrapIcons() etc.) on their MauiAppBuilder, the fonts won't render at runtime. The IMauiInitializeService fallback may catch this, but it's still a confusing experience.

Proposed Solution

Ship a Roslyn DiagnosticAnalyzer alongside the source generator that detects this at compile time.

Detection logic

  1. Scan for methods returning MauiApp (the standard CreateMauiApp() pattern)
  2. Check if any call to UseIconFonts() or a per-font Use{FontClass}() method exists
  3. If the icon font package is referenced but no initialization call is found → emit warning
ICONFONT001: Icon font package is referenced but UseIconFonts() was not called on MauiAppBuilder.
             Add .UseIconFonts() to your MauiAppBuilder in MauiProgram.cs.

Severity

DiagnosticSeverity.Warning — not an error, since the IMauiInitializeService fallback will still register the fonts. This is a best-practice nudge.

Approach Options

Approach Pros Cons
Roslyn Analyzer (recommended) Compile-time, zero runtime cost, ships in same NuGet Cannot inspect XAML directly, only C# syntax trees
Runtime #if DEBUG check Simple, catches it at startup Only at runtime, requires app to run
Static flag in UseIconFonts() Very simple Only runtime, same limitation

The Roslyn Analyzer approach is preferred. It can be scoped to: "if the project has any IconFontDefinition items (or MauiFont items with the generator) and no UseIconFonts() call exists anywhere in the assembly, emit the warning."

Packaging

The analyzer DLL ships in the same NuGet package as the source generator (in the analyzers/ folder). No additional package reference needed.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions