InvalidOperationException from TaskLoggingHelper.FormatResourceString(): The "Csc" task has not registered its resources.
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]
This was intermittent and happened only once.
https://github.com/dotnet/roslyn/issues/46772 is potentially related or same issue.
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
Same on big sur with a .NET Core console application. :(
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()
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.
@jaredpar we think there's an intermittent issue with the Csc task, any quick guesses? Have you seen this before?
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
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.
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.
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
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
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.
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.
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.
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.
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:
- Change
TaskLoggingHelperto initializeTaskResourcesbased on theITaskin the constructor - Change
TaskLoggingHelper.TaskResourcesto fall back toITask.TaskResourcesif the value isnull
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.
Sorry, accidentally button slip and hit re-open.
Change TaskLoggingHelper.TaskResources to fall back to ITask.TaskResources if the value is null
Sounds to me like a reasonable ask, Reopening for triage
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.
@BenVillalobos in dotnet/runtime we've gotten random build failures with this callstack. +1 to some fix this iteration.