Repository navigation
Emit SupportedOSPlatform("browser") on generated JSImport partials - #131055
Conversation
JSImportAttribute carries [SupportedOSPlatform("browser")], but placing
that attribute on the attribute type has no effect on the methods it
annotates, so CA1416 never fires when a JSImport method is called from
non-browser-reachable code.
Have the JSImport source generator apply
[SupportedOSPlatform("browser")] to the generated partial method
implementation instead. Partial-method attribute merging then makes the
combined method browser-only, so the platform-compatibility analyzer
reports CA1416 as expected.
Updates the generated-source verification tests accordingly.
Fixes #121165.
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: ca1f38e1-9120-4345-ad8d-55c09a2407c5
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Updates the JSImport source generator to emit [SupportedOSPlatform("browser")] directly on generated partial method implementations so platform compatibility analysis (CA1416) correctly applies to JSImport methods.
Changes:
- Add
[SupportedOSPlatform("browser")]to generated JSImport partial method implementations. - Introduce generator constants for the SupportedOSPlatform attribute and
"browser"platform string. - Update generated-source verification snapshots to include the new attribute.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Runtime.InteropServices.JavaScript/tests/JSImportGenerator.UnitTest/Compiles.cs | Updates expected generated source to include [SupportedOSPlatform("browser")]. |
| src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSImportGenerator.cs | Emits the SupportedOSPlatform attribute on generated partial method implementations. |
| src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Constants.cs | Adds constants for SupportedOSPlatform and the "browser" platform value. |
- Use ParseName for the fully-qualified attribute names so the generated syntax tree is structurally correct instead of a single odd identifier token. - Build the method's attribute lists in a single WithAttributeLists call. The previous code called AddAttributeLists(AdditionalAttributes) and then immediately replaced the whole list via WithAttributeLists, so the AdditionalAttributes were silently discarded. Emitting those attributes (GeneratedCode/SkipLocalsInit) is a separate behavior change and is out of scope here, so the dead call is removed. Generated output is unchanged aside from the new SupportedOSPlatform attribute. Co-authored-by: Copilot App <[email protected]> Copilot-Session: ca1f38e1-9120-4345-ad8d-55c09a2407c5
|
Workflow state for the Holistic Review Orchestrator. {
"version": 5,
"last_dispatched_commit": "2b6b9eeb851fca3b6b3fbbd7a3b86086b958c82c",
"last_dispatched_base_ref": "main",
"last_dispatched_base_sha": "563abf220847ed66cab58f0d7d166cb264874f40",
"last_reviewed_commit": "2b6b9eeb851fca3b6b3fbbd7a3b86086b958c82c",
"last_reviewed_base_ref": "main",
"last_reviewed_base_sha": "563abf220847ed66cab58f0d7d166cb264874f40",
"last_recorded_worker_run_id": "29714511116",
"review_attempt_commit": "",
"review_attempt_base_ref": "",
"review_attempt_count": 0,
"max_review_attempts": 5,
"review_history_format": "holistic-review-disclosure-v1",
"review_history": [
{
"commit": "2b6b9eeb851fca3b6b3fbbd7a3b86086b958c82c",
"review_id": 4732017077
}
]
} |
There was a problem hiding this comment.
Holistic Review
Motivation: The problem is real and well-diagnosed. [SupportedOSPlatform("browser")] on the JSImportAttribute type has no effect on the annotated methods, so CA1416 never fires when a JSImport method is reachable from non-browser code (issue #121165). Emitting the attribute on the generated partial implementation restores the intended platform-compatibility diagnostic.
Approach: Correct. Applying [SupportedOSPlatform("browser")] to the generated partial method body relies on partial-method attribute merging to make the combined method browser-only, which is exactly how the analyzer expects platform annotations to be conveyed. Leaving JSExport unchanged is justified in the PR description (the user authors the non-partial method; the generator emits no declaration to decorate).
Summary: ✅ LGTM. The change is minimal, correct, and the two generated-source snapshot tests were updated to assert the new attribute. No blocking issues found.
Detailed Findings
✅ Correctness — No regression from the removed AddAttributeLists call
The original code chained .AddAttributeLists(stub.SignatureContext.AdditionalAttributes.ToArray()) immediately followed by .WithAttributeLists(SingletonList(...)). Because WithAttributeLists replaces the entire attribute-list collection, the AdditionalAttributes (e.g. GeneratedCodeAttribute) were already being discarded before this PR — the existing snapshots emit only [DebuggerNonUserCode], confirming this. The new code builds a single List(...) of both attribute lists, preserving the prior behavior while adding SupportedOSPlatform, so removing the dead AddAttributeLists call is safe and not a behavioral change.
✅ Style — ParseName vs IdentifierName
Switching from IdentifierName(Constants.DebuggerNonUserCodeAttribute) to ParseName(...) is more correct for the fully-qualified global::-prefixed names, and matches the new SupportedOSPlatform usage. Consistent and low-risk.
✅ Test quality — Snapshot coverage
Both affected generated-source verification snapshots (Annotated, Import1) were updated to include the new attribute, and the PR notes 22/22 unit tests pass. This is the appropriate test surface for a source-generator output change.
💡 Observation — Ineffective class-level attribute left in place
As the author notes, the class-level [SupportedOSPlatform("browser")] on JSImportAttribute/JSExportAttribute remains (harmless but ineffective). Leaving it avoids public-ref churn; removing it would be a reasonable follow-up but is out of scope here.
Note
This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.
Generated by Holistic Review · 59.1 AIC · ⌖ 14.8 AIC · ⊞ 10K
…System.IsBrowser() Fixes CA1416 introduced by the runtime JSImportGenerator change (dotnet/runtime#131055) that stamps [SupportedOSPlatform("browser")] on generated [JSImport] partial stubs. Microsoft.JSInterop is a plain SDK library with warnings-as-errors, so the unguarded call to the generated DisposeJSObjectReferenceById partial tripped CA1416. Guarding with OperatingSystem.IsBrowser() is behavior-neutral since JSInProcessObjectReference is only constructed by the WASM in-process runtime. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 30f68f14-f3e6-47e5-8344-dffb2b3d1952
…owser
Fixes CA1416 across Microsoft.JSInterop.WebAssembly and Microsoft.AspNetCore.Components.WebAssembly caused by the runtime JSImportGenerator SupportedOSPlatform("browser") change (dotnet/runtime#131055). These assemblies only run on the browser (Mono WebAssembly) runtime, so narrowing SupportedPlatform to browser resolves every generated [JSImport] call site at once.
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 30f68f14-f3e6-47e5-8344-dffb2b3d1952
…ject NoWarn
The SupportedPlatform Remove/Include browser narrowing does not suppress CA1416 in this SDK. Replace it with a per-project NoWarn on Microsoft.JSInterop.WebAssembly and Microsoft.AspNetCore.Components.WebAssembly, which is zero-cascade (an attribute-based SupportedOSPlatform narrowing would cascade CA1416 into the host-side Components.WebAssembly.Server that references these assemblies). Addresses CA1416 from the runtime JSImportGenerator SupportedOSPlatform("browser") change (dotnet/runtime#131055).
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 30f68f14-f3e6-47e5-8344-dffb2b3d1952
Fixes #121165.
Problem
JSImportAttributecarries[SupportedOSPlatform("browser")], but placing that attribute on the attribute type has no effect on the methods it annotates. As a result,CA1416never fires when aJSImportmethod is called from code that is reachable on non-browser platforms — the platform guidance the attribute was meant to convey is silently lost.Fix
Have the JSImport source generator apply
[SupportedOSPlatform("browser")]to the generated partial method implementation instead. Partial-method attribute merging then makes the combined method browser-only, so the platform-compatibility analyzer reportsCA1416as expected when the method is used from non-browser code.Notes
JSExportAttributehas the same ineffective class-level[SupportedOSPlatform("browser")], butJSExportis applied to a user-authored non-partial method — the generator only emits wrapper/registration code, not the method itself, so there is no generated declaration to decorate.[SupportedOSPlatform("browser")]onJSImportAttribute/JSExportAttributewas left in place. It is ineffective for the annotated-method scenario but harmless, and removing it would churn the public ref surface. Happy to remove it if preferred.Testing
clr+libsbuild succeeded.JSImportGenerator.Unit.Tests: 22/22 pass, including the two generated-source verification snapshots (Annotated,Import1) updated to include the new attribute.Note
This pull request was authored with the assistance of GitHub Copilot.