dotnet / dotnet/Scaffolding

[Infrastructure] CodeAnalysis.Compilation bug due to minimal dotnet templates

Open
#1,605 0 comments 0 reactions 0 assignees View on GitHub
area-scaffolding
Dominant language
C#
Stars
818
Forks
260
Avg merge
1d 8h
Merged PRs (30d)
10

Description

**Version Used**:
- CodeAnalysis package versions = 4.0.0-3.21376.3
- Microsoft.CodeAnalysis.Razor package version = 6.0.0-preview.7.21369.1

Bug description :
This bug ONLY an issue with new minimal hosting scenario C# templates for .NET 6 projects using the .NET 6 Preview 7 sdk and do not occur in projects with older Program and Startup.cs files even with the newer sdk.

[Scaffolding ](https://github.com/dotnet/scaffolding)relies on this for in-memory compilation after adding/editing syntax trees in the C# project. For this, we use Compilation.AddSyntaxTrees [here](https://github.com/dotnet/Scaffolding/blob/9a7e0a89ac4e4af1021946a54bff2fcb1558678d/src/Scaffolding/VS.Web.CG.EFCore/EntityFrameworkModelProcessor.cs#L332). With the new edited compilation, we try to check for any errors in the compilation using this [method ](https://github.com/dotnet/Scaffolding/blob/9a7e0a89ac4e4af1021946a54bff2fcb1558678d/src/Scaffolding/VS.Web.CG.Utils/CommonUtilities.cs#L17)(Compilation.Emit).
Regardless of editing/adding new syntax trees, even a Compilation of the base project with no additions produces the following errors. These pertain to the minimal templates mentioned above (missing namespaces and {} placements).

```
error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Index.cshtml.cs(8,22): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Privacy.cshtml.cs(10,25): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Index.cshtml.cs(10,23): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Error.cshtml.cs(15,22): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Error.cshtml.cs(17,23): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
```

Target framework: .NET 6
Operating system: Win x64
SDK version : 6.0.100-preview.7.21376.15.

**Steps to Reproduce**:

1. Create a new dotnet web app (using .NET 6.0.100-preview.7.21376.15)
2. Create a workspace, load all the assembly references, files and dependencies for the new webapp in a different project (similar to our command line tool).
3. Get the CodeAnalysis.Compilation, and try to stream it in-memory using CodeAnalysis.Compilation.Emit (what we do after making changes to said compilation for in memory compilation).

**Expected Behavior**:
1. No errors as its a project with the default templates (evident after performing a `dotnet build`

**Actual Behavior**:
Following errors in CodeAnalysis.Emit.EmitResult :
```
error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Index.cshtml.cs(8,22): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Privacy.cshtml.cs(10,25): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Index.cshtml.cs(10,23): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Error.cshtml.cs(15,22): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
D:\WebProject\Pages\Error.cshtml.cs(17,23): error CS0246: The type or namespace name 'ILogger<>' could not be found (are you missing a using directive or an assembly reference?)
```

Blocks [Scaffolding ](https://github.com/dotnet/scaffolding)command line [scenarios](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator?view=aspnetcore-5.0) and VS scenarios; [identity](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio) [ef scaffolders](https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-5.0&tabs=visual-studio)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.