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
77 changes: 77 additions & 0 deletions src/Build.UnitTests/BackEnd/DotNetRuntimeTask_EndToEnd_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.UnitTests;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
using Xunit.NetCore.Extensions;

namespace Microsoft.Build.Engine.UnitTests.BackEnd
{
/// <summary>
/// End-to-end tests for .NET Runtime task error handling.
/// These tests verify that MSBuild 17.14 gives a clear error when attempting to use Runtime="NET" tasks.
/// </summary>
public class DotNetRuntimeTask_EndToEnd_Tests
{
private readonly ITestOutputHelper _output;

public DotNetRuntimeTask_EndToEnd_Tests(ITestOutputHelper output)
{
_output = output;
}

/// <summary>
/// Test that BuildManager API gives clear error (MSB4233) when attempting to build a project
/// that uses a task with Runtime="NET".
/// </summary>
[WindowsFullFrameworkOnlyFact]
public void BuildManager_WithDotNetRuntimeTask_ShowsClearError()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot fix the tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it fails with
src\MSBuild.UnitTests\DotNetRuntimeTask_CLI_Tests.cs(40,76): error CS0234: (NETCORE_ENGINEERING_TELEMETRY=Build) The type or namespace name 'ProcessIdTask' does not exist in the namespace 'Microsoft.Build.UnitTests' (are you missing an assembly reference?)

@copilot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests currently build successfully. Could you provide more details about what issue needs to be fixed? Are there specific test failures in CI, or specific behavior that needs to be changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the compilation error by constructing the assembly path dynamically at runtime instead of using typeof(ProcessIdTask) which required a compile-time reference. The test now builds the path using Assembly.GetExecutingAssembly().Location and Path.Combine. (commit: 3f5d8a1)

{
// This test uses a real task but specifies Runtime="NET" which is not supported on .NET Framework builds.
// We expect the build to fail with MSB4233 error that clearly explains the issue.
#if NETFRAMEWORK
string projectContent = @"
<Project>
<UsingTask TaskName=""ProcessIdTask"" AssemblyName=""Microsoft.Build.Engine.UnitTests"" Runtime=""NET"" />
<Target Name='TestTask'>
<ProcessIdTask>
<Output PropertyName=""PID"" TaskParameter=""Pid"" />
</ProcessIdTask>
</Target>
</Project>";

using (var env = TestEnvironment.Create(_output))
{
var testProject = env.CreateTestProjectWithFiles(projectContent);
var logger = new MockLogger(_output);

var parameters = new BuildParameters
{
Loggers = new[] { logger }
};

var result = Helpers.BuildProjectFileUsingBuildManager(testProject.ProjectFile, logger, parameters, targetsToBuild: new[] { "TestTask" });

// Build should fail
result.OverallResult.ShouldBe(BuildResultCode.Failure);

// Should log MSB4233 error
logger.ErrorCount.ShouldBeGreaterThan(0);
logger.Errors[0].Code.ShouldBe("MSB4233");

// Error message should contain the task name and indicate .NET runtime is not supported
string? errorMessage = logger.Errors[0].Message;
errorMessage.ShouldNotBeNull();
errorMessage.ShouldContain("ProcessIdTask");
errorMessage.ShouldContain(".NET runtime");
errorMessage.ShouldContain("MSBuild 18.0");
}
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,20 @@ internal static string GetMSBuildLocationFromHostContext(HandshakeOptions hostCo
/// </summary>
internal bool AcquireAndSetUpHost(HandshakeOptions hostContext, INodePacketFactory factory, INodePacketHandler handler, TaskHostConfiguration configuration)
{
#if NETFRAMEWORK
// MSB4233: Check if .NET runtime is requested, which is not supported in .NET Framework builds of MSBuild (17.14 and earlier).
// .NET Core/5+ builds of MSBuild (18.0+) do support .NET runtime tasks, so this check is not needed there.
// This provides a clear error message instead of the confusing "MSBuild.dll not found" error that would otherwise occur.
if ((hostContext & HandshakeOptions.NET) == HandshakeOptions.NET)
{
ProjectFileErrorUtilities.ThrowInvalidProjectFile(
new BuildEventFileInfo(configuration.ProjectFileOfTask, configuration.LineNumberOfTask, configuration.ColumnNumberOfTask),
"TaskRuntimeNET",
configuration.TaskName,
configuration.TaskLocation);
}
#endif

bool nodeCreationSucceeded;
if (!_nodeContexts.ContainsKey(hostContext))
{
Expand Down
4 changes: 4 additions & 0 deletions src/Build/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,10 @@
<value>MSB4217: Task host node exited prematurely. Diagnostic information may be found in files in the temporary files directory named MSBuild_*.failure.txt. {0}</value>
<comment>{StrBegin="MSB4217: "}</comment>
</data>
<data name="TaskRuntimeNET" xml:space="preserve">
<value>MSB4233: The task "{0}" from assembly "{1}" was specified to load with the .NET runtime, but this version of MSBuild does not support loading tasks with that runtime. To run .NET tasks, MSBuild 18.0 or Visual Studio 2026 or higher must be used.</value>
<comment>{StrBegin="MSB4233: "}{Locked=".NET"}{Locked="MSBuild"}{Locked="Visual Studio 2026"}LOCALIZATION: .NET, MSBuild, and Visual Studio 2026 should not be localized.</comment>
</data>
<data name="TaskParametersError" xml:space="preserve">
<value>MSB4063: The "{0}" task could not be initialized with its input parameters. {1}</value>
<comment>{StrBegin="MSB4063: "}</comment>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading