Skip to content
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
12 changes: 12 additions & 0 deletions src/Build.UnitTests/NetTaskHost_E2E_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
}
}
32 changes: 29 additions & 3 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,42 @@ public bool InitializeForBatch(TaskLoggingContext loggingContext, ItemBucket bat
}

string realTaskAssemblyLocation = TaskInstance.GetType().Assembly.Location;
Comment thread
YuliiaKovalova marked this conversation as resolved.
Resolved
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.
Comment thread
YuliiaKovalova marked this conversation as resolved.
Resolved
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;
Comment thread
YuliiaKovalova marked this conversation as resolved.
Resolved
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Instance/TaskFactories/TaskHostTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ public ITaskHost HostObject
}
}

/// <summary>
/// Gets information about the assembly from which the task type was loaded.
/// </summary>
public AssemblyLoadInfo LoadedTaskAssemblyInfo => _taskType.Assembly;
Comment thread
YuliiaKovalova marked this conversation as resolved.
Resolved

/// <summary>
/// Sets the requested task parameter to the requested value.
/// </summary>
Expand Down