diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_AddProjectReference.cs b/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_AddProjectReference.cs
index 5d02f99d27a61e7352204b2e2fb20b5cdf0e9482..6412725d43482caf4d0767e81d242327a4667c3e 100644
--- a/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_AddProjectReference.cs
+++ b/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_AddProjectReference.cs
@@ -23,10 +23,7 @@ internal ProjectDependencyGraph WithAdditionalProjectReferences(ProjectId projec
var newReferencesMap = ComputeNewReferencesMapForAdditionalProjectReferences(_referencesMap, projectId, referencedProjectIds);
- var newReverseReferencesMap =
- _lazyReverseReferencesMap != null
- ? ComputeNewReverseReferencesMapForAdditionalProjectReferences(_lazyReverseReferencesMap, projectId, referencedProjectIds)
- : null;
+ var newReverseReferencesMap = ComputeNewReverseReferencesMapForAdditionalProjectReferences(_lazyReverseReferencesMap, projectId, referencedProjectIds);
var newTransitiveReferencesMap = ComputeNewTransitiveReferencesMapForAdditionalProjectReferences(_transitiveReferencesMap, projectId, referencedProjectIds);
@@ -64,13 +61,15 @@ internal ProjectDependencyGraph WithAdditionalProjectReferences(ProjectId projec
///
/// Computes a new for the addition of additional project references.
- /// Must be called on a non-null map.
///
- private static ImmutableDictionary> ComputeNewReverseReferencesMapForAdditionalProjectReferences(
- ImmutableDictionary> existingReverseReferencesMap,
+ private static ImmutableDictionary>? ComputeNewReverseReferencesMapForAdditionalProjectReferences(
+ ImmutableDictionary>? existingReverseReferencesMap,
ProjectId projectId,
IReadOnlyList referencedProjectIds)
{
+ if (existingReverseReferencesMap is null)
+ return null;
+
var builder = existingReverseReferencesMap.ToBuilder();
foreach (var referencedProject in referencedProjectIds)
diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_RemoveProject.cs b/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_RemoveProject.cs
index b5d9fabdfbcbab1daaaacf9dc47a64cb702690a4..4e4f9b8d6d6fac4585c1fcab3f043d8323505aad 100644
--- a/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_RemoveProject.cs
+++ b/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_RemoveProject.cs
@@ -23,12 +23,10 @@ internal ProjectDependencyGraph WithProjectRemoved(ProjectId projectId)
// The direct reverse references map is updated by removing the key for the project getting removed, and
// also updating any direct references to the removed project.
- var reverseReferencesMap = _lazyReverseReferencesMap is object
- ? ComputeNewReverseReferencesMapForRemovedProject(
- existingForwardReferencesMap: _referencesMap,
- existingReverseReferencesMap: _lazyReverseReferencesMap,
- projectId)
- : null;
+ var reverseReferencesMap = ComputeNewReverseReferencesMapForRemovedProject(
+ existingForwardReferencesMap: _referencesMap,
+ existingReverseReferencesMap: _lazyReverseReferencesMap,
+ projectId);
var transitiveReferencesMap = ComputeNewTransitiveReferencesMapForRemovedProject(_transitiveReferencesMap, projectId);
var reverseTransitiveReferencesMap = ComputeNewReverseTransitiveReferencesMapForRemovedProject(_reverseTransitiveReferencesMap, projectId);
return new ProjectDependencyGraph(
@@ -84,11 +82,17 @@ internal ProjectDependencyGraph WithProjectRemoved(ProjectId projectId)
///
/// Computes a new for the removal of a project.
///
- private static ImmutableDictionary> ComputeNewReverseReferencesMapForRemovedProject(
+ private static ImmutableDictionary>? ComputeNewReverseReferencesMapForRemovedProject(
ImmutableDictionary> existingForwardReferencesMap,
- ImmutableDictionary> existingReverseReferencesMap,
+ ImmutableDictionary>? existingReverseReferencesMap,
ProjectId projectId)
{
+ if (existingReverseReferencesMap is null)
+ {
+ // The map was never calculated for the previous graph, so there is nothing to update.
+ return null;
+ }
+
var builder = existingReverseReferencesMap.ToBuilder();
// Iterate over each project referenced by 'projectId', which is now being removed. Update the reverse
diff --git a/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_RemoveProjectReference.cs b/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_RemoveProjectReference.cs
index b1e1011782e238eb82401201675c1863362f0f72..a9e2a0034548e35eae0fe898b9987e91464a947a 100644
--- a/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_RemoveProjectReference.cs
+++ b/src/Workspaces/Core/Portable/Workspace/Solution/ProjectDependencyGraph_RemoveProjectReference.cs
@@ -20,9 +20,7 @@ internal ProjectDependencyGraph WithProjectReferenceRemoved(ProjectId projectId,
// Incrementally update the graph
var referencesMap = _referencesMap.SetItem(projectId, _referencesMap[projectId].Remove(referencedProjectId));
- var reverseReferencesMap = _lazyReverseReferencesMap is object
- ? ComputeNewReverseReferencesMapForRemovedProjectReference(_lazyReverseReferencesMap, projectId, referencedProjectId)
- : null;
+ var reverseReferencesMap = ComputeNewReverseReferencesMapForRemovedProjectReference(_lazyReverseReferencesMap, projectId, referencedProjectId);
var transitiveReferencesMap = ComputeNewTransitiveReferencesMapForRemovedProjectReference(_transitiveReferencesMap, projectId, referencedProjectId);
var reverseTransitiveReferencesMap = ComputeNewReverseTransitiveReferencesMapForRemovedProjectReference(_reverseTransitiveReferencesMap, projectId, referencedProjectId);
@@ -36,11 +34,14 @@ internal ProjectDependencyGraph WithProjectReferenceRemoved(ProjectId projectId,
dependencySets: default);
}
- private ImmutableDictionary> ComputeNewReverseReferencesMapForRemovedProjectReference(
- ImmutableDictionary> existingReverseReferencesMap,
+ private ImmutableDictionary>? ComputeNewReverseReferencesMapForRemovedProjectReference(
+ ImmutableDictionary>? existingReverseReferencesMap,
ProjectId projectId,
ProjectId referencedProjectId)
{
+ if (existingReverseReferencesMap is null)
+ return null;
+
if (existingReverseReferencesMap.TryGetValue(referencedProjectId, out var referencingProjects))
{
return existingReverseReferencesMap.SetItem(referencedProjectId, referencingProjects.Remove(projectId));