Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Loading of Plugin Assemblies #6189

Merged
merged 6 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ComparePathsNoThrow handles the normalization part of this for you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Forgind. I'd like to leave this here. The reason is that it happens only once per process, and has the ability to simplify the checks that must happen per assembly, instead of just once per process. If we don't do this here, and the value would have changed, then when loading an assembly, we'd have to do a string comparison which would fail, and then we'd have to normalize both inputs, and then try again.

If on the other hand, the initial normalize call here changed the input, it could possibly avoid the extra normalizations per assembly load.

_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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why is the default ALC called legacy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. This is beyond my time in this code base.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The not-legacy ALC was a recent addition. rainersigwald tried to make it on by default, but that broke people, so he switched it to off-by-default but encouraged. It might have been turned on by default in the SDK; not sure.

}
Expand Down
12 changes: 0 additions & 12 deletions src/Shared/MSBuildLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,8 @@ internal class MSBuildLoadContext : AssemblyLoadContext
"MSBuild",
"Microsoft.Build",
"Microsoft.Build.Framework",
"Microsoft.Build.NuGetSdkResolver",
"Microsoft.Build.Tasks.Core",
"Microsoft.Build.Utilities.Core",
"NuGet.Build.Tasks",
"NuGet.Common",
"NuGet.Configuration",
"NuGet.Credentials",
"NuGet.DependencyResolver.Core",
"NuGet.Frameworks",
"NuGet.LibraryModel",
"NuGet.Packaging",
"NuGet.Protocol",
"NuGet.ProjectModel",
"NuGet.Versioning",
}.ToImmutableHashSet();

internal static readonly string[] Extensions = new[] { "ni.dll", "ni.exe", "dll", "exe" };
Expand Down