Skip to content

Add Roslyn analyzer for actor registration#1441

Merged
WhitWaldo merged 13 commits intodapr:release-1.16from
ngruson:1426-ng-analyzer-actor
Apr 9, 2025
Merged

Add Roslyn analyzer for actor registration#1441
WhitWaldo merged 13 commits intodapr:release-1.16from
ngruson:1426-ng-analyzer-actor

Conversation

@ngruson
Copy link
Contributor

@ngruson ngruson commented Jan 11, 2025

Description

This PR adds a Roslyn analyzer that checks if actors are registered properly.
For every actor in the project, the analyzer checks if there's a corresponding RegisterActor call in the application setup.

If there's no AddActors call yet, it will add it as well. The analyzer will look for WebApplication.CreateBuilder and adds the actor registration directly below it.

  • Introduced Dapr.Actors package reference.
  • Added Microsoft.CodeAnalysis.CSharp.Analyzer.Testing package reference (v1.1.2).
  • Created Dapr.Actors.Analyzers and Dapr.Actors.Analyzers.Test projects.
  • Implemented ActorRegistrationAnalyzer to warn about unregistered actors.
  • Developed ActorRegistrationCodeFixProvider to suggest actor registration.
  • Added unit tests for both the analyzer and code fix provider.
  • Enhanced test project with necessary references and utility methods.

Issue reference

This PR belongs to issue #1426.

Checklist

Please make sure you've completed the relevant tasks for this PR, out of the following list:

  • Code compiles correctly
  • Created/updated tests
  • Extended the documentation

- Introduced `Dapr.Actors` package reference (v1.15.0-rc01).
- Added `Microsoft.CodeAnalysis.CSharp.Analyzer.Testing` package reference (v1.1.2).
- Created `Dapr.Actors.Analyzers` and `Dapr.Actors.Analyzers.Test` projects.
- Implemented `ActorRegistrationAnalyzer` to warn about unregistered actors.
- Developed `ActorRegistrationCodeFixProvider` to suggest actor registration.
- Added unit tests for both the analyzer and code fix provider.
- Updated documentation for analyzer releases.
- Enhanced test project with necessary references and utility methods.

Signed-off-by: Nils Gruson <ngruson@hotmail.com>
@ngruson ngruson requested review from a team as code owners January 11, 2025 10:04
@WhitWaldo WhitWaldo added this to the v1.16 milestone Jan 11, 2025
ngruson and others added 6 commits January 13, 2025 10:53
Renamed `ActorRegistrationAnalyzer` to `ActorAnalyzer` and enhanced it to analyze JSON serialization options for actors. Updated the code fix provider to `ActorJsonSerializationCodeFixProvider` to enable JSON serialization when not set. Introduced new diagnostic `DAPR0002` for interoperability with non-.NET actors. Updated project references and added tests for the new functionality while improving code structure and readability.

Signed-off-by: Nils Gruson <ngruson@hotmail.com>
Introduce a new diagnostic rule (DAPR0003) that warns users to call `app.MapActorsHandlers` for Dapr actors. Implemented in the `ActorAnalyzer` class with a corresponding code fix provider (`MapActorsHandlersCodeFixProvider`) to automatically add the call when needed.

Updated `SupportedDiagnostics` to include DAPR0003 and added the `AnalyzeMapActorsHandlers` method for syntax tree analysis. Introduced `FindInvocation` to locate method calls in the syntax tree.

Added tests in `ActorAnalyzerTests` and `MapActorsHandlersCodeFixProviderTests` to validate the new functionality, covering various scenarios. Updated `Utilities` and `VerifyAnalyzer` classes to include necessary assembly references for compatibility across .NET versions.

Made minor adjustments to existing code and tests for consistency.

Signed-off-by: Nils Gruson <ngruson@hotmail.com>
Simplify handling of createBuilderInvocation and buildInvocation by removing unnecessary nested checks. Improve code readability with clearer conditional blocks. Ensure the document is returned with the modified syntax root after changes.

Signed-off-by: Nils Gruson <ngruson@hotmail.com>
@WhitWaldo WhitWaldo added kind/enhancement New feature or request needs-review labels Feb 28, 2025
@WhitWaldo WhitWaldo changed the base branch from master to release-1.16 March 10, 2025 22:08
private static readonly DiagnosticDescriptor DiagnosticDescriptorActorRegistration = new(
"DAPR0001",
"Actor class not registered",
"The actor class '{0}' is not registered",
Copy link
Contributor

Choose a reason for hiding this comment

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

We'll likely need to revisit this in the future as there's consideration for facilitating cross-service actor invocation, so it might very well be the case that an actor is referenced that doesn't exist in the same service.

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
…ed in a future release

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
…split out analyzers to separate classes and updated tests accordingly. Update to rule ID numbering and verbiage.

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
@WhitWaldo
Copy link
Contributor

@ngruson I've refactored your solution a bit to fit in with the current bits on 1.16 and to put the analyzers in separate files.

Otherwise, this looks good to go. Thank you very much for your contribution!

@WhitWaldo WhitWaldo merged commit 76bfed2 into dapr:release-1.16 Apr 9, 2025
8 checks passed
@WhitWaldo WhitWaldo moved this from Backlog to Done in v1.16 Release Tracking Board Apr 9, 2025
@ngruson ngruson deleted the 1426-ng-analyzer-actor branch April 10, 2025 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/actor kind/enhancement New feature or request

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants