msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

Better error when UsingTask Reference Include is empty

Open KirillOsenkov opened this issue 1 year ago • 5 comments

<Project>

    <UsingTask
      TaskName="WriteAttributes"
      TaskFactory="CodeTaskFactory"
      AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
    <Task>
      <Reference Include="$(CppCodeProvider)" />
      <Code Type="Class" Language="cs">
        <![CDATA[
public class WriteAttributes : Task
{
  public override bool Execute()
  {
    return true;
  }
}
      ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="Build">
    <WriteAttributes />
  </Target>

</Project>

The error isn't great here:

image

KirillOsenkov avatar Jul 08 '24 23:07 KirillOsenkov

Team triage: Let's re-phrase the message. Seems quite repetitive.

AR-May avatar Jul 09 '24 13:07 AR-May

While looking into this issue, bug #10389 was found.

GangWang01 avatar Jul 17 '24 07:07 GangWang01

@baronfel would you like to suggest a better error message when UsingTask Reference Include is empty?

GangWang01 avatar Aug 21 '24 10:08 GangWang01

The problem is that Include is required for the Reference element here, but it evaluated to empty?

UsingTask's Reference cannot be empty, but was set to $(CppCodeProvider) which evaluated to empty.

Template:

(Task)'s (Metadata) cannot be empty, but was set to (UnEvaluatedItemSpec) which evaluated to empty.

baronfel avatar Aug 21 '24 14:08 baronfel

CodeTaskFactory claims The "Include" attribute has been set even if the attribute has not been set at all. In this case, there wouldn't be an UnEvaluatedItemSpec for the task factories to log.

Adapted from https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-inline-tasks?view=vs-2022:

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- This simple inline task does nothing. -->
  <UsingTask
    TaskName="DoNothing"
    TaskFactory="CodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
    <ParameterGroup />
    <Task>
      <Reference />
      <Using Namespace="" />
      <Code Type="Fragment" Language="cs">
      </Code>
    </Task>
  </UsingTask>
  <Target Name="first">
    <DoNothing/>
  </Target>
</Project>
MSBuild version 17.10.4+10fbfbf2e for .NET Framework
Build started 21.8.2024 18.02.58.


Project "[REDACTED]\gug.proj" on node 1 (default targets).
[REDACTED]\gug.proj(16,5): error MSB3752: The "Include" attribute has been set but is empty. If the "Include" attribute is set it must not be empty.
[REDACTED]\gug.proj(16,5): error MSB4036: The "DoNothing" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64" directory.
Done Building Project "[REDACTED]\gug.proj" (default targets) -- FAILED.


Build FAILED.

"[REDACTED]\gug.proj" (default target) (1) ->
(first target) ->
  [REDACTED]\gug.proj(16,5): error MSB3752: The "Include" attribute has been set but is empty. If the "Include" attribute is set it must not be empty.
  [REDACTED]\gug.proj(16,5): error MSB4036: The "DoNothing" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the
 "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64" directory.

    0 Warning(s)
    2 Error(s)

https://github.com/dotnet/msbuild/blob/10fbfbf2eeb0597fdc1f600d87d38c7f57317bdc/src/Tasks/CodeTaskFactory.cs#L430-L443


RoslynCodeTaskFactory in .NET SDK 9.0.100-preview.7.24407.12 (MSBuild 17.12.0.37402) likewise:

  gug failed with 2 error(s) (0,1s)
    [REDACTED]\gug.proj(16,5): error MSB3752: The "Include" attribute of the <Reference> element has been set but is empty. If the "Include" attribute is set it must not be empty.
    [REDACTED]\gug.proj(16,5): error MSB4036: The "DoNothing" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "C:\Program Files\dotnet\sdk\9.0.100-preview.7.24407.12" directory.

Build failed with 2 error(s) in 0,2s

https://github.com/dotnet/msbuild/blob/48e81c6f136e6ee3c568d0a38180cfea151129dd/src/Tasks/RoslynCodeTaskFactory/RoslynCodeTaskFactory.cs#L346-L359

KalleOlaviNiemitalo avatar Aug 21 '24 15:08 KalleOlaviNiemitalo