Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Roslyn analyzer for actor registration #1441

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

ngruson
Copy link

@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 <[email protected]>
@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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants