Skip to content

Commit d4aab64

Browse files
authored
Update GetFirstRelatedDocumentId to not return documents with the same path within the same project (#78475)
GetFirstRelatedDocumentId had a mismatch in behavior for files within the same project that have the same path (shouldn't really happen, would cause compile errors). Previously, the code would return files from the same proejct when searching the cache, but not when doing the linear walk. Talked with Jason, and he indicated that it was better to have the code not return files from the same project, as callers wouldn't expect that.
1 parent 299955a commit d4aab64

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Workspaces/Core/Portable/Workspace/Solution/SolutionState.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,8 @@ public SolutionState WithAnalyzerReferences(IReadOnlyList<AnalyzerReference> ana
12841284
{
12851285
foreach (var relatedDocumentId in relatedDocumentIds)
12861286
{
1287-
if (relatedDocumentId != documentId)
1287+
// Match the linear search behavior below and do not return documents from the same project.
1288+
if (relatedDocumentId != documentId && relatedDocumentId.ProjectId != documentId.ProjectId)
12881289
return relatedDocumentId;
12891290
}
12901291

src/Workspaces/CoreTest/SolutionTests/SolutionTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,29 @@ public void AddDocument_SourceText()
24352435
Assert.Throws<InvalidOperationException>(() => solution.AddDocument(documentId: DocumentId.CreateNewId(ProjectId.CreateNewId()), "name", sourceText));
24362436
}
24372437

2438+
[Fact]
2439+
public async Task GetFirstRelatedDocumentIdWithDuplicatedDocuments()
2440+
{
2441+
using var workspace = CreateWorkspaceWithProjectAndDocuments();
2442+
var origSolution = workspace.CurrentSolution;
2443+
var project = origSolution.Projects.Single();
2444+
2445+
var origDocumentId = project.DocumentIds.Single();
2446+
var newDocumentId = DocumentId.CreateNewId(project.Id);
2447+
2448+
var document = project.GetRequiredDocument(origDocumentId);
2449+
var sourceText = await document.GetTextAsync();
2450+
2451+
var newSolution = origSolution.AddDocument(newDocumentId, document.Name, sourceText, filePath: document.FilePath!);
2452+
2453+
// Populate the SolutionState cache for this document id
2454+
_ = newSolution.GetRelatedDocumentIds(origDocumentId);
2455+
2456+
// Ensure a GetFirstRelatedDocumentId call with a poulated cache doesn't return newDocumentId
2457+
var relatedDocument = newSolution.GetFirstRelatedDocumentId(origDocumentId, relatedProjectIdHint: null);
2458+
Assert.Null(relatedDocument);
2459+
}
2460+
24382461
[Fact]
24392462
public void AddDocument_SyntaxRoot()
24402463
{

0 commit comments

Comments
 (0)