-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74626 from dibarbet/vscode_source_link
Add support in DevKit for source link go to definition
- Loading branch information
Showing
8 changed files
with
234 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/VisualStudio/Core/Def/PdbSourceDocument/AbstractSourceLinkService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.IO; | ||
using System.Reflection.PortableExecutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.PdbSourceDocument; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Microsoft.VisualStudio.Debugger.Contracts.SourceLink; | ||
using Microsoft.VisualStudio.Debugger.Contracts.SymbolLocator; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServices.PdbSourceDocument; | ||
|
||
internal abstract class AbstractSourceLinkService : ISourceLinkService | ||
{ | ||
public async Task<PdbFilePathResult?> GetPdbFilePathAsync(string dllPath, PEReader peReader, bool useDefaultSymbolServers, CancellationToken cancellationToken) | ||
{ | ||
var hasCodeViewEntry = false; | ||
uint timeStamp = 0; | ||
CodeViewDebugDirectoryData codeViewEntry = default; | ||
using var _ = ArrayBuilder<PdbChecksum>.GetInstance(out var checksums); | ||
foreach (var entry in peReader.ReadDebugDirectory()) | ||
{ | ||
if (entry.Type == DebugDirectoryEntryType.PdbChecksum) | ||
{ | ||
var checksum = peReader.ReadPdbChecksumDebugDirectoryData(entry); | ||
checksums.Add(new PdbChecksum(checksum.AlgorithmName, checksum.Checksum)); | ||
} | ||
else if (entry.Type == DebugDirectoryEntryType.CodeView && entry.IsPortableCodeView) | ||
{ | ||
hasCodeViewEntry = true; | ||
timeStamp = entry.Stamp; | ||
codeViewEntry = peReader.ReadCodeViewDebugDirectoryData(entry); | ||
} | ||
} | ||
|
||
if (!hasCodeViewEntry) | ||
return null; | ||
|
||
var pdbInfo = new SymbolLocatorPdbInfo( | ||
Path.GetFileName(codeViewEntry.Path), | ||
codeViewEntry.Guid, | ||
(uint)codeViewEntry.Age, | ||
timeStamp, | ||
checksums.ToImmutable(), | ||
dllPath, | ||
codeViewEntry.Path); | ||
|
||
var flags = useDefaultSymbolServers | ||
? SymbolLocatorSearchFlags.ForceNuGetSymbolServer | SymbolLocatorSearchFlags.ForceMsftSymbolServer | ||
: SymbolLocatorSearchFlags.None; | ||
var result = await LocateSymbolFileAsync(pdbInfo, flags, cancellationToken).ConfigureAwait(false); | ||
if (result is null) | ||
{ | ||
Logger?.Log($"{nameof(LocateSymbolFileAsync)} returned null"); | ||
return null; | ||
} | ||
|
||
if (result.Value.Found && result.Value.SymbolFilePath is not null) | ||
{ | ||
return new PdbFilePathResult(result.Value.SymbolFilePath); | ||
} | ||
else if (Logger is not null) | ||
{ | ||
// We log specific info from the debugger if there is a failure, but the caller will log general failure | ||
// information otherwise | ||
Logger.Log(result.Value.Status); | ||
Logger.Log(result.Value.Log); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public async Task<SourceFilePathResult?> GetSourceFilePathAsync(string url, string relativePath, CancellationToken cancellationToken) | ||
{ | ||
var result = await GetSourceLinkAsync(url, relativePath, cancellationToken).ConfigureAwait(false); | ||
if (result is null) | ||
{ | ||
Logger?.Log($"{nameof(GetSourceLinkAsync)} returned null"); | ||
return null; | ||
} | ||
|
||
if (result.Value.Status == SourceLinkResultStatus.Succeeded && result.Value.Path is not null) | ||
{ | ||
return new SourceFilePathResult(result.Value.Path); | ||
} | ||
else if (Logger is not null && result.Value.Log is not null) | ||
{ | ||
// We log specific info from the debugger if there is a failure, but the caller will log general failure | ||
// information otherwise. | ||
Logger.Log(result.Value.Log); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected abstract Task<SymbolLocatorResult?> LocateSymbolFileAsync(SymbolLocatorPdbInfo pdbInfo, SymbolLocatorSearchFlags flags, CancellationToken cancellationToken); | ||
|
||
protected abstract Task<SourceLinkResult?> GetSourceLinkAsync(string url, string relativePath, CancellationToken cancellationToken); | ||
|
||
protected abstract IPdbSourceDocumentLogger? Logger { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/VisualStudio/DevKit/Impl/SourceLink/VSCodePdbSourceDocumentLogger.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Composition; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.PdbSourceDocument; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.Services.SourceLink; | ||
|
||
[Export(typeof(IPdbSourceDocumentLogger)), Shared] | ||
[method: ImportingConstructor] | ||
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
internal class VSCodePdbSourceDocumentLogger(ILoggerFactory loggerFactory) : IPdbSourceDocumentLogger | ||
{ | ||
private readonly ILogger _logger = loggerFactory.CreateLogger("SourceLink"); | ||
|
||
public void Clear() | ||
{ | ||
// Do nothing, we just leave all the logs up. | ||
return; | ||
} | ||
|
||
public void Log(string message) | ||
{ | ||
_logger.LogTrace(message); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/VisualStudio/DevKit/Impl/SourceLink/VSCodeSourceLinkService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.BrokeredServices; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.PdbSourceDocument; | ||
using Microsoft.ServiceHub.Framework; | ||
using Microsoft.VisualStudio.Debugger.Contracts.SourceLink; | ||
using Microsoft.VisualStudio.Debugger.Contracts.SymbolLocator; | ||
using Microsoft.VisualStudio.LanguageServices.PdbSourceDocument; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.Services.SourceLink; | ||
|
||
[Export(typeof(ISourceLinkService)), Shared] | ||
[method: ImportingConstructor] | ||
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
internal class VSCodeSourceLinkService(IServiceBrokerProvider serviceBrokerProvider, IPdbSourceDocumentLogger logger) : AbstractSourceLinkService | ||
{ | ||
private readonly IServiceBroker _serviceBroker = serviceBrokerProvider.ServiceBroker; | ||
|
||
protected override async Task<SymbolLocatorResult?> LocateSymbolFileAsync(SymbolLocatorPdbInfo pdbInfo, SymbolLocatorSearchFlags flags, CancellationToken cancellationToken) | ||
{ | ||
var proxy = await _serviceBroker.GetProxyAsync<IDebuggerSymbolLocatorService>(BrokeredServiceDescriptors.DebuggerSymbolLocatorService, cancellationToken).ConfigureAwait(false); | ||
using ((IDisposable?)proxy) | ||
{ | ||
if (proxy is null) | ||
{ | ||
return null; | ||
} | ||
|
||
try | ||
{ | ||
var result = await proxy.LocateSymbolFileAsync(pdbInfo, flags, progress: null, cancellationToken).ConfigureAwait(false); | ||
return result; | ||
} | ||
catch (StreamJsonRpc.RemoteMethodNotFoundException) | ||
{ | ||
// Older versions of DevKit use an invalid service descriptor - calling it will throw a RemoteMethodNotFoundException. | ||
// Just return null as there isn't a valid service available. | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
protected override async Task<SourceLinkResult?> GetSourceLinkAsync(string url, string relativePath, CancellationToken cancellationToken) | ||
{ | ||
var proxy = await _serviceBroker.GetProxyAsync<IDebuggerSourceLinkService>(BrokeredServiceDescriptors.DebuggerSourceLinkService, cancellationToken).ConfigureAwait(false); | ||
using ((IDisposable?)proxy) | ||
{ | ||
if (proxy is null) | ||
{ | ||
return null; | ||
} | ||
|
||
try | ||
{ | ||
var result = await proxy.GetSourceLinkAsync(url, relativePath, allowInteractiveLogin: false, cancellationToken).ConfigureAwait(false); | ||
return result; | ||
} | ||
catch (StreamJsonRpc.RemoteMethodNotFoundException) | ||
{ | ||
// Older versions of DevKit use an invalid service descriptor - calling it will throw a RemoteMethodNotFoundException. | ||
// Just return null as there isn't a valid service available. | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
protected override IPdbSourceDocumentLogger? Logger => logger; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters