From cc30d9813bf37d6f9aa215ade19b44e592d0e2f7 Mon Sep 17 00:00:00 2001 From: Brian Robbins Date: Tue, 23 Feb 2021 11:32:50 -0800 Subject: [PATCH 1/6] Revert "Add NuGet assemblies to the list of well-known assemblies." This reverts commit 183645c502cc41de4b63662704b7b339c32f0f12. --- src/Shared/MSBuildLoadContext.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Shared/MSBuildLoadContext.cs b/src/Shared/MSBuildLoadContext.cs index d44b57480d4..f080c2e05a9 100644 --- a/src/Shared/MSBuildLoadContext.cs +++ b/src/Shared/MSBuildLoadContext.cs @@ -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" }; From 3e6e65393e0584c929ba842fc2b48d1d4e788182 Mon Sep 17 00:00:00 2001 From: Brian Robbins Date: Tue, 23 Feb 2021 14:47:30 -0800 Subject: [PATCH 2/6] Don't load assemblies in the plugin context if they are in the MSBuild directory. --- src/Shared/CoreCLRAssemblyLoader.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Shared/CoreCLRAssemblyLoader.cs b/src/Shared/CoreCLRAssemblyLoader.cs index 14cd04a244d..f831501a845 100644 --- a/src/Shared/CoreCLRAssemblyLoader.cs +++ b/src/Shared/CoreCLRAssemblyLoader.cs @@ -23,8 +23,14 @@ internal sealed class CoreClrAssemblyLoader private bool _resolvingHandlerHookedUp = false; + private static string _msbuildDirPath; private static readonly Version _currentAssemblyVersion = new Version(Microsoft.Build.Shared.MSBuildConstants.CurrentAssemblyVersion); + internal CoreClrAssemblyLoader() + { + _msbuildDirPath = Path.GetDirectoryName(BuildEnvironmentHelper.Instance.CurrentMSBuildExePath); + } + public void AddDependencyLocation(string fullPath) { if (fullPath == null) @@ -52,7 +58,11 @@ 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 || string.Equals(assemblyDir, _msbuildDirPath)) { return LoadUsingLegacyDefaultContext(fullPath); } From 364eb66791f18897648e829521bd8883ce6d3e1d Mon Sep 17 00:00:00 2001 From: Brian Robbins Date: Tue, 23 Feb 2021 17:18:16 -0800 Subject: [PATCH 3/6] Normalize the MSBuild dir path. --- src/Shared/CoreCLRAssemblyLoader.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Shared/CoreCLRAssemblyLoader.cs b/src/Shared/CoreCLRAssemblyLoader.cs index f831501a845..a7848f6f4fb 100644 --- a/src/Shared/CoreCLRAssemblyLoader.cs +++ b/src/Shared/CoreCLRAssemblyLoader.cs @@ -28,7 +28,8 @@ internal sealed class CoreClrAssemblyLoader internal CoreClrAssemblyLoader() { - _msbuildDirPath = Path.GetDirectoryName(BuildEnvironmentHelper.Instance.CurrentMSBuildExePath); + _msbuildDirPath = FileUtilities.NormalizePath(BuildEnvironmentHelper.Instance.CurrentMSBuildExePath); + _msbuildDirPath = Path.GetDirectoryName(_msbuildDirPath); } public void AddDependencyLocation(string fullPath) From 0b3d41e7df202239314889071049d2b24fb66084 Mon Sep 17 00:00:00 2001 From: Brian Robbins Date: Tue, 23 Feb 2021 17:18:30 -0800 Subject: [PATCH 4/6] Fix the assembly loader test. --- .../Microsoft.Build.CommandLine.UnitTests.csproj | 6 ++++++ src/MSBuild.UnitTests/XMake_Tests.cs | 9 ++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/MSBuild.UnitTests/Microsoft.Build.CommandLine.UnitTests.csproj b/src/MSBuild.UnitTests/Microsoft.Build.CommandLine.UnitTests.csproj index be9203b581a..d84158d112e 100644 --- a/src/MSBuild.UnitTests/Microsoft.Build.CommandLine.UnitTests.csproj +++ b/src/MSBuild.UnitTests/Microsoft.Build.CommandLine.UnitTests.csproj @@ -74,4 +74,10 @@ + + + + + + diff --git a/src/MSBuild.UnitTests/XMake_Tests.cs b/src/MSBuild.UnitTests/XMake_Tests.cs index 630890e9085..b3c65a114a6 100644 --- a/src/MSBuild.UnitTests/XMake_Tests.cs +++ b/src/MSBuild.UnitTests/XMake_Tests.cs @@ -2237,7 +2237,8 @@ public void EndToEndWarnAsErrors() #if FEATURE_ASSEMBLYLOADCONTEXT /// - /// Ensure that tasks get loaded into their own . + /// Ensure that tasks get loaded into their own + /// if they are in a directory other than the MSBuild directory. /// /// /// When loading a task from a test assembly in a test within that assembly, the assembly is already loaded @@ -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 = $@" @@ -2259,7 +2263,6 @@ public void TasksGetAssemblyLoadContexts() ExecuteMSBuildExeExpectSuccess(projectContents); } - #endif private string CopyMSBuild() From b86d06c9c4ef1f85161cc1a74192aa8beb654c71 Mon Sep 17 00:00:00 2001 From: Brian Robbins Date: Wed, 24 Feb 2021 13:40:22 -0800 Subject: [PATCH 5/6] Harden path checks and detection of MSBuild dll file path. --- src/Shared/CoreCLRAssemblyLoader.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Shared/CoreCLRAssemblyLoader.cs b/src/Shared/CoreCLRAssemblyLoader.cs index a7848f6f4fb..e8f89cf533d 100644 --- a/src/Shared/CoreCLRAssemblyLoader.cs +++ b/src/Shared/CoreCLRAssemblyLoader.cs @@ -26,9 +26,9 @@ internal sealed class CoreClrAssemblyLoader private static string _msbuildDirPath; private static readonly Version _currentAssemblyVersion = new Version(Microsoft.Build.Shared.MSBuildConstants.CurrentAssemblyVersion); - internal CoreClrAssemblyLoader() + static CoreClrAssemblyLoader() { - _msbuildDirPath = FileUtilities.NormalizePath(BuildEnvironmentHelper.Instance.CurrentMSBuildExePath); + _msbuildDirPath = FileUtilities.NormalizePath(typeof(CoreClrAssemblyLoader).Assembly.Location); _msbuildDirPath = Path.GetDirectoryName(_msbuildDirPath); } @@ -63,7 +63,8 @@ public Assembly LoadFromPath(string fullPath) // the load is part of the platform, and load it using the Default ALC. string assemblyDir = Path.GetDirectoryName(fullPath); - if (Traits.Instance.EscapeHatches.UseSingleLoadContext || string.Equals(assemblyDir, _msbuildDirPath)) + if (Traits.Instance.EscapeHatches.UseSingleLoadContext || + FileUtilities.ComparePathsNoThrow(assemblyDir, _msbuildDirPath, string.Empty)) { return LoadUsingLegacyDefaultContext(fullPath); } From 3de22d13725783273e4a8499984ba9c1e1164ec0 Mon Sep 17 00:00:00 2001 From: Brian Robbins Date: Mon, 1 Mar 2021 07:01:07 -0800 Subject: [PATCH 6/6] Mark _msbuildDirPath readonly. --- src/Shared/CoreCLRAssemblyLoader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shared/CoreCLRAssemblyLoader.cs b/src/Shared/CoreCLRAssemblyLoader.cs index e8f89cf533d..385c5c878e8 100644 --- a/src/Shared/CoreCLRAssemblyLoader.cs +++ b/src/Shared/CoreCLRAssemblyLoader.cs @@ -23,7 +23,7 @@ internal sealed class CoreClrAssemblyLoader private bool _resolvingHandlerHookedUp = false; - private static string _msbuildDirPath; + private static readonly string _msbuildDirPath; private static readonly Version _currentAssemblyVersion = new Version(Microsoft.Build.Shared.MSBuildConstants.CurrentAssemblyVersion); static CoreClrAssemblyLoader()