diff --git a/src/Shared/MSBuildLoadContext.cs b/src/Shared/MSBuildLoadContext.cs index 3e797529682..fa2bcce58a2 100644 --- a/src/Shared/MSBuildLoadContext.cs +++ b/src/Shared/MSBuildLoadContext.cs @@ -94,13 +94,15 @@ public MSBuildLoadContext(string assemblyPath) // If the Assembly is provided via a file path, the following rules are used to load the assembly: // - the assembly from the user specified path is loaded, if it exists, into the custom ALC, or // - if the simple name of the assembly exists in the same folder as msbuild.exe, then that assembly gets loaded - // into the default ALC (so it's shared with other uses). + // into the default ALC (so it's shared with other uses), or into the custom ALC if we are not running on + // MSBuild.exe (because it might interfere with the app's own dependencies). var assemblyNameInExecutableDirectory = Path.Combine(BuildEnvironmentHelper.Instance.CurrentMSBuildToolsDirectory, $"{assemblyName.Name}.dll"); if (FileSystems.Default.FileExists(assemblyNameInExecutableDirectory)) { - return AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyNameInExecutableDirectory); + AssemblyLoadContext targetAlc = BuildEnvironmentHelper.Instance.RunningInMSBuildExe ? Default : this; + return targetAlc.LoadFromAssemblyPath(assemblyNameInExecutableDirectory); } return null;