Skip to content

Commit

Permalink
Add more resiliency when communicating with DTE
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kzu committed Nov 20, 2020
1 parent 3d06fa2 commit 8de0efb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
23 changes: 15 additions & 8 deletions src/SmallSharp.Build/MonitorActiveDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ public override bool Execute()
if (LaunchProfiles == null || UserFile == null || FlagFile == null)
return true;

if (BuildEngine4.GetRegisteredTaskObject(nameof(ActiveDocumentMonitor), RegisteredTaskObjectLifetime.AppDomain) is not ActiveDocumentMonitor monitor)
try
{
if (WindowsInterop.GetServiceProvider() is IServiceProvider services)
if (BuildEngine4.GetRegisteredTaskObject(nameof(ActiveDocumentMonitor), RegisteredTaskObjectLifetime.AppDomain) is not ActiveDocumentMonitor monitor)
{
BuildEngine4.RegisterTaskObject(nameof(ActiveDocumentMonitor),
new ActiveDocumentMonitor(LaunchProfiles, UserFile, FlagFile, services),
RegisteredTaskObjectLifetime.AppDomain, false);
if (WindowsInterop.GetServiceProvider() is IServiceProvider services)
{
BuildEngine4.RegisterTaskObject(nameof(ActiveDocumentMonitor),
new ActiveDocumentMonitor(LaunchProfiles, UserFile, FlagFile, services),
RegisteredTaskObjectLifetime.AppDomain, false);
}
}
else
{
// NOTE: this means we only support ONE project/launchProfiles per IDE.
monitor.Refresh(LaunchProfiles, UserFile, FlagFile);
}
}
else
catch (Exception e)
{
// NOTE: this means we only support ONE project/launchProfiles per IDE.
monitor.Refresh(LaunchProfiles, UserFile, FlagFile);
Log.LogWarning($"Failed to start active document monitoring: {e}");
}

return true;
Expand Down
19 changes: 14 additions & 5 deletions src/SmallSharp.Build/WindowsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void EnsureOpened(string filePath, TimeSpan delay = default)

dte.ExecuteCommand("File.OpenFile", filePath);
}
catch
catch
{
Debug.Fail($"Failed to open {filePath}.");
}
Expand All @@ -43,11 +43,20 @@ public static void EnsureOpened(string filePath, TimeSpan delay = default)
if (delay != default)
Thread.Sleep(delay);

var dte = GetDTE();
if (dte == null)
return null;
try
{

return new OleServiceProvider(dte);
var dte = GetDTE();
if (dte == null)
return null;

return new OleServiceProvider(dte);
}
catch
{
Debug.Fail("Failed to get IDE service provider.");
return null;
}
}

static EnvDTE.DTE? GetDTE()
Expand Down

0 comments on commit 8de0efb

Please sign in to comment.