Skip to content

Commit 48d5ef6

Browse files
committed
Add diagnostic messages around DiaSession failures
1 parent 32b7c99 commit 48d5ef6

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

src/xunit.runner.visualstudio/Utility/DiaSessionWrapper.cs

+42-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
33
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Navigation;
4+
using Xunit.Abstractions;
5+
using Xunit.Internal;
46

57
namespace Xunit.Runner.VisualStudio.Utility;
68

@@ -13,38 +15,65 @@ class DiaSessionWrapper : IDisposable
1315
readonly AppDomainManager? appDomainManager;
1416
#endif
1517
readonly DiaSessionWrapperHelper? helper;
16-
readonly DiaSession session;
18+
readonly DiaSession? session;
19+
readonly DiagnosticMessageSink diagnosticMessageSink;
1720

18-
public DiaSessionWrapper(string assemblyFileName)
21+
public DiaSessionWrapper(
22+
string assemblyFileName,
23+
DiagnosticMessageSink diagnosticMessageSink)
1924
{
20-
session = new DiaSession(assemblyFileName);
25+
this.diagnosticMessageSink = Guard.ArgumentNotNull(diagnosticMessageSink);
2126

22-
#if NETFRAMEWORK
23-
var adapterFileName = typeof(DiaSessionWrapperHelper).Assembly.GetLocalCodeBase();
24-
if (adapterFileName is not null)
27+
try
28+
{
29+
session = new DiaSession(assemblyFileName);
30+
}
31+
catch (Exception ex)
2532
{
26-
appDomainManager = new AppDomainManager(assemblyFileName);
27-
helper = appDomainManager.CreateObject<DiaSessionWrapperHelper>(typeof(DiaSessionWrapperHelper).Assembly.GetName(), typeof(DiaSessionWrapperHelper).FullName!, adapterFileName);
33+
diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Exception creating DiaSession: {ex}"));
2834
}
35+
36+
try
37+
{
38+
#if NETFRAMEWORK
39+
var adapterFileName = typeof(DiaSessionWrapperHelper).Assembly.GetLocalCodeBase();
40+
if (adapterFileName is not null)
41+
{
42+
appDomainManager = new AppDomainManager(assemblyFileName);
43+
helper = appDomainManager.CreateObject<DiaSessionWrapperHelper>(typeof(DiaSessionWrapperHelper).Assembly.GetName(), typeof(DiaSessionWrapperHelper).FullName!, adapterFileName);
44+
}
2945
#else
30-
helper = new DiaSessionWrapperHelper(assemblyFileName);
46+
helper = new DiaSessionWrapperHelper(assemblyFileName);
3147
#endif
48+
}
49+
catch (Exception ex)
50+
{
51+
diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Exception creating DiaSessionWrapperHelper: {ex}"));
52+
}
3253
}
3354

3455
public INavigationData? GetNavigationData(
3556
string typeName,
3657
string methodName)
3758
{
38-
if (helper is null)
59+
if (session is null || helper is null)
3960
return null;
4061

41-
helper.Normalize(ref typeName, ref methodName);
42-
return session.GetNavigationDataForMethod(typeName, methodName);
62+
try
63+
{
64+
helper.Normalize(ref typeName, ref methodName);
65+
return session.GetNavigationDataForMethod(typeName, methodName);
66+
}
67+
catch (Exception ex)
68+
{
69+
diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Exception getting source mapping for {typeName}.{methodName}: {ex}"));
70+
return null;
71+
}
4372
}
4473

4574
public void Dispose()
4675
{
47-
session.Dispose();
76+
session?.Dispose();
4877
#if NETFRAMEWORK
4978
appDomainManager?.Dispose();
5079
#endif

src/xunit.runner.visualstudio/Utility/VisualStudioSourceInformationProvider.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ public class VisualStudioSourceInformationProvider : LongLivedMarshalByRefObject
1818
/// Initializes a new instance of the <see cref="VisualStudioSourceInformationProvider" /> class.
1919
/// </summary>
2020
/// <param name="assemblyFileName">The assembly file name.</param>
21-
public VisualStudioSourceInformationProvider(string assemblyFileName)
21+
/// <param name="diagnosticMessageSink">The message sink to send internal diagnostic messages to.</param>
22+
public VisualStudioSourceInformationProvider(
23+
string assemblyFileName,
24+
DiagnosticMessageSink diagnosticMessageSink)
2225
{
23-
session = new DiaSessionWrapper(assemblyFileName);
26+
session = new DiaSessionWrapper(assemblyFileName, diagnosticMessageSink);
2427
}
2528

2629
/// <inheritdoc/>

src/xunit.runner.visualstudio/VsTestRunner.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void DiscoverTests<TVisitor>(
240240
var diagnosticSink = DiagnosticMessageSink.ForDiagnostics(logger, fileName, assembly.Configuration.DiagnosticMessagesOrDefault);
241241
var appDomain = assembly.Configuration.AppDomain ?? AppDomainDefaultBehavior;
242242

243-
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assembly.AssemblyFilename);
243+
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assembly.AssemblyFilename, diagnosticSink);
244244
using var controller = new XunitFrontController(appDomain, assembly.AssemblyFilename, shadowCopy: shadowCopy, sourceInformationProvider: sourceInformationProvider, diagnosticMessageSink: MessageSinkAdapter.Wrap(diagnosticSink));
245245
if (!DiscoverTestsInSource(controller, logger, testPlatformContext, runSettings, visitorFactory, visitComplete, assembly))
246246
break;
@@ -428,7 +428,7 @@ void RunTestsInAssembly(
428428

429429
var diagnosticSink = DiagnosticMessageSink.ForDiagnostics(logger, assemblyDisplayName, runInfo.Assembly.Configuration.DiagnosticMessagesOrDefault);
430430
var diagnosticMessageSink = MessageSinkAdapter.Wrap(diagnosticSink);
431-
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName);
431+
using var sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName, diagnosticSink);
432432
using var controller = new XunitFrontController(appDomain, assemblyFileName, shadowCopy: shadowCopy, sourceInformationProvider: sourceInformationProvider, diagnosticMessageSink: diagnosticMessageSink);
433433
var testCasesMap = new Dictionary<string, TestCase>();
434434
var testCases = new List<ITestCase>();

0 commit comments

Comments
 (0)