diff --git a/src/Build.UnitTests/NetTaskHost_E2E_Tests.cs b/src/Build.UnitTests/NetTaskHost_E2E_Tests.cs index 04c583d308d..808ebbc431d 100644 --- a/src/Build.UnitTests/NetTaskHost_E2E_Tests.cs +++ b/src/Build.UnitTests/NetTaskHost_E2E_Tests.cs @@ -4,6 +4,7 @@ using System; using System.IO; using Microsoft.Build.Internal; +using Microsoft.Build.Shared; using Microsoft.Build.UnitTests; using Microsoft.Build.UnitTests.Shared; using Shouldly; @@ -14,6 +15,8 @@ namespace Microsoft.Build.Engine.UnitTests { public class NetTaskHost_E2E_Tests { + private const string LatestDotNetCoreForMSBuild = "net10.0"; + private static string AssemblyLocation { get; } = Path.Combine(Path.GetDirectoryName(typeof(NetTaskHost_E2E_Tests).Assembly.Location) ?? AppContext.BaseDirectory); private static string TestAssetsRootPath { get; } = Path.Combine(AssemblyLocation, "TestAssets"); @@ -44,6 +47,15 @@ public void NetTaskHostTest() successTestTask.ShouldBeTrue(); testTaskOutput.ShouldContain($"The task is executed in process: dotnet"); testTaskOutput.ShouldContain($"Process path: {bootstrapCorePath}", customMessage: testTaskOutput); + + var customTaskAssemblyLocation = Path.GetFullPath(Path.Combine(AssemblyLocation, "..", LatestDotNetCoreForMSBuild, "ExampleTask.dll")); + var resource = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword( + "TaskAssemblyLocationMismatch", + + // Microsoft.Build.dll represents TaskHostTask wrapper for the custom task here. + Path.Combine(RunnerUtilities.BootstrapRootPath, "net472", "MSBuild", "Current", "Bin", "Microsoft.Build.dll"), + customTaskAssemblyLocation); + testTaskOutput.ShouldNotContain(resource); } } } diff --git a/src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs b/src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs index a8ea306fb91..b5551809c8e 100644 --- a/src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs +++ b/src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs @@ -337,16 +337,42 @@ public bool InitializeForBatch(TaskLoggingContext loggingContext, ItemBucket bat } string realTaskAssemblyLocation = TaskInstance.GetType().Assembly.Location; - if (!string.IsNullOrWhiteSpace(realTaskAssemblyLocation) && - realTaskAssemblyLocation != _taskFactoryWrapper.TaskFactoryLoadedType.Path) + + // When MSBuild loads a task assembly, it uses Assembly.LoadFrom() with a specific path, + // but .NET then loads based on the assembly identity with that path only as a hint. + // This can result in the assembly being loaded from a different location than expected: + // + // 1. Assembly loading from the Global Assembly Cache (GAC) if that assembly version is GACed + // 2. Assembly loading from elsewhere if someone already loaded an assembly with the same + // identity from a different path + // + // Both scenarios can result in confusing task behavior because you're not loading the + // assembly you intended. MSBuild tells .NET Framework to load a specific assembly, + // but .NET Framework may load something else entirely. + // + // Common example: A NuGet package task that doesn't change its assembly version between + // package versions. When you update the package and build while worker nodes are still + // alive, the task stays loaded from the old version instead of the new one. + // + // This validation helps identify these scenarios by checking if the loaded assembly + // location matches what we expected, and logging a message when there's a mismatch + // to help diagnose confusing task behavior issues. + if (!string.IsNullOrWhiteSpace(realTaskAssemblyLocation) && realTaskAssemblyLocation != _taskFactoryWrapper.TaskFactoryLoadedType.Path) { - _taskLoggingContext.LogComment(MessageImportance.Normal, "TaskAssemblyLocationMismatch", realTaskAssemblyLocation, _taskFactoryWrapper.TaskFactoryLoadedType.Path); + if (!IsTaskAssemblyMatchFactoryType()) + { + _taskLoggingContext.LogComment(MessageImportance.Normal, "TaskAssemblyLocationMismatch", realTaskAssemblyLocation, _taskFactoryWrapper.TaskFactoryLoadedType.Path); + } } TaskInstance.BuildEngine = _buildEngine; TaskInstance.HostObject = _taskHost; return true; + + // Function to validate that if this is a TaskHostTask, the assembly it loaded is the same one we found in the registry. + bool IsTaskAssemblyMatchFactoryType() => TaskInstance is not TaskHostTask tht + || tht.LoadedTaskAssemblyInfo.AssemblyLocation == _taskFactoryWrapper.TaskFactoryLoadedType.Path; } /// diff --git a/src/Build/Instance/TaskFactories/TaskHostTask.cs b/src/Build/Instance/TaskFactories/TaskHostTask.cs index 6d90fa71380..be7a41298e9 100644 --- a/src/Build/Instance/TaskFactories/TaskHostTask.cs +++ b/src/Build/Instance/TaskFactories/TaskHostTask.cs @@ -204,6 +204,11 @@ public ITaskHost HostObject } } + /// + /// Gets information about the assembly from which the task type was loaded. + /// + public AssemblyLoadInfo LoadedTaskAssemblyInfo => _taskType.Assembly; + /// /// Sets the requested task parameter to the requested value. ///