dotnet / dotnet/roslyn

Rename crashes with source generated partial method signatures

Open
#77,986 0 comments 0 reactions 1 assignee Claimed by @jasonmalinowski View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/Rename-crashes-with-source-generated-par/10881364)._

---
[severity:It bothers me. A fix would be nice]
I have a logging class that uses source generators to help with logging Api calls: `internal static partial class ApiLogger`. This class has a number of methods that are defined for example like this:
```C#
[LoggerMessage(
Message = "Api {httpMethod} {methodName}. User: {principal}. Parameters: {parametersJson}.",
Level = LogLevel.Information
)]
internal static partial void LogApiCallWithParameters(this ILogger logger,
HttpMethod httpMethod,
string methodName,
PrincipalLogFormatter principal,
string parametersJson
);
```

If using rename command on one of the parameters, VS will give an error "Feature 'Rename' is currently unavailable due to an internal error." and a stack trace:
```
StreamJsonRpc.RemoteInvocationException: The given key 'SourceFile(C:\my-project\obj\Debug\net8.0\Microsoft.Gen.Logging\Microsoft.Gen.Logging.LoggingGenerator\Logging.g.cs[5972..5983))' was not present in the dictionary.
at StreamJsonRpc.JsonRpc.<InvokeCoreAsync>d__156`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.ValueTask`1.get_Result()
at Microsoft.CodeAnalysis.Remote.BrokeredServiceConnection`1.<TryInvokeAsync>d__18`1.MoveNext()
RPC server exception:
System.Collections.Generic.KeyNotFoundException: The given key 'SourceFile(C:\my-project\obj\Debug\net8.0\Microsoft.Gen.Logging\Microsoft.Gen.Logging.LoggingGenerator\Logging.g.cs[5972..5983))' was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at System.Collections.Immutable.ImmutableArray`1.Builder.AddRange(IEnumerable`1 items)
at Microsoft.CodeAnalysis.CSharp.Rename.CSharpRenameConflictLanguageService.ComputeDeclarationConflictsAsync(String replacementText, ISymbol renamedSymbol, ISymbol renameSymbol, IEnumerable`1 referencedSymbols, Solution baseSolution, Solution newSolution, IDictionary`2 reverseMappedLocations, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver.AddDeclarationConflictsAsync(ISymbol renamedSymbol, ISymbol renameSymbol, IEnumerable`1 referencedSymbols, MutableConflictResolution conflictResolution, IDictionary`2 reverseMappedLocations, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver.Session.IdentifyConflictsAsync(HashSet`1 documentIdsForConflictResolution, IEnumerable`1 allDocumentIdsInProject, ProjectId projectId, MutableConflictResolution conflictResolution, ImmutableHashSet`1 conflictLocations)
at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver.Session.ResolveConflictsAsync()
at Microsoft.CodeAnalysis.Rename.ConflictEngine.ConflictResolver.ResolveSymbolicLocationConflictsInCurrentProcessAsync(SymbolicRenameLocations renameLocations, String replacementText, ImmutableArray`1 nonConflictSymbolKeys, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.RemoteRenamerService.<>c__DisplayClass4_0.<<ResolveConflictsAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.<>c__DisplayClass7_0`1.<<RunWithSolutionAsync>g__ProcessSolutionAsync|1>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.RunWithSolutionAsync[T](AssetProvider assetProvider, Checksum solutionChecksum, Boolean updatePrimaryBranch, Func`2 implementation, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.RunWithSolutionAsync[T](AssetProvider assetProvider, Checksum solutionChecksum, Boolean updatePrimaryBranch, Func`2 implementation, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.RunWithSolutionAsync[T](Checksum solutionChecksum, Func`2 implementation, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.RunServiceImplAsync[T](Func`2 implementation, CancellationToken cancellationToken)
```

I'd guess it's a common problem when using automated features with source generation.

---
### Original Comments

#### Feedback Bot on 31/3/2025, 01:48 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.