Skip to content

Commit 2fa97fe

Browse files
committed
Keep tracked nodes in a list
Lazily-enumerated nodes which are passed to TrackNodes and then enumerated a second time can be garbage collected between the two passes. If this occurs, the node tracking will fail. We resolve the issue by ensuring the nodes are held in an eagerly-evaluated list. See dotnet/roslyn#61958
1 parent 01f7674 commit 2fa97fe

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
116116
var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
117117
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
118118

119-
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true).FirstAncestorOrSelf<AttributeListSyntax>());
119+
// 🐉 Need to eagerly evaluate this with ToList() to ensure nodes are not garbage collected between the
120+
// call to TrackNodes and subsequent enumeration.
121+
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true).FirstAncestorOrSelf<AttributeListSyntax>()).ToList();
120122

121123
var newRoot = syntaxRoot.TrackNodes(nodes);
122124

0 commit comments

Comments
 (0)