Skip to content

Commit 8de0efb

Browse files
committed
Add more resiliency when communicating with DTE
Since this is a cross-process operation that may involve attempting to acquire the UI thread while the target VS is actually busy, it may actually fail with a COM exception. In that case, default to not throwing, since that destabilizes the MSBuild process and causes us to not recover the active tracking once the UI thread lock is gone.
1 parent 3d06fa2 commit 8de0efb

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/SmallSharp.Build/MonitorActiveDocument.cs

+15-8
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,26 @@ public override bool Execute()
2121
if (LaunchProfiles == null || UserFile == null || FlagFile == null)
2222
return true;
2323

24-
if (BuildEngine4.GetRegisteredTaskObject(nameof(ActiveDocumentMonitor), RegisteredTaskObjectLifetime.AppDomain) is not ActiveDocumentMonitor monitor)
24+
try
2525
{
26-
if (WindowsInterop.GetServiceProvider() is IServiceProvider services)
26+
if (BuildEngine4.GetRegisteredTaskObject(nameof(ActiveDocumentMonitor), RegisteredTaskObjectLifetime.AppDomain) is not ActiveDocumentMonitor monitor)
2727
{
28-
BuildEngine4.RegisterTaskObject(nameof(ActiveDocumentMonitor),
29-
new ActiveDocumentMonitor(LaunchProfiles, UserFile, FlagFile, services),
30-
RegisteredTaskObjectLifetime.AppDomain, false);
28+
if (WindowsInterop.GetServiceProvider() is IServiceProvider services)
29+
{
30+
BuildEngine4.RegisterTaskObject(nameof(ActiveDocumentMonitor),
31+
new ActiveDocumentMonitor(LaunchProfiles, UserFile, FlagFile, services),
32+
RegisteredTaskObjectLifetime.AppDomain, false);
33+
}
34+
}
35+
else
36+
{
37+
// NOTE: this means we only support ONE project/launchProfiles per IDE.
38+
monitor.Refresh(LaunchProfiles, UserFile, FlagFile);
3139
}
3240
}
33-
else
41+
catch (Exception e)
3442
{
35-
// NOTE: this means we only support ONE project/launchProfiles per IDE.
36-
monitor.Refresh(LaunchProfiles, UserFile, FlagFile);
43+
Log.LogWarning($"Failed to start active document monitoring: {e}");
3744
}
3845

3946
return true;

src/SmallSharp.Build/WindowsInterop.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void EnsureOpened(string filePath, TimeSpan delay = default)
2929

3030
dte.ExecuteCommand("File.OpenFile", filePath);
3131
}
32-
catch
32+
catch
3333
{
3434
Debug.Fail($"Failed to open {filePath}.");
3535
}
@@ -43,11 +43,20 @@ public static void EnsureOpened(string filePath, TimeSpan delay = default)
4343
if (delay != default)
4444
Thread.Sleep(delay);
4545

46-
var dte = GetDTE();
47-
if (dte == null)
48-
return null;
46+
try
47+
{
4948

50-
return new OleServiceProvider(dte);
49+
var dte = GetDTE();
50+
if (dte == null)
51+
return null;
52+
53+
return new OleServiceProvider(dte);
54+
}
55+
catch
56+
{
57+
Debug.Fail("Failed to get IDE service provider.");
58+
return null;
59+
}
5160
}
5261

5362
static EnvDTE.DTE? GetDTE()

0 commit comments

Comments
 (0)