diff --git a/src/Features/Core/Portable/Diagnostics/EngineV2/DiagnosticIncrementalAnalyzer_IncrementalAnalyzer.cs b/src/Features/Core/Portable/Diagnostics/EngineV2/DiagnosticIncrementalAnalyzer_IncrementalAnalyzer.cs index 83ed92840c7e8..9096e1b4a7b03 100644 --- a/src/Features/Core/Portable/Diagnostics/EngineV2/DiagnosticIncrementalAnalyzer_IncrementalAnalyzer.cs +++ b/src/Features/Core/Portable/Diagnostics/EngineV2/DiagnosticIncrementalAnalyzer_IncrementalAnalyzer.cs @@ -361,7 +361,17 @@ private void RaiseProjectDiagnosticsCreated(Project project, StateSet stateSet, foreach (var documentId in newAnalysisResult.DocumentIds) { var document = project.GetDocument(documentId); - Contract.ThrowIfNull(document); + if (document == null) + { + // it can happen with build synchronization since, in build case, + // we don't have actual snapshot (we have no idea what sources out of proc build has picked up) + // so we might be out of sync. + // example of such cases will be changing anything about solution while building is going on. + // it can be user explict actions such as unloading project, deleting a file, but also it can be + // something project system or roslyn workspace does such as populating workspace right after + // solution is loaded. + continue; + } RaiseDocumentDiagnosticsIfNeeded(document, stateSet, AnalysisKind.NonLocal, oldAnalysisResult, newAnalysisResult, raiseEvents); diff --git a/src/Features/Core/Portable/SolutionCrawler/SolutionCrawlerRegistrationService.cs b/src/Features/Core/Portable/SolutionCrawler/SolutionCrawlerRegistrationService.cs index 3e668c0a7d1a5..d3d89522bc543 100644 --- a/src/Features/Core/Portable/SolutionCrawler/SolutionCrawlerRegistrationService.cs +++ b/src/Features/Core/Portable/SolutionCrawler/SolutionCrawlerRegistrationService.cs @@ -91,7 +91,10 @@ public void Reanalyze(Workspace workspace, IIncrementalAnalyzer analyzer, IEnume var coordinator = default(WorkCoordinator); if (!_documentWorkCoordinatorMap.TryGetValue(workspace, out coordinator)) { - throw new ArgumentException("workspace"); + // this can happen if solution crawler is already unregistered from workspace. + // one of those example will be VS shutting down so roslyn package is disposed but there is a pending + // async operation. + return; } // no specific projects or documents provided