diff --git a/src/EditorFeatures/CSharpTest/PdbSourceDocument/AbstractPdbSourceDocumentTests.cs b/src/EditorFeatures/CSharpTest/PdbSourceDocument/AbstractPdbSourceDocumentTests.cs index c0f9e6a0b2a60..678b8b0fd99a4 100644 --- a/src/EditorFeatures/CSharpTest/PdbSourceDocument/AbstractPdbSourceDocumentTests.cs +++ b/src/EditorFeatures/CSharpTest/PdbSourceDocument/AbstractPdbSourceDocumentTests.cs @@ -155,15 +155,18 @@ protected static async Task GenerateFileAndVerifyAsync( var masWorkspace = service.TryGetWorkspace(); - using var fileStream = File.OpenRead(file.FilePath); - var sourceText = SourceText.From(fileStream); - var text = SourceText.From(File.ReadAllText(file.FilePath)); - var pdbService = (PdbSourceDocumentMetadataAsSourceFileProvider)workspace.ExportProvider.GetExportedValues().Single(s => s is PdbSourceDocumentMetadataAsSourceFileProvider); - var documentInfo = pdbService.GetTestAccessor().Documents[file.FilePath]; - masWorkspace!.OnDocumentAdded(documentInfo.DocumentInfo); - var document = masWorkspace!.CurrentSolution.Projects.First().Documents.First(d => d.FilePath == file.FilePath); + // Add the document to the workspace. We provide an empty static source text as the API requires it to open the document. + // 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. + var result = pdbService.TryAddDocumentToWorkspace((MetadataAsSourceWorkspace)masWorkspace!, file.FilePath, new StaticSourceTextContainer(SourceText.From(string.Empty)), out _); + Assert.True(result); + + // Immediately close the document so that we get the source text provided by the workspace (instead of the empty one we passed). + var info = pdbService.GetTestAccessor().Documents[file.FilePath]; + masWorkspace!.OnDocumentClosed(info.DocumentId, new WorkspaceFileTextLoader(workspace.Services.SolutionServices, file.FilePath, info.Encoding)); + + var document = masWorkspace!.CurrentSolution.GetRequiredDocument(info.DocumentId); // Mapping the project from the generated document should map back to the original project var provider = workspace.ExportProvider.GetExportedValues().OfType().Single(); @@ -339,7 +342,7 @@ protected static string GetPdbPath(string path) return Path.Combine(path, "reference.pdb"); } - private class StaticSourceTextContainer(SourceText sourceText) : SourceTextContainer + protected class StaticSourceTextContainer(SourceText sourceText) : SourceTextContainer { public override SourceText CurrentText => sourceText; diff --git a/src/EditorFeatures/CSharpTest/PdbSourceDocument/PdbSourceDocumentTests.cs b/src/EditorFeatures/CSharpTest/PdbSourceDocument/PdbSourceDocumentTests.cs index 5b6456133778e..0d9bdc78c4013 100644 --- a/src/EditorFeatures/CSharpTest/PdbSourceDocument/PdbSourceDocumentTests.cs +++ b/src/EditorFeatures/CSharpTest/PdbSourceDocument/PdbSourceDocumentTests.cs @@ -951,4 +951,29 @@ await RunTestAsync(async path => await GenerateFileAndVerifyAsync(project, symbol, Location.Embedded, source2.ToString(), expectedSpan, expectNullResult: false); }); } + + [Fact, WorkItem("https://github.com/dotnet/vscode-csharp/issues/7532")] + public async Task OpenFileWithDifferentCase() + { + var source = """ + public class C + { + public int P { get; set; } + } + """; + + await RunTestAsync(async path => + { + var (project, symbol) = await CompileAndFindSymbolAsync(path, Location.Embedded, Location.Embedded, source, c => c.GetMember("C.P")); + + using var workspace = (EditorTestWorkspace)project.Solution.Workspace; + var service = workspace.GetService(); + var file = await service.GetGeneratedFileAsync(project.Solution.Workspace, project, symbol, signaturesOnly: false, options: MetadataAsSourceOptions.Default, cancellationToken: CancellationToken.None); + + var requestPath = file.FilePath.ToUpperInvariant(); + + var result = service.TryAddDocumentToWorkspace(requestPath, new StaticSourceTextContainer(SourceText.From(string.Empty)), out var documentId); + Assert.True(result); + }); + } } diff --git a/src/Features/Core/Portable/PdbSourceDocument/PdbSourceDocumentMetadataAsSourceFileProvider.cs b/src/Features/Core/Portable/PdbSourceDocument/PdbSourceDocumentMetadataAsSourceFileProvider.cs index c0216482b1ca0..8ee8a4734d5a7 100644 --- a/src/Features/Core/Portable/PdbSourceDocument/PdbSourceDocumentMetadataAsSourceFileProvider.cs +++ b/src/Features/Core/Portable/PdbSourceDocument/PdbSourceDocumentMetadataAsSourceFileProvider.cs @@ -68,7 +68,7 @@ internal sealed class PdbSourceDocumentMetadataAsSourceFileProvider( /// generally run concurrently. However, to be safe, we make this a concurrent dictionary to be safe to that /// potentially happening. /// - private readonly ConcurrentDictionary _fileToDocumentInfoMap = []; + private readonly ConcurrentDictionary _fileToDocumentInfoMap = new(StringComparer.OrdinalIgnoreCase); public async Task GetGeneratedFileAsync( MetadataAsSourceWorkspace metadataWorkspace,