Repository navigation
Description
Current behavior 🐛
The XAML source generator writes a registration into the app's generated code for every [assembly: ApiExtension(...)] found on a referenced assembly. It emits the types as typeof(global::X) / new global::Y(o) without checking that they are accessible from the app. If the extended type or the extension type is internal (or otherwise not visible to the app), the app fails to build:
error CS0122: 'IMyExtension' is inaccessible due to its protection level
error CS0122: 'MyExtension' is inaccessible due to its protection level
The generated line that fails is ApiExtensibility.Register(typeof(global::Lib.IMyExtension), o => new global::Lib.MyExtension(o));, placed in the generated code of the app's App class.
The attribute itself compiles fine in the library that declares it, so the author of the library gets no warning. Every app that references the library breaks instead.
Expected behavior 🎯
Either:
- the generator skips (and reports a diagnostic for) registrations whose types are not accessible from the consuming compilation, or
- it emits a reflection-based registration for them, or
- the attribute's contract (public types only) is documented and enforced with an analyzer in the declaring assembly.
A library's [ApiExtension] should not be able to break the build of every app that references it.
How to reproduce it (as minimally and precisely as possible) 🔬
- Create a class library
Lib(Uno.Sdk,net10.0) with this file:
using Uno.Foundation.Extensibility;
[assembly: ApiExtension(typeof(Lib.IMyExtension), typeof(Lib.MyExtension))]
namespace Lib;
internal interface IMyExtension
{
}
internal sealed class MyExtension : IMyExtension
{
public MyExtension(object owner)
{
}
}- Create an Uno app
App(Uno.Sdk,net10.0-desktop,UnoSingleProject=true) with a plainApp.xaml/App.xaml.cs, and add<ProjectReference Include="..\Lib\Lib.csproj" />. dotnet buildthe app.
Result: error CS0122 for both IMyExtension and MyExtension in the generated App_<hash>.cs file (a CS5001 about the missing entry point also shows up, which is just because the repro has no Main).
Only the internal case was built; other inaccessible cases (for example private nested types) should fail the same way by inspection, since the emitted code is identical.
This was run as a real build, against Uno.Sdk 6.7.22. The generator code is the same on current master.
Workaround 🛠️
Do not use [assembly: ApiExtension] with non-public types. Register from the library's own runtime code instead, for example through Type.GetType("<type>, <assembly>") and Activator.CreateInstance, or make the extension types public.
Renderer 🎨
Skia
Affected platforms 📱💻🖥️
All platforms 🌍 (any head whose app references an assembly with [assembly: ApiExtension] on non-public types)
Uno.Sdk version (and other relevant versions) 📦
Uno.Sdk 6.7.22, net10.0 / net10.0-desktop. The generator code is unchanged on master at bfc8dff.
IDE version 🧑💻
Command line, dotnet build (.NET SDK 10.0.400).
Anything else we need to know? 💬
Confirmed by build (see above). The root cause is from source inspection.
XamlFileGenerator.GenerateApiExtensionRegistrations reads the constructor arguments of every ApiExtensionAttribute on every referenced assembly, and formats registration.ElementAt(n).Value (an ITypeSymbol) straight into global::{type} strings at L680, L684, L694 and L698. Nothing looks at the symbol's DeclaredAccessibility or calls Compilation.IsSymbolAccessibleWithin.
This only works inside the Uno repository because the internal types are visible there through InternalsVisibleTo. It is invisible to add-in authors until an app consumes the package.
Suggested fix: in the loop, resolve the two (or three) type arguments to ITypeSymbols, and skip the registration when any of them is not accessible from the app compilation (Compilation.IsSymbolAccessibleWithin(symbol, Compilation.Assembly)), reporting a warning diagnostic that names the attribute and the assembly. A reflection-based registration would be the alternative if inaccessible types must keep working.
Related but different: #22568 (the attribute on the app's own assembly is not picked up, because only external references are scanned).
Activity
fix(xamlgen): Skip ApiExtension registrations of inaccessible types
fix(xamlgen): Skip ApiExtension registrations of inaccessible types
fix(xamlgen): Skip ApiExtension registrations of inaccessible types
fix(xamlgen): Skip ApiExtension registrations of inaccessible types