[Unhandled Exception]: ArgumentNullException "Parameter "projectFile" cannot be null" in ctor of InvalidProjectFileException
Issue Description
Found this issue when I tried to use the new https://aka.ms/nettaskhost feature in VS2026 Insiders (11201.2):
I got this error:
Launching task "Task1" from assembly "E:\ws\Test2\publish\Test.dll" in an external task host with a runtime of "NET" and a process architecture of "x64". (TaskId:2)
MSBUILD : error : The task failed to load because it requires the MSBuild .NET Runtime Task Host, but the .NET Runtime Task Host could not be found for the specified version. [E:\ws\Test2\Test.proj]
MSBUILD : error : See https://aka.ms/nettaskhost for details on how to resolve this error. [E:\ws\Test2\Test.proj]
However, I can only see this error when I specify ContinueOnError="true" on my task.
If I don't, MSBuild terminates with an unhandled exception: System.ArgumentNullException: Parameter "projectFile" cannot be null.
Steps to Reproduce
Minimal project (4 files) that repos the issue: Code.zip Just run Test.ps1 from "Developer PowerShell for VS 18"
Actual Behavior
MSBUILD : error : This is an unhandled exception in MSBuild -- PLEASE UPVOTE AN EXISTING ISSUE OR FILE A NEW ONE AT https://aka.ms/msbuild/unhandled [E:\ws\Test2\Test.proj]
MSBUILD : error : System.ArgumentNullException: Parameter "projectFile" cannot be null. [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.Shared.ErrorUtilities.ThrowArgumentNull(String parameterName, String resourceName) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.Exceptions.InvalidProjectFileException..ctor(String projectFile, Int32 lineNumber, Int32 columnNumber, Int32 endLineNumber, Int32 endColumnNumber, String message, String errorSubcategory, String errorCode, String helpKeyword, Exception innerException) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.Exceptions.InvalidProjectFileException..ctor(String message, InvalidProjectFileException innerException) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TaskBuilder.<InitializeAndExecuteTask>d__24.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteBucket>d__19.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteTask>d__18.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteTask>d__13.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TargetEntry.<ProcessBucket>d__50.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TargetEntry.<ExecuteTarget>d__43.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TargetBuilder.<ProcessTargetStack>d__24.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.TargetBuilder.<BuildTargets>d__11.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.RequestBuilder.<BuildProject>d__67.MoveNext() [E:\ws\Test2\Test.proj]
MSBUILD : error : --- End of stack trace from previous location where exception was thrown --- [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [E:\ws\Test2\Test.proj]
MSBUILD : error : at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) [E:\ws\Test2\Test.proj]
MSBUILD : error : at Microsoft.Build.BackEnd.RequestBuilder.<RequestThreadProc>d__58.MoveNext() [E:\ws\Test2\Test.proj]
Analysis
In this line: https://github.com/dotnet/msbuild/blob/27c7c81bbd20f7bf1252782826c1fc8ee1427b2f/src/Build/BackEnd/Components/RequestBuilder/TaskBuilder.cs#L896
A InvalidProjectFileException is wrapped in another InvalidProjectFileException.
The ctor passes the property ProjectFile as the projectFile argument:
https://github.com/dotnet/msbuild/blob/27c7c81bbd20f7bf1252782826c1fc8ee1427b2f/src/Build/Errors/InvalidProjectFileException.cs#L73-L74
The inner ctor throws if projectFile is null despite the fact that the ProjectFile property can (and is) null.
The xml docs explicitly say that it can be null:
https://github.com/dotnet/msbuild/blob/27c7c81bbd20f7bf1252782826c1fc8ee1427b2f/src/Build/Errors/InvalidProjectFileException.cs#L279-L280
The exception seems to originate here: https://github.com/dotnet/msbuild/blob/27c7c81bbd20f7bf1252782826c1fc8ee1427b2f/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcTaskHost.cs#L470
where is was created without a value for projectFile.
Versions & Configurations
msbuild -version:
MSBuild version 18.0.2+995a3dce4 for .NET Framework
18.0.2.52102
dotnet --info:
<details>
.NET SDK:
Version: 10.0.100-rc.2.25502.107
Commit: 89c8f6a112
Workload version: 10.0.100-manifests.4d32cd9e
MSBuild version: 18.0.0-preview-25502-107+89c8f6a11
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.100-rc.2.25502.107\
.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.
Host:
Version: 10.0.0-rc.2.25502.107
Architecture: x64
Commit: 89c8f6a112
.NET SDKs installed:
8.0.415 [C:\Program Files\dotnet\sdk]
9.0.306 [C:\Program Files\dotnet\sdk]
10.0.100-rc.2.25502.107 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.0-rc.2.25502.107 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.0-rc.2.25502.107 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.0-rc.2.25502.107 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
Hi @PhilippNaused ,
Thank you for reporting the issue. Error seems to be relevant, since the required data for running net task host wasn't defined. @rainersigwald / @baronfel , do you have any idea why it's not available it the evaluation properties?
https://github.com/dotnet/msbuild/blob/27c7c81bbd20f7bf1252782826c1fc8ee1427b2f/src/Build/Instance/TaskFactories/AssemblyTaskFactory.cs#L611
This binlog is from a project that doesn't import the Microsoft.NET.SDK - this SDK provides all of the properties we need to operate - trying to use the .NET TaskHost feature from a super-simple project like:
<Project>
<UsingTask TaskName="Test.Task1"
Runtime="NET"
TaskFactory="TaskHostFactory"
AssemblyFile="$(MSBuildThisFileDirectory)publish\Test.dll" />
<Target Name="MyTarget">
<Test.Task1 />
</Target>
</Project>
Isn't supported at all. The only supported way to use the .NET TaskHost feature is through the .NET SDK Resolver, which requires using the Microsoft.NET.SDK MSBuild SDK as a base.
For testing you may be able to hack it by specifying the RuntimeIdentifierGraphPath property and DOTNET_HOST_PATH env var, but this is not supported.
Yup, was typing out the same as @baronfel. We could keep this as a feature request to support that case in the future--but to set expectations, I don't expect that it will be very high priority.
We should have a much clearer error in this case, though.
Thank you for the explanation.
I will make the use of this feature conditional on '$(UsingMicrosoftNETSdk)' == 'true'. That seems to prevent the issue.
What about the unhandled ArgumentNullException? It covered up the inner error message, so without adding ContinueOnError="true", it was impossible to see what went wrong.
What about the unhandled ArgumentNullException? It covered up the inner error message, so without adding ContinueOnError="true", it was impossible to see what went wrong.
We definitely need to make this better, we haven't yet dug into your analysis to see how/where we can mitigate the stack that's confusing things.