dotnet / dotnet/msbuild

Consider using #line directives in RoslynCodeTaskFactory

Open
#9,196 3 comments 0 reactions 0 assignees View on GitHub
Area: Engine Area: Tasks backlog Priority:3 triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 8h
Merged PRs (30d)
141

Description

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:

```xml








```

you get

```sh-session
❯ 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 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](https://learn.microsoft.com/dotnet/csharp/language-reference/preprocessor-directives#error-and-warning-information) 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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.