Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
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
58 changes: 58 additions & 0 deletions src/Build.UnitTests/BackEnd/AssemblyLoadContextTestTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,69 @@

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.Reflection;

#nullable disable

namespace AssemblyLoadContextTest
{
/// <summary>
/// Task that validates assembly version roll-forward behavior.
/// Tests that MSBuildLoadContext accepts newer assembly versions when older versions are requested.
/// </summary>
public class ValidateAssemblyVersionRollForward : Task
{
/// <summary>
/// The name of the assembly to check (e.g., "System.Collections.Immutable")
/// </summary>
[Required]
public string AssemblyName { get; set; }

/// <summary>
/// The minimum expected version (e.g., "1.0.0.0")
/// </summary>
[Required]
public string MinimumVersion { get; set; }

public override bool Execute()
{
try
{
// Try to load the assembly by name with minimum version
var minimumVersion = Version.Parse(MinimumVersion);
var assemblyName = new AssemblyName(AssemblyName)
{
Version = minimumVersion
};

// This will trigger MSBuildLoadContext.Load which should accept newer versions
var assembly = Assembly.Load(assemblyName);
var loadedVersion = assembly.GetName().Version;

Log.LogMessage(MessageImportance.High,
$"Requested {AssemblyName} version {minimumVersion}, loaded version {loadedVersion}");

// Verify that we got a version >= minimum
if (loadedVersion < minimumVersion)
{
Log.LogError(
$"Assembly version roll-forward failed: requested {minimumVersion}, but loaded {loadedVersion} which is older");
return false;
}

Log.LogMessage(MessageImportance.High,
$"Assembly version roll-forward succeeded: loaded version {loadedVersion} >= requested {minimumVersion}");
return true;
}
catch (Exception ex)
{
Log.LogErrorFromException(ex, showStackTrace: true);
return false;
}
}
}

public class RegisterObject : Task
{
internal const string CacheKey = "RegressionForMSBuild#5080";
Expand Down
27 changes: 27 additions & 0 deletions src/Build.UnitTests/BackEnd/TaskBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,33 @@ public void SameAssemblyFromDifferentRelativePathsSharesAssemblyLoadContext()
logger.AssertLogDoesntContain("MSB4018");
}

#if FEATURE_ASSEMBLYLOADCONTEXT
/// <summary>
/// Regression test for https://github.com/dotnet/msbuild/issues/12370
/// Verifies that MSBuildLoadContext accepts newer assembly versions when older versions are requested (version roll-forward).
/// </summary>
[Fact]
public void MSBuildLoadContext_AcceptsNewerAssemblyVersions()
{
string realTaskPath = Assembly.GetExecutingAssembly().Location;

// Use System.Collections.Immutable as test assembly - it's available in modern .NET runtime
// Request an older version (1.0.0.0) which should roll forward to whatever version is available
string projectContents = @"<Project ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>
<UsingTask TaskName=`ValidateAssemblyVersionRollForward` AssemblyFile=`" + realTaskPath + @"` />

<Target Name=`Build`>
<ValidateAssemblyVersionRollForward AssemblyName=`System.Collections.Immutable` MinimumVersion=`1.0.0.0` />
</Target>
</Project>";

MockLogger logger = ObjectModelHelpers.BuildProjectExpectSuccess(projectContents, _testOutput);

// Verify that the task logged success message
logger.AssertLogContains("Assembly version roll-forward succeeded");
}
#endif


#if FEATURE_CODETASKFACTORY
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/MSBuildLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public MSBuildLoadContext(string assemblyPath)
}

AssemblyName candidateAssemblyName = AssemblyLoadContext.GetAssemblyName(candidatePath);
if (candidateAssemblyName.Version != assemblyName.Version)
if (candidateAssemblyName.Version < assemblyName.Version)
{
continue;
}
Expand Down
Loading