Skip to content

Commit

Permalink
Use ordinal ignore case for opening pdb source documents
Browse files Browse the repository at this point in the history
  • Loading branch information
dibarbet committed Sep 9, 2024
1 parent bd5c00e commit dca25b1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IMetadataAsSourceFileProvider>().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<IMetadataAsSourceFileProvider>().OfType<PdbSourceDocumentMetadataAsSourceFileProvider>().Single();
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IMetadataAsSourceFileService>();
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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
private readonly ConcurrentDictionary<string, SourceDocumentInfo> _fileToDocumentInfoMap = [];
private readonly ConcurrentDictionary<string, SourceDocumentInfo> _fileToDocumentInfoMap = new(StringComparer.OrdinalIgnoreCase);

public async Task<MetadataAsSourceFile?> GetGeneratedFileAsync(
MetadataAsSourceWorkspace metadataWorkspace,
Expand Down

0 comments on commit dca25b1

Please sign in to comment.