Skip to content

Commit 59b2e51

Browse files
committed
chore: .NET 9 build fixes
Fix a JSON trimmer error: src/Uno.Extensions.Authentication.MSAL/MsalAuthenticationProvider.cs(249,52): error IL2026: Using member 'System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. Update `MsalAuthenticationProvider.cs` to use `System.Text.Json` [Source Generators][0]. "Fix" or otherwise silence trimmer errors (?!): uno.extensions/src/Uno.Extensions.Hosting.UI/UnoHost.cs(105,42): error IL2026: Using member 'Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure<TOptions>(IServiceCollection, IConfiguration)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. TOptions's dependent types may have their members trimmed. Ensure all required members are preserved. In this case I'm not sure what the correct fix *is*, so use `[UnconditionalSuppressMessage]` to silence the underlying warning. src/Uno.Extensions.Navigation.UI/RouteResolverDefault.cs(261,11): error IL2026: Using member 'System.Reflection.Assembly.GetTypes()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed. Add `[RequiresUnreferencedCode]` to `AssemblyExtensions.SafeGetTypes()` and `RouteResolverDefault.LoadedTypes` (calls `.SafeGetTypes()`), then use `[UnconditionalSuppressMessage]` to "limit the contagion" of the `[RequiresUnreferencedCode]` on `.LoadedTypes`. [0]: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation
1 parent 1562b28 commit 59b2e51

3 files changed

Lines changed: 49 additions & 7 deletions

File tree

src/Uno.Extensions.Authentication.MSAL/MsalAuthenticationProvider.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using Uno.Extensions.Logging;
55
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
66
#if UNO_EXT_MSAL
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
using System.Text.Json.Serialization.Metadata;
710
using MsalCacheHelper = Microsoft.Identity.Client.Extensions.Msal.MsalCacheHelper;
811
#endif
912

@@ -246,7 +249,7 @@ private ValueTask<AuthenticationResult> AcquireInteractiveTokenAsync(IDispatcher
246249
try
247250
{
248251
Logger.LogInformation("Attempting to perform silent sign in . . .");
249-
Logger.LogInformation($"Authentication Scopes: {JsonSerializer.Serialize(_scopes)}");
252+
Logger.LogInformation($"Authentication Scopes: {ToJson(_scopes)}");
250253

251254
Logger.LogInformation($"Account Name: {firstAccount.Username}");
252255

@@ -268,5 +271,24 @@ private ValueTask<AuthenticationResult> AcquireInteractiveTokenAsync(IDispatcher
268271

269272
return default;
270273
}
274+
275+
static string? ToJson (string[]? values)
276+
{
277+
if (values == null)
278+
{
279+
return null;
280+
}
281+
282+
return JsonSerializer.Serialize(values, StringArrayJsonSerializerContext.Default.StringArray);
283+
}
284+
271285
#endif
272286
}
287+
288+
#if UNO_EXT_MSAL
289+
[JsonSourceGenerationOptions]
290+
[JsonSerializable(typeof(string[]))]
291+
internal sealed partial class StringArrayJsonSerializerContext : JsonSerializerContext
292+
{
293+
}
294+
#endif // UNO_EXT_MSAL

src/Uno.Extensions.Hosting.UI/UnoHost.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Uno.Extensions.Hosting;
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace Uno.Extensions.Hosting;
24

35
/// <summary>
46
/// Contains helpers to create a HostBuilder that is tailored to multiple target platforms.
@@ -59,7 +61,7 @@ public static IHostBuilder CreateDefaultBuilder(Assembly applicationAssembly, st
5961
{
6062
PlatformHelper.SetAppAssembly(applicationAssembly);
6163
applicationAssembly = PlatformHelper.GetAppAssembly()!;
62-
return new HostBuilder()
64+
var builder = new HostBuilder()
6365
.ConfigureCustomDefaults(args)
6466
.ConfigureAppConfiguration((ctx, appConfig) =>
6567
{
@@ -102,7 +104,15 @@ public static IHostBuilder CreateDefaultBuilder(Assembly applicationAssembly, st
102104
config.AddInMemoryCollection(queryDict);
103105
}
104106
})
105-
.ConfigureServices((ctx, services) => services.Configure<HostConfiguration>(ctx.Configuration.GetSection(nameof(HostConfiguration))))
106107
.UseStorage();
108+
return ConfigureHostConfigurationServices(builder);
109+
110+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "🤷‍♂️")]
111+
static IHostBuilder ConfigureHostConfigurationServices(IHostBuilder builder)
112+
{
113+
builder
114+
.ConfigureServices((ctx, services) => services.Configure<HostConfiguration>(ctx.Configuration.GetSection(nameof(HostConfiguration))));
115+
return builder;
116+
}
107117
}
108118
}

src/Uno.Extensions.Navigation.UI/RouteResolverDefault.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Reflection;
23

34
namespace Uno.Extensions.Navigation;
45

@@ -160,14 +161,14 @@ private RouteInfo[] DefaultMapping(string? path = null, Type? view = null, Type?
160161
return default;
161162
}
162163

163-
if (allowMatchExact && LoadedTypes.TryGetValue(path, out var type))
164+
if (allowMatchExact && TryGetLoadedType(path, out var type))
164165
{
165166
return type;
166167
}
167168

168169
foreach (var suffix in suffixes)
169170
{
170-
if (LoadedTypes.TryGetValue($"{path}{suffix}", out type))
171+
if (TryGetLoadedType($"{path}{suffix}", out type))
171172
{
172173
if (condition?.Invoke(type) ?? true)
173174
{
@@ -179,6 +180,12 @@ private RouteInfo[] DefaultMapping(string? path = null, Type? view = null, Type?
179180
Logger.LogWarningMessage($"Navigation failed: Could not resolve type for path '{path}'.");
180181

181182
return null;
183+
184+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "LoadedTypes has the message; suppress use to limit contagion.")]
185+
bool TryGetLoadedType(string path, [NotNullWhen (true)] out Type? type)
186+
{
187+
return LoadedTypes.TryGetValue(path, out type);
188+
}
182189
}
183190

184191
private string PathFromTypes(Type? view, Type? viewModel)
@@ -235,6 +242,7 @@ private string TrimSuffices(string? path, IEnumerable<string> suffixes)
235242

236243
public IDictionary<string, Type> LoadedTypes
237244
{
245+
[RequiresUnreferencedCode("From Assembly.GetTypes(): Types might be removed")]
238246
get
239247
{
240248
if (loadedTypes is null)
@@ -254,6 +262,8 @@ where t.IsClass
254262
public static class AssemblyExtensions
255263
{
256264
public static IList<string> Excludes { get; } = new List<string>();
265+
266+
[RequiresUnreferencedCode("From Assembly.GetTypes(): Types might be removed")]
257267
public static Type[] SafeGetTypes(this Assembly assembly)
258268
{
259269
try

0 commit comments

Comments
 (0)