dotnet / dotnet/aspnetcore

JSRuntime.Invoke APIs aren't correctly annotated for trimming

Open
#39,839 14 comments 3 reactions 0 assignees View on GitHub
area-blazor enhancement feature-blazor-jsinterop feature-blazor-wasm feature-trimming Pillar: Technical Debt Priority:1
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

Follow up to https://github.com/dotnet/aspnetcore/pull/39838. JSRuntime's API (excluding the unmarshalled ones) use JSON serialization to serialize the args array and as such are subject to being trimmed away. S.T.Js API for its serialization are annotated with RequiresUnreferencedCode with a recommendation to use the source-generator based overload to avoid this.

Blazor's APIs suppress STJ's warnings (since it happens deep in its bowels) and don't have a trimmer safe alternative. Perhaps one option is to annotate all of the JSRuntime APIs with `RequiresUnferencedCode` and add an JSRuntime.InvokeAsync overload that accepts exactly one argument a `JsonTypeInfo` to go with it. Something like so:

```C#
TValue IJSRuntime.InvokeAsync(string method, TArg arg, JsonTypeInfo t1, JsonTypeInfo t2, CancellationToken cancellationToken)
```

At the very least, we could require that all code in the framework uses this API for it's interop.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.