Repository navigation
Add DM0026 and a code fix that makes the service type explicit - #126
Merged
Merged
Conversation
ServiceTypeAnalyzer reports [SingletonService], [ScopedService] and [TransientService] without As = typeof(...). It is off by default. It computes the inferred type with ServiceTypeInference and passes it to the code fix in the diagnostic's properties, so the fix writes what the generator registers and the code fix assembly references only Roslyn. The code fix is in DependencyModules.CodeFixes, because a generator assembly must not reference Microsoft.CodeAnalysis.Workspaces (RS1038). It is packed beside the generator in analyzers/dotnet/cs. Part of #122. Closes #124. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Agent-Driver-Session: 6b413364-2a61-4a69-81a1-b0d64aba5bf5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
This adds DM0026, an analyzer that reports
[SingletonService],[ScopedService]and[TransientService]attributes withoutAs = typeof(...). It is off by default. It also adds a code fix that writes the type the generator infers now.ServiceTypeAnalyzeris in the generator assembly. It callsServiceTypeInferenceand passes the inferred type to the fix in the diagnostic's properties. It passes the type only whentypeofof it, bound at the attribute, reads back as the same type.ServiceTypeCodeFixProvideris in a newDependencyModules.CodeFixesproject. That project references only Roslyn and is packed beside the generator inanalyzers/dotnet/cs.scripts/verify-packages.shchecks that the DLL is in the package and that a consumer build with DM0026 set to error fails with DM0026.website/reference/diagnostics.mddocuments DM0026.Closes #124. Part of #122.
Why
Projects that want every registration written out can turn DM0026 on and apply the fix, including Fix All, without changing what the container gets. The code fix is a separate assembly because a generator assembly must not reference
Microsoft.CodeAnalysis.Workspaces(RS1038).How it was tested
dotnet test tests/DependencyModules.Tests -c Release --filter "ServiceTypeCodeFixTests|CodeFixAssemblyTests|PublicApiTests|ServiceTypeInference"passes 50 tests on net8.0 and net10.0.ApplyingEveryFix_LeavesTheRegistrationsUnchangedbuilds the module, applies all 12 fixes, builds it again, and compares the service type, lifetime, key and implementation type of each registration../scripts/verify-packages.shpasses for net8.0 and net10.0.dotnet csharpier check .is clean.dotnet packlistsanalyzers/dotnet/cs/DependencyModules.CodeFixes.dll, and the nuspec has no dependencies.Review guide
Start with
src/DependencyModules.SourceGenerator/ServiceTypeAnalyzer.cs, mainlyWritableServiceType, which decides when a fix is offered. Then readsrc/DependencyModules.CodeFixes/ServiceTypeCodeFixProvider.cs, mainlyWithAs, which edits only the argument list. The tests are intests/DependencyModules.Tests/GeneratorTests/ServiceTypeCodeFixTests.cs.To check by hand, open a project that uses the packed package in Visual Studio or Rider, set
dotnet_diagnostic.DM0026.severity = warning, and apply the fix and Fix All. No IDE was run for this change.Answers to the item's questions:
CodeFixAssemblyTestsloads it in a context that refuses every other assembly, so an IDE should load it without a FileNotFoundException for a sibling DLL.Asby its written name, the same check the generator uses. Aliased and fully qualified attributes are found and fixed.Notes:
ServiceTypeCodeFixTests.csadds four xUnit1051 warnings per target framework. Those calls do not passTestContext.Current.CancellationToken.ServiceModelUtility.GetTypeDeclarationDefinitionchanged from private to internal so the analyzer can call it.🤖 Generated with Claude Code
Agent Driver: sessions 6b413364 and ba4bdc11, Task 467d4213 (Require explicit As on service attributes), item add-dm0026-and-its-code-fix. Open on your network