MSBuildLocator
MSBuildLocator copied to clipboard
AppDomains
We need to figure out what to do about AppDomains (such as MarkupCompiler):
> PresentationBuildTasks.dll!MS.Internal.MarkupCompiler.OnSourceFileResolve Line 693 C#
PresentationBuildTasks.dll!MS.Internal.MarkupCompiler.Initialize Line 460 C#
PresentationBuildTasks.dll!MS.Internal.MarkupCompiler.Compile Line 265 C#
PresentationBuildTasks.dll!MS.Internal.CompilerWrapper.DoCompilation Line 268 C#
[AppDomain (HtmlGenerator.exe, #1) -> AppDomain (markupCompilationAppDomain, #2)]
PresentationBuildTasks.dll!Microsoft.Build.Tasks.Windows.MarkupCompilePass1.DoMarkupCompilation Line 1274 C#
PresentationBuildTasks.dll!Microsoft.Build.Tasks.Windows.MarkupCompilePass1.Execute Line 194 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute Line 631 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask Line 787 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.InitializeAndExecuteTask Line 660 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteBucket Line 441 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteTask Line 331 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteTask Line 182 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetEntry.ProcessBucket Line 850 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetEntry.ExecuteTarget Line 515 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetBuilder.ProcessTargetStack Line 467 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetBuilder.Microsoft.Build.BackEnd.ITargetBuilderCallback.LegacyCallTarget Line 285 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskHost.InternalBuildProjects Line 671 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.MSBuild.ExecuteTargets Line 680 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.CallTarget.ExecuteInternal Line 134 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask Line 779 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.InitializeAndExecuteTask Line 660 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteBucket Line 441 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteTask Line 331 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteTask Line 182 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetEntry.ProcessBucket Line 850 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetEntry.ExecuteTarget Line 515 C#
Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetBuilder.ProcessTargetStack Line 467 C#
We can use AppDomainManager
to install the resolver for all appdomains in the process:
https://github.com/KirillOsenkov/AppDomainManagerTest/blob/master/README.md
Relying on AppDomainManager
is probably not the best solution since it exposes the fact that we're doing custom AppDomain resolution. IMO, the Locator should push the assembly resolution event handler somewhere into MSBuild. Then, MSBuild should hook it up when creating AppDomains for tasks that require them.
FWIW, at the moment, this is a big problem with using the locator approach generally because any XAML-based project will fail..
Yes, if we control the place where we create appdomains for tasks then it is certainly better.
MSBuild controls it for AppDomainIsolatedTask, but tasks are (sadly) free to create their own AppDomains. Not sure if XAML does that, though.
FWIW, it looks like XAML does create their own AppDomain, so this is a sticky problem.
A workaround here for the XAML case is to set AlwaysCompileMarkupFilesInSeparateDomain=false
https://github.com/KirillOsenkov/SourceBrowser/commit/a6753300bbf2a82260ca66b677482186d7d7aecd
I was able to successfully use the AppDomainManager to intercept AppDomain creation to install the MSBuildLocator in all AppDomains, and it works great with XAML markup compile appdomain.
https://github.com/KirillOsenkov/MSBuildStructuredLog/blob/master/src/StructuredLogger/CustomAppDomainManager.cs
Here's another successful example of installing MSBuildLocator for all appdomains: https://github.com/KirillOsenkov/SourceBrowser/commit/962457bdc0f778461f85ccff8438f9f2cc0a72b8
It would be nice to make that CustomAppDomainManager part of the MSBuild Locator assembly, which could optionally be specified in the app.config file for an application. For now I've added a package reference to SourceBrowser just so I can use that type. I would have just copied the class, but it doesn't work with publicsign.