Skip to content

Commit 6ad21c4

Browse files
authored
Fix logging when item counts change in LoadedProject (#79640)
1 parent f51bf07 commit 6ad21c4

File tree

1 file changed

+15
-16
lines changed
  • src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace

1 file changed

+15
-16
lines changed

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/HostWorkspace/LoadedProject.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void Dispose()
154154
DocumentFileInfoComparer.Instance,
155155
document => _projectSystemProject.AddSourceFile(document.FilePath, folders: document.Folders),
156156
document => _projectSystemProject.RemoveSourceFile(document.FilePath),
157-
"Project {0} now has {1} source file(s).");
157+
"Project {0} now has {1} source file(s). ({2} added, {3} removed.)");
158158

159159
var relativePathResolver = new RelativePathResolver(commandLineArguments.ReferencePaths, commandLineArguments.BaseDirectory);
160160
var metadataReferences = commandLineArguments.MetadataReferences.Select(cr =>
@@ -175,7 +175,7 @@ public void Dispose()
175175
EqualityComparer<CommandLineReference>.Default, // CommandLineReference already implements equality
176176
reference => _projectSystemProject.AddMetadataReference(reference.Reference, reference.Properties),
177177
reference => _projectSystemProject.RemoveMetadataReference(reference.Reference, reference.Properties),
178-
"Project {0} now has {1} reference(s).");
178+
"Project {0} now has {1} reference(s). ({2} added, {3} removed.)");
179179

180180
// Now that we've updated it hold onto the old list of references so we can remove them if there's a later update
181181
_mostRecentMetadataReferences = metadataReferences;
@@ -193,7 +193,7 @@ public void Dispose()
193193
EqualityComparer<CommandLineAnalyzerReference>.Default, // CommandLineAnalyzerReference already implements equality
194194
reference => _projectSystemProject.AddAnalyzerReference(reference.FilePath),
195195
reference => _projectSystemProject.RemoveAnalyzerReference(reference.FilePath),
196-
"Project {0} now has {1} analyzer reference(s).");
196+
"Project {0} now has {1} analyzer reference(s). ({2} added, {3} removed.)");
197197

198198
_mostRecentAnalyzerReferences = analyzerReferences;
199199

@@ -203,23 +203,23 @@ public void Dispose()
203203
DocumentFileInfoComparer.Instance,
204204
document => _projectSystemProject.AddAdditionalFile(document.FilePath, folders: document.Folders),
205205
document => _projectSystemProject.RemoveAdditionalFile(document.FilePath),
206-
"Project {0} now has {1} additional file(s).");
206+
"Project {0} now has {1} additional file(s). ({2} added, {3} removed.)");
207207

208208
UpdateProjectSystemProjectCollection(
209209
newProjectInfo.AnalyzerConfigDocuments,
210210
_mostRecentFileInfo?.AnalyzerConfigDocuments,
211211
DocumentFileInfoComparer.Instance,
212212
document => _projectSystemProject.AddAnalyzerConfigFile(document.FilePath),
213213
document => _projectSystemProject.RemoveAnalyzerConfigFile(document.FilePath),
214-
"Project {0} now has {1} analyzer config file(s).");
214+
"Project {0} now has {1} analyzer config file(s). ({2} added, {3} removed.)");
215215

216216
UpdateProjectSystemProjectCollection(
217217
newProjectInfo.AdditionalDocuments.Where(TreatAsIsDynamicFile),
218218
_mostRecentFileInfo?.AdditionalDocuments.Where(TreatAsIsDynamicFile),
219219
DocumentFileInfoComparer.Instance,
220220
document => _projectSystemProject.AddDynamicSourceFile(document.FilePath, folders: []),
221221
document => _projectSystemProject.RemoveDynamicSourceFile(document.FilePath),
222-
"Project {0} now has {1} dynamic file(s).");
222+
"Project {0} now has {1} dynamic file(s). ({2} added, {3} removed.)");
223223

224224
WatchProjectAssetsFile(newProjectInfo);
225225

@@ -243,33 +243,32 @@ public void Dispose()
243243
var telemetryInfo = new ProjectLoadTelemetryReporter.TelemetryInfo { OutputKind = outputKind, MetadataReferences = metadataReferences };
244244
return (telemetryInfo, needsRestore);
245245

246-
// logMessage should be a string with two placeholders; the first is the project name, the second is the number of items.
246+
// logMessage must have 4 placeholders: project name, number of items, added items count, and removed items count.
247247
void UpdateProjectSystemProjectCollection<T>(IEnumerable<T> loadedCollection, IEnumerable<T>? oldLoadedCollection, IEqualityComparer<T> comparer, Action<T> addItem, Action<T> removeItem, string logMessage)
248248
{
249249
var newItems = new HashSet<T>(loadedCollection, comparer);
250-
var oldItems = new HashSet<T>(comparer);
251-
var oldItemsCount = oldItems.Count;
250+
var oldItems = new HashSet<T>(oldLoadedCollection ?? [], comparer);
252251

253-
if (oldLoadedCollection != null)
254-
{
255-
foreach (var item in oldLoadedCollection)
256-
oldItems.Add(item);
257-
}
252+
var addedCount = 0;
258253

259254
foreach (var newItem in newItems)
260255
{
261256
// If oldItems already has this, we don't need to add it again. We'll remove it, and what is left in oldItems is stuff to remove
262257
if (!oldItems.Remove(newItem))
258+
{
263259
addItem(newItem);
260+
addedCount++;
261+
}
264262
}
265263

264+
var removedCount = oldItems.Count;
266265
foreach (var oldItem in oldItems)
267266
{
268267
removeItem(oldItem);
269268
}
270269

271-
if (newItems.Count != oldItemsCount)
272-
logger.LogTrace(logMessage, projectFullPathWithTargetFramework, newItems.Count);
270+
if (addedCount != 0 || removedCount != 0)
271+
logger.LogTrace(logMessage, projectFullPathWithTargetFramework, newItems.Count, addedCount, removedCount);
273272
}
274273

275274
void WatchProjectAssetsFile(ProjectFileInfo currentProjectInfo)

0 commit comments

Comments
 (0)