msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

InvalidOperationException from TaskLoggingHelper.FormatResourceString(): The "Csc" task has not registered its resources.

Open KirillOsenkov opened this issue 5 years ago • 29 comments

Building using bootstrap MSBuild from 80b7f2dcb has resulted in a Csc failure:

Csc
    Assembly = C:\msbuild\artifacts\bin\bootstrap\net472\MSBuild\Current\Bin\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll
C:\msbuild\artifacts\bin\bootstrap\net472\MSBuild\Current\Bin\Roslyn
    Microsoft (R) Visual C# Compiler version 3.9.0-2.20574.26 (e9fd4dc7)
    Errors
        C:\msbuild\artifacts\bin\bootstrap\net472\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018: The "Csc" task failed unexpectedly.
This is an unhandled exception from a task -- PLEASE OPEN A BUG AGAINST THE TASK OWNER.
System.InvalidOperationException: The "Csc" task has not registered its resources. In order to use the "TaskLoggingHelper.FormatResourceString()" method this task needs to register its resources either during construction, or via the "TaskResources" property.
   at Microsoft.Build.Shared.ErrorUtilities.ThrowInvalidOperation(String resourceName, Object[] args) in C:\msbuild\src\Shared\ErrorUtilities.cs:line 327
   at Microsoft.Build.Shared.ErrorUtilities.VerifyThrowInvalidOperation(Boolean condition, String resourceName, Object arg0) in C:\msbuild\src\Shared\ErrorUtilities.cs:line 368
   at Microsoft.Build.Utilities.TaskLoggingHelper.FormatResourceString(String resourceName, Object[] args) in C:\msbuild\src\Shared\TaskLoggingHelper.cs:line 203
   at Microsoft.Build.Utilities.TaskLoggingHelper.LogErrorWithCodeFromResources(String subcategoryResourceName, String file, Int32 lineNumber, Int32 columnNumber, Int32 endLineNumber, Int32 endColumnNumber, String messageResourceName, Object[] messageArgs) in C:\msbuild\src\Shared\TaskLoggingHelper.cs:line 825
   at Microsoft.Build.Utilities.TaskLoggingHelper.LogErrorWithCodeFromResources(String messageResourceName, Object[] messageArgs) in C:\msbuild\src\Shared\TaskLoggingHelper.cs:line 779
   at Microsoft.CodeAnalysis.BuildTasks.ManagedCompiler.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands)
   at Microsoft.Build.Utilities.ToolTask.Execute() in C:\msbuild\src\Utilities\ToolTask.cs:line 1519
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() in C:\msbuild\src\Build\BackEnd\TaskExecutionHost\TaskExecutionHost.cs:line 578
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() in C:\msbuild\src\Build\BackEnd\Components\RequestBuilder\TaskBuilder.cs:line 809 [C:\Ide\src\CodexShell\CodexShell.csproj]

KirillOsenkov avatar Mar 15 '21 01:03 KirillOsenkov

This was intermittent and happened only once.

https://github.com/dotnet/roslyn/issues/46772 is potentially related or same issue.

KirillOsenkov avatar Mar 15 '21 01:03 KirillOsenkov

I'm running into this issues on MacOs Big Sur when using Project Tye to run a Blazor project. The other projects in the solution (one webapi and one console) build fine. This also only started happening when I upgraded my project to .NET 5

cecilphillip avatar Mar 25 '21 03:03 cecilphillip

Same on big sur with a .NET Core console application. :(

conniey avatar Apr 03 '21 21:04 conniey

I'm having the same issues, on Windows 10, and still no solution so far.

The "Csc" task failed unexpectedly.
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018: System.InvalidOperationException: The "Csc" task has not registered its resources. In order to use the "TaskLoggingHelper.FormatResourceString()" method this task needs to register its resources either during construction, or via the "TaskResources" property.
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018:    at Microsoft.Build.Shared.ErrorUtilities.ThrowInvalidOperation(String resourceName, Object[] args)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018:    at Microsoft.Build.Shared.ErrorUtilities.VerifyThrowInvalidOperation(Boolean condition, String resourceName, Object arg0)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018:    at Microsoft.Build.Utilities.TaskLoggingHelper.FormatResourceString(String resourceName, Object[] args)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018:    at Microsoft.Build.Utilities.TaskLoggingHelper.LogErrorWithCodeFromResources(String messageResourceName, Object[] messageArgs)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018:    at Microsoft.CodeAnalysis.BuildTasks.ManagedCompiler.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018:    at Microsoft.Build.Utilities.ToolTask.Execute()
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(71,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

Edo90 avatar Apr 07 '21 12:04 Edo90

Team Triage: @KirillOsenkov We're wondering how/where this would be an issue with MSBuild. At first glance this looks like an issue with Roslyn as it's in a targets file under Roslyn\ and has to do with the csc task.

benvillalobos avatar Apr 07 '21 15:04 benvillalobos

@jaredpar we think there's an intermittent issue with the Csc task, any quick guesses? Have you seen this before?

KirillOsenkov avatar Apr 07 '21 19:04 KirillOsenkov

As far as I can tell, this is where Csc initializes its resources, which is pretty early on, in the constructor of the base class: https://github.com/dotnet/roslyn/blob/275f4c790945c73ca308716ba56c224a95e13ca9/src/Compilers/Core/MSBuildTask/ManagedCompiler.cs#L59

Should it be calling this constructor instead? https://github.com/dotnet/msbuild/blob/38da84d453b14008b148c7eea28df24ab4443bb7/src/Utilities/ToolTask.cs#L91

KirillOsenkov avatar Apr 07 '21 19:04 KirillOsenkov

Never seen this before nor do I know what is required. Just based off the info here I suspect we're running into the following line

https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/MSBuildTask/ManagedCompiler.cs#L557

At the same time I'm not sure what is missing that we should be doing.

jaredpar avatar Apr 07 '21 19:04 jaredpar

Yup, you're right, it looks like there's an exception on that line (we don't know which one), and when trying to log it, we fail to get the resource string because resources haven't been initialized.

KirillOsenkov avatar Apr 07 '21 19:04 KirillOsenkov

Yeah you need to register resources via the constructor or the TaskResources property

https://github.com/dotnet/msbuild/blob/7804350cd4fe271bee48b3dc99e9f4681ee686c5/src/Utilities/Task.cs#L123

jeffkl avatar Apr 07 '21 19:04 jeffkl

I've filed Roslyn issue https://github.com/dotnet/roslyn/issues/52467 - would be nice to fix this soon, as a lot of customers are hitting this

KirillOsenkov avatar Apr 07 '21 19:04 KirillOsenkov

Yeah you need to register resources via the constructor or the TaskResources property

Turn out this isn't 100% true. The problem is that we're going through TaskLoggingHelper. Even though that takes an ITask as a constructor argument it does not utilize the ITask.TaskResources property. Instead you have to manually set that up on TaskLoggingHelper instances.

So even though our Task registered everything properly the TaskLoggingHelper didn't take advantage of it. That's a bit unintuitive.

jaredpar avatar Apr 20 '21 23:04 jaredpar

Yeah I think I saw you're creating an isolated TaskLoggingHelper that's disconnected from the task, and one probably needs to pass stuff to it for it to work as expected. Unfortunately this silly exception is hiding the real exception that's happening in the compiler server.

KirillOsenkov avatar Apr 20 '21 23:04 KirillOsenkov

Yep. Thats' what I get for trying to use the right API for logging exceptions 😦

Fix is easy but testing requires me to restructure the code a bit.

jaredpar avatar Apr 21 '21 00:04 jaredpar

Team Triage: Closing since the fix is available within roslyn. @jaredpar if you think there's anything we can do in our API to make this easier, let us know.

benvillalobos avatar Apr 21 '21 15:04 benvillalobos

Team Triage: Closing since the fix is available within roslyn. @jaredpar if you think there's anything we can do in our API to make this easier, let us know.

IMHO, you should really consider one of two bug fixes here:

  1. Change TaskLoggingHelper to initialize TaskResources based on the ITask in the constructor
  2. Change TaskLoggingHelper.TaskResources to fall back to ITask.TaskResources if the value is null

This seems relatively safe because in the cases where this has an impact the code would already be in a failure state. I imagine I'm not the only one who making the assumption that a type named TaskLoggingHelper would attempt to get critical state from the provided ITask.

jaredpar avatar Apr 21 '21 15:04 jaredpar

Sorry, accidentally button slip and hit re-open.

jaredpar avatar Apr 21 '21 15:04 jaredpar

Change TaskLoggingHelper.TaskResources to fall back to ITask.TaskResources if the value is null

Sounds to me like a reasonable ask, Reopening for triage

benvillalobos avatar Apr 21 '21 16:04 benvillalobos

BLARG you also need to manually set IBuildEngine in the TaskLoggingHelper.

At this point I think the fastest way to fix this bug is to stop using TaskLoggingHelper. It's just causing pain at this point because the usability level is not good. Really think you all should add constructors that actually take the arguments necessary to make type functional and deprecate the existing ones. Current state is just leading users to this type of bug.

jaredpar avatar Apr 22 '21 15:04 jaredpar

@BenVillalobos in dotnet/runtime we've gotten random build failures with this callstack. +1 to some fix this iteration.

danmoseley avatar May 19 '21 15:05 danmoseley