Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
serhiikuksov3shape opened this issue Mar 28, 2024 · 8 comments
Closed

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

serhiikuksov3shape opened this issue Mar 28, 2024 · 8 comments
Labels
bug Documentation Issues about docs, including errors and areas we should extend (this repo and learn.microsoft.com) triaged

Comments

@serhiikuksov3shape
Copy link

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

@rainersigwald
Copy link
Member

@serhiikuksov3shape how are you invoking the build?

@serhiikuksov3shape
Copy link
Author

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

@rainersigwald
Copy link
Member

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

@serhiikuksov3shape
Copy link
Author

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.

@rainersigwald
Copy link
Member

rainersigwald commented Mar 28, 2024

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.

@3s-ap
Copy link

3s-ap commented Apr 1, 2024

Hi @rainersigwald,

Is there any other option to run c# code on the "afterbuild" event, in Visual Studio (.NET 6 only)?

@KalleOlaviNiemitalo
Copy link

If you build your code as an executable, you can use the Exec task to run it from the post-build target. But then it cannot use the MSBuild API for parameters and logging.

Another option might be to target netstandard2.0 in the task project, but then the project would not be able to reference NuGet packages that support only net6.0. IIRC, referencing Microsoft.Build.Utilities.Core should be OK, though.

@AR-May AR-May added Documentation Issues about docs, including errors and areas we should extend (this repo and learn.microsoft.com) triaged labels Apr 2, 2024
@serhiikuksov3shape
Copy link
Author

Thank you for your answers, we already found a workaround for this issue, so it can be closed.

@rainersigwald rainersigwald closed this as not planned Won't fix, can't repro, duplicate, stale Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Documentation Issues about docs, including errors and areas we should extend (this repo and learn.microsoft.com) triaged
Projects
None yet
Development

No branches or pull requests

5 participants