Skip to content

Commit

Permalink
Fix Loading of Plugin Assemblies (#6189)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianrob authored Mar 3, 2021
1 parent 202f872 commit a71a130
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<Target Name="CreateTaskDir" AfterTargets="Build" Condition="'$(TargetFrameworkIdentifier)' != ''">
<ItemGroup>
<OutputAssemblyList Include="$(TargetDir)Microsoft.Build.CommandLine.UnitTests.dll" />
</ItemGroup>
<Copy SourceFiles="@(OutputAssemblyList)" DestinationFolder="$(TargetDir)Task" />
</Target>
</Project>
9 changes: 6 additions & 3 deletions src/MSBuild.UnitTests/XMake_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,8 @@ public void EndToEndWarnAsErrors()

#if FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Ensure that tasks get loaded into their own <see cref="System.Runtime.Loader.AssemblyLoadContext"/>.
/// Ensure that tasks get loaded into their own <see cref="System.Runtime.Loader.AssemblyLoadContext"/>
/// if they are in a directory other than the MSBuild directory.
/// </summary>
/// <remarks>
/// When loading a task from a test assembly in a test within that assembly, the assembly is already loaded
Expand All @@ -2247,7 +2248,10 @@ public void EndToEndWarnAsErrors()
[Fact]
public void TasksGetAssemblyLoadContexts()
{
string customTaskPath = Assembly.GetExecutingAssembly().Location;
string customTaskPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"Task",
Path.GetFileName(Assembly.GetExecutingAssembly().Location));

string projectContents = $@"<Project ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>
<UsingTask TaskName=`ValidateAssemblyLoadContext` AssemblyFile=`{customTaskPath}` />
Expand All @@ -2259,7 +2263,6 @@ public void TasksGetAssemblyLoadContexts()

ExecuteMSBuildExeExpectSuccess(projectContents);
}

#endif

private string CopyMSBuild()
Expand Down
14 changes: 13 additions & 1 deletion src/Shared/CoreCLRAssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ internal sealed class CoreClrAssemblyLoader

private bool _resolvingHandlerHookedUp = false;

private static readonly string _msbuildDirPath;
private static readonly Version _currentAssemblyVersion = new Version(Microsoft.Build.Shared.MSBuildConstants.CurrentAssemblyVersion);

static CoreClrAssemblyLoader()
{
_msbuildDirPath = FileUtilities.NormalizePath(typeof(CoreClrAssemblyLoader).Assembly.Location);
_msbuildDirPath = Path.GetDirectoryName(_msbuildDirPath);
}

public void AddDependencyLocation(string fullPath)
{
if (fullPath == null)
Expand Down Expand Up @@ -52,7 +59,12 @@ public Assembly LoadFromPath(string fullPath)
// folders in a NuGet package).
fullPath = FileUtilities.NormalizePath(fullPath);

if (Traits.Instance.EscapeHatches.UseSingleLoadContext)
// If the requested load comes from the same directory as MSBuild, assume that
// the load is part of the platform, and load it using the Default ALC.
string assemblyDir = Path.GetDirectoryName(fullPath);

if (Traits.Instance.EscapeHatches.UseSingleLoadContext ||
FileUtilities.ComparePathsNoThrow(assemblyDir, _msbuildDirPath, string.Empty))
{
return LoadUsingLegacyDefaultContext(fullPath);
}
Expand Down

0 comments on commit a71a130

Please sign in to comment.