Skip to content

Commit 71bf9fd

Browse files
authored
Merge pull request #75033 from dibarbet/cherry_pick_sourcelink
Cherry-pick sourcelink casing fix
2 parents 854d84d + 407dae3 commit 71bf9fd

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/EditorFeatures/CSharpTest/PdbSourceDocument/AbstractPdbSourceDocumentTests.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,18 @@ protected static async Task GenerateFileAndVerifyAsync(
155155

156156
var masWorkspace = service.TryGetWorkspace();
157157

158-
using var fileStream = File.OpenRead(file.FilePath);
159-
var sourceText = SourceText.From(fileStream);
160-
var text = SourceText.From(File.ReadAllText(file.FilePath));
161-
162158
var pdbService = (PdbSourceDocumentMetadataAsSourceFileProvider)workspace.ExportProvider.GetExportedValues<IMetadataAsSourceFileProvider>().Single(s => s is PdbSourceDocumentMetadataAsSourceFileProvider);
163159

164-
var documentInfo = pdbService.GetTestAccessor().Documents[file.FilePath];
165-
masWorkspace!.OnDocumentAdded(documentInfo.DocumentInfo);
166-
var document = masWorkspace!.CurrentSolution.Projects.First().Documents.First(d => d.FilePath == file.FilePath);
160+
// Add the document to the workspace. We provide an empty static source text as the API requires it to open the document.
161+
// We're not really trying to verify that the source text the editor hands to us is the right encoding - just that the document we added has the right encoding.
162+
var result = pdbService.TryAddDocumentToWorkspace((MetadataAsSourceWorkspace)masWorkspace!, file.FilePath, new StaticSourceTextContainer(SourceText.From(string.Empty)), out _);
163+
Assert.True(result);
164+
165+
// Immediately close the document so that we get the source text provided by the workspace (instead of the empty one we passed).
166+
var info = pdbService.GetTestAccessor().Documents[file.FilePath];
167+
masWorkspace!.OnDocumentClosed(info.DocumentId, new WorkspaceFileTextLoader(workspace.Services.SolutionServices, file.FilePath, info.Encoding));
168+
169+
var document = masWorkspace!.CurrentSolution.GetRequiredDocument(info.DocumentId);
167170

168171
// Mapping the project from the generated document should map back to the original project
169172
var provider = workspace.ExportProvider.GetExportedValues<IMetadataAsSourceFileProvider>().OfType<PdbSourceDocumentMetadataAsSourceFileProvider>().Single();
@@ -339,7 +342,7 @@ protected static string GetPdbPath(string path)
339342
return Path.Combine(path, "reference.pdb");
340343
}
341344

342-
private class StaticSourceTextContainer(SourceText sourceText) : SourceTextContainer
345+
protected class StaticSourceTextContainer(SourceText sourceText) : SourceTextContainer
343346
{
344347
public override SourceText CurrentText => sourceText;
345348

src/EditorFeatures/CSharpTest/PdbSourceDocument/PdbSourceDocumentTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,4 +951,29 @@ await RunTestAsync(async path =>
951951
await GenerateFileAndVerifyAsync(project, symbol, Location.Embedded, source2.ToString(), expectedSpan, expectNullResult: false);
952952
});
953953
}
954+
955+
[Fact, WorkItem("https://github.com/dotnet/vscode-csharp/issues/7532")]
956+
public async Task OpenFileWithDifferentCase()
957+
{
958+
var source = """
959+
public class C
960+
{
961+
public int P { get; set; }
962+
}
963+
""";
964+
965+
await RunTestAsync(async path =>
966+
{
967+
var (project, symbol) = await CompileAndFindSymbolAsync(path, Location.Embedded, Location.Embedded, source, c => c.GetMember("C.P"));
968+
969+
using var workspace = (EditorTestWorkspace)project.Solution.Workspace;
970+
var service = workspace.GetService<IMetadataAsSourceFileService>();
971+
var file = await service.GetGeneratedFileAsync(project.Solution.Workspace, project, symbol, signaturesOnly: false, options: MetadataAsSourceOptions.Default, cancellationToken: CancellationToken.None);
972+
973+
var requestPath = file.FilePath.ToUpperInvariant();
974+
975+
var result = service.TryAddDocumentToWorkspace(requestPath, new StaticSourceTextContainer(SourceText.From(string.Empty)), out var documentId);
976+
Assert.True(result);
977+
});
978+
}
954979
}

src/Features/Core/Portable/PdbSourceDocument/PdbSourceDocumentMetadataAsSourceFileProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal sealed class PdbSourceDocumentMetadataAsSourceFileProvider(
6868
/// generally run concurrently. However, to be safe, we make this a concurrent dictionary to be safe to that
6969
/// potentially happening.
7070
/// </summary>
71-
private readonly ConcurrentDictionary<string, SourceDocumentInfo> _fileToDocumentInfoMap = [];
71+
private readonly ConcurrentDictionary<string, SourceDocumentInfo> _fileToDocumentInfoMap = new(StringComparer.OrdinalIgnoreCase);
7272

7373
public async Task<MetadataAsSourceFile?> GetGeneratedFileAsync(
7474
MetadataAsSourceWorkspace metadataWorkspace,

0 commit comments

Comments
 (0)