Skip to content

Add JSInterop analyzer ensuring 'IsInteractive' is checked before using it. - #67969

Open
skrustev wants to merge 39 commits into
dotnet:mainfrom
skrustev:skrastev/jsinterop-interactive-analyzer
Open

skrustev wants to merge 39 commits into
dotnet:mainfrom
skrustev:skrastev/jsinterop-interactive-analyzer

Conversation

@skrustev

@skrustev skrustev commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Add JSInterop analyzer ensuring 'IsInteractive' is checked before using it.

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

Add analyzer that shows warning when JSInterop calls like InvokeAsync are done outside OnAfterRender/OnAfterRenderAsync without checking for RendererInfo.IsInteractive.

Fixes #65233

@skrustev
skrustev requested a review from a team as a code owner July 23, 2026 07:09
Copilot AI review requested due to automatic review settings July 23, 2026 07:09

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

This PR introduces a new Blazor analyzer diagnostic (BL0017) intended to warn when JavaScript interop is invoked outside OnAfterRender/OnAfterRenderAsync without first ensuring the renderer is interactive via RendererInfo.IsInteractive.

Changes:

  • Added JsInteropUsageWithoutCheckAnalyzer to detect (unguarded) JS interop usage outside OnAfterRender*.
  • Added BL0017 descriptor + localized resources, and extended analyzer API constants.
  • Added a new analyzer test suite and documented BL0017 in the diagnostics list.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
src/Components/Analyzers/test/JsInteropUsageWithoutCheckAnalyzerTest.cs Adds coverage for the new analyzer’s guard-detection behavior.
src/Components/Analyzers/src/Resources.resx Adds BL0017 title/format/description strings.
src/Components/Analyzers/src/JsInteropUsageWithoutCheckAnalyzer.cs Implements the new analyzer logic and flow/guard tracking.
src/Components/Analyzers/src/DiagnosticDescriptors.cs Registers the new BL0017 diagnostic descriptor.
src/Components/Analyzers/src/ComponentsApi.cs Adds metadata constants for RendererInfo/JSInterop types.
docs/list-of-diagnostics.md Documents BL0017 in the public diagnostics list.

Comment thread docs/list-of-diagnostics.md Resolved
@dariatiurina

dariatiurina commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

@skrustev

Thank you for this work!

I haven't read it in details, but wouldn't this analyzer throw false positive when we have no prerendering? E.g. in the case of component having @rendermode @(new InteractiveServerRenderMode(prerender: false)), we are always in interactive context (so we don't need to check RendererInfo.IsInteractive) and don't need to have JsInterop in OnAfterRender.

@skrustev

skrustev commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor Author

@dariatiurina Yeah, just as I was PR-ing I figured it would need to check the render mode that is for the component or if set globally. Also since WebAssembly from my understanding is only in interactive mode anyway, so will need to see how can that be checked as well. Not sure if there's some different API to check that, but I am looking into it.

@dariatiurina

Copy link
Copy Markdown
Contributor

@dariatiurina Yeah, just as I was PR-ing I figured it would need to check the render mode that is for the component or if set globally. Also since WebAssembly from my understanding is only in interactive mode anyway, so will need to see how can that be checked as well. Not sure if there's some different API to check that, but I am looking into it.

But then we also need to check for the inner component's rendermode that gets inherited from the parent component, which we, as far as I know, can't do in the analyzer.

@ilonatommy

Copy link
Copy Markdown
Member

@dariatiurina Yeah, just as I was PR-ing I figured it would need to check the render mode that is for the component or if set globally. Also since WebAssembly from my understanding is only in interactive mode anyway, so will need to see how can that be checked as well. Not sure if there's some different API to check that, but I am looking into it.

But then we also need to check for the inner component's rendermode that gets inherited from the parent component, which we, as far as I know, can't do in the analyzer.

I think we can afford false positives in this case, building the whole component dependency tree would be an overkill. I think checking component's own settings should be enough. When setting the rules we should take into consideration that false positive is just a harmless, redundant warning. False negative, on the other hand is a real, runtime crash risk. From this reason I would just check if we're wrapped in IsInteractive block. I'm not 100% aure about component's own RenderModeAttribute because in case when parent is setting prerendering:true, child's prerendering:false is not effective - a false negative risk.

nit: WebAssembly is not the only interactive mode, there's also Server interactivity (we're not loading WASM bundle to the browser but talking over SignalR circuit to the server).

@skrustev

skrustev commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor Author

@Youssef1313 @dariatiurina @ilonatommy
Sorry for the delay, I was thinking what approach would be best and I just cannot find a reliable way to deterministically find out which methods are gonna be called on the server as well and which only on the Client during build time.

The only somewhat sure way to limit the cases we want to warn is to warn for OnInitialized/OnParametersSet/EventHandlers. From my understanding they are almost sure to trigger in the prerendering phases and cover the most cases that could cause issues? This sure makes things a bit more complicated though to know which methods are called where and find their implementation.

Otherwise the other solution I see is to leave it as is and warn for every case(where it is not checked with IsInteractive) and let the user decide if he wants to disable the warning for certain lines or overall?

@skrustev
skrustev requested a review from SamMonoRT as a code owner August 19, 2026 10:17
@Youssef1313

Copy link
Copy Markdown
Member

If we want to get this in .NET 11, we should either remember to backport after merging, or simply target release/11.0.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 9, 2026
@kotlarmilos

Copy link
Copy Markdown
Member

@skrustev The earlier control-flow issue still appears to be present. Could you revisit the unsafe cases and JS calls inside conditions?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] Warn when performing JS interop outside of OnAfterRender(Async)() without checking RendererInfo.IsInteractive

8 participants