dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.

Home Page:https://docs.microsoft.com/visualstudio/msbuild/msbuild

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Task failing unexpectedly on .Net 6.0 platform

serhiikuksov3shape opened this issue · comments

Issue Description

After the migration from .Net48 to .Net6.0 I`m getting weird errors on a post-build event that triggers a custom task.

Error MSB4018 The "PostBuildTask" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at Core.PostBuildTask.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.d__26.MoveNext()

My csproj file:

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>net6.0</TargetFramework>
	</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.9.5" />
  </ItemGroup>

  <UsingTask TaskName="PostBuildTask" AssemblyFile="C:\Git\TestingTaskIssue\Core\bin\Debug\net6.0\Core.dll" />
  <Target Name="ExecutePostBuildTask" AfterTargets="afterbuild" >
	  <PostBuildTask />
  </Target>

</Project>

Steps to Reproduce

Issue.zip

Expected Behavior

Task is running without errors

Actual Behavior

Error MSB4018 The "PostBuildTask" task failed unexpectedly.

Analysis

No response

Versions & Configurations

No response

@serhiikuksov3shape how are you invoking the build?

You need just to rebuild the solution to see an error.

@serhiikuksov3shape exactly how please? What command line are you invoking?

I was building this in Visual Studio 2022, by right click on Solution --> Rebuild. Also, this can be achieved with
msbuild TestingTaskIssue.sln /t:Rebuild. I can attach an archive with a whole solution if it will help.

No need, that reveals the problem: you're running the build from a .NET Framework process, so it can't run a .NET 6 DLL. If you want your task to work in that situation you must target .NET Framework 4.7.2. If you can run all builds with dotnet build you can target only .NET 6, but that precludes using Visual Studio. Many tasks multitarget and select with conditions on $(MSBuildRuntimeType) in the UsingTask.

It may be possible at some point in the future to run a .NET 6 task in these scenarios; that's tracked as #4834.