From c7000f23c4704e07963fee86132a75f6a66fffa0 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Wed, 13 Jun 2018 16:47:10 -0700 Subject: [PATCH] Replace a hard crash with an NFW Fixes Watson bug VSO [400829](https://devdiv.visualstudio.com/DevDiv/NET%20Developer%20Experience%20Productivity/_workitems/edit/400829) --- .../TableDataSource/TableItem.cs | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/VisualStudio/Core/Def/Implementation/TableDataSource/TableItem.cs b/src/VisualStudio/Core/Def/Implementation/TableDataSource/TableItem.cs index 4c9b61bec88..01014131965 100644 --- a/src/VisualStudio/Core/Def/Implementation/TableDataSource/TableItem.cs +++ b/src/VisualStudio/Core/Def/Implementation/TableDataSource/TableItem.cs @@ -5,6 +5,7 @@ using System.Collections.Immutable; using System.Linq; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.ErrorReporting; using Roslyn.Utilities; namespace Microsoft.VisualStudio.LanguageServices.Implementation.TableDataSource @@ -29,22 +30,27 @@ public TableItem(T item, Func keyGenerator) public TableItem(IEnumerable> items) { -#if DEBUG - // If code reached here, - // There must be at least 1 item in the list - Contract.ThrowIfFalse(items.Count() > 0); + var itemCount = items.Count(); + if (itemCount == 0) + { + // There must be at least 1 item in the list + FatalError.ReportWithoutCrash(new ArgumentException("Contains no items", nameof(items))); + } - // There must be document id - Contract.ThrowIfTrue(items.Any(i => i.PrimaryDocumentId == null)); -#endif + var filteredItems = items.Where(i => i.PrimaryDocumentId != null); + if (filteredItems.Count() != itemCount) + { + // There must be document id for provided items. + FatalError.ReportWithoutCrash(new ArgumentException("Contains an item with null PrimaryDocumentId", nameof(items))); + } var first = true; var collectionHash = 1; var count = 0; // Make things to be deterministic. - var ordereditems = items.OrderBy(i => i.PrimaryDocumentId.Id); - foreach (var item in ordereditems) + var orderedItems = filteredItems.OrderBy(i => i.PrimaryDocumentId.Id); + foreach (var item in orderedItems) { count++; @@ -68,7 +74,7 @@ public TableItem(IEnumerable> items) } // order of item is important. make sure we maintain it. - _cache = SharedInfoCache.GetOrAdd(collectionHash, ordereditems, c => new SharedInfoCache(c.Select(i => i.PrimaryDocumentId).ToImmutableArray())); + _cache = SharedInfoCache.GetOrAdd(collectionHash, orderedItems, c => new SharedInfoCache(c.Select(i => i.PrimaryDocumentId).ToImmutableArray())); } public DocumentId PrimaryDocumentId -- GitLab