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

Consider using #line directives in RoslynCodeTaskFactory #9196

Open
rainersigwald opened this issue Aug 31, 2023 · 3 comments
Open

Consider using #line directives in RoslynCodeTaskFactory #9196

rainersigwald opened this issue Aug 31, 2023 · 3 comments
Labels
Area: Engine Issues impacting the core execution of targets and tasks. Area: Tasks Issues impacting the tasks shipped in Microsoft.Build.Tasks.Core.dll. backlog Priority:3 Work that is nice to have triaged

Comments

@rainersigwald
Copy link
Member

The CodeTaskFactory implementations can work by being given a snippet of C#, which is assembled into a full class definition and fed to the compiler. But errors in C# syntax are reported in a convoluted way. Given:

<Project>
  <UsingTask
    TaskName="HelloWorld"
    TaskFactory="RoslynCodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
    <ParameterGroup />
    <Task>
      <Code Type="Fragment" Language="cs">
<![CDATA[
C# syntax error
]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="Go">
    <HelloWorld />
  </Target>
</Project>

you get

msbuild .\foo.proj
MSBuild version 17.8.0-preview-23423-02+1d8146d3d for .NET Framework
  foo failed with errors (0.1s)
    C:\Users\raines\AppData\Local\Temp\MSBuildTempraines\tmp36947aa3db784f3b87b3ca8bfd166d69.tmp(37,2): error CS1040: Preprocessor directives must appear as the first non-whitespace character on a line
    C:\Users\raines\AppData\Local\Temp\MSBuildTempraines\tmp36947aa3db784f3b87b3ca8bfd166d69.tmp(37,2): error CS1002: ; expected
    S:\play\codetaskfactory-errors\foo.proj(17,5): error : The source file for this compilation can be found at: "C:\Users\raines\AppData\Local\Temp\MSBuildTempraines\tmp36947aa3db784f3b87b3ca8bfd166d69.tmp"
    S:\play\codetaskfactory-errors\foo.proj(17,5): error MSB4036: The "HelloWorld" 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\Preview\MSBuild\Current\Bin\amd64" directory.

Build failed with errors in 0.2s

This makes sense given the details of the system, but is hard to chase back to the original error.

By using the #line directive we could change the line reported by the compilation to be the line in the project file (or included .cs fragment) instead, which would be clearer.

@rainersigwald rainersigwald added backlog Area: Tasks Issues impacting the tasks shipped in Microsoft.Build.Tasks.Core.dll. Area: Engine Issues impacting the core execution of targets and tasks. Priority:3 Work that is nice to have labels Aug 31, 2023
@rainersigwald
Copy link
Member Author

(this arose from a conversation with @MichalPavlik about the inline-task UX)

@KalleOlaviNiemitalo
Copy link

But consider this kind of mistake (using Type="Method" when Type="Class" is needed):

<Project>
  <UsingTask
    TaskName="HelloWorld"
    TaskFactory="RoslynCodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
    <ParameterGroup />
    <Task>
      <Code Type="Method" Language="cs">
<![CDATA[
public class HelloWorld : Microsoft.Build.Framework.ITask
{
    public Microsoft.Build.Framework.IBuildEngine BuildEngine { get; set; }
    public Microsoft.Build.Framework.ITaskHost HostObject { get; set; }
    public bool Execute() => true;
}
]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="Go">
    <HelloWorld />
  </Target>
</Project>

Here, "error CS0542: 'HelloWorld': member names cannot be the same as their enclosing type" becomes more difficult to understand if the user cannot see the context in the generated C# file.
I hope "The source file for this compilation can be found at" would still be shown.

@danmoseley
Copy link
Member

Just curious, why do we need to write the code to a temp file for RoslynCodeTaskFactory? I know that was inevitable as part of how Code DOM worked ,but Roslyn can accept memory streams surely.

@AR-May AR-May added the triaged label Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Engine Issues impacting the core execution of targets and tasks. Area: Tasks Issues impacting the tasks shipped in Microsoft.Build.Tasks.Core.dll. backlog Priority:3 Work that is nice to have triaged
Projects
None yet
Development

No branches or pull requests

4 participants