Skip to content

Emit SupportedOSPlatform("browser") on generated JSImport partials - #131055

Merged
lewing merged 2 commits into
mainfrom
lewing-animated-journey
Jul 20, 2026
Merged

lewing merged 2 commits into
mainfrom
lewing-animated-journey

Conversation

@lewing

@lewing lewing commented Jul 20, 2026

Copy link
Copy Markdown
Member

Fixes #121165.

Problem

JSImportAttribute carries [SupportedOSPlatform("browser")], but placing that attribute on the attribute type has no effect on the methods it annotates. As a result, CA1416 never fires when a JSImport method 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 reports CA1416 as expected when the method is used from non-browser code.

Notes

  • JSExport is intentionally not changed. JSExportAttribute has the same ineffective class-level [SupportedOSPlatform("browser")], but JSExport is 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.
  • The class-level [SupportedOSPlatform("browser")] on JSImportAttribute/JSExportAttribute was 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

  • Baseline clr+libs build 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.

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
Copilot AI review requested due to automatic review settings July 20, 2026 01:45
@lewing
lewing requested a review from pavelsavara as a code owner July 20, 2026 01:45
@azure-pipelines

Copy link
Copy Markdown
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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copilot AI review requested due to automatic review settings July 20, 2026 03:10
@github-actions

github-actions Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

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
    }
  ]
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@lewing
lewing merged commit b02a4fe into main Jul 20, 2026
96 checks passed
@lewing
lewing deleted the lewing-animated-journey branch July 20, 2026 14:52
lewing added a commit to dotnet/dotnet that referenced this pull request Jul 23, 2026
…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
lewing added a commit to dotnet/dotnet that referenced this pull request Jul 23, 2026
…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
lewing added a commit to dotnet/dotnet that referenced this pull request Jul 24, 2026
…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
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSImport and SupportedOSPlatform("browser")

3 participants