dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.74k stars 1.07k forks source link

CS8785 error after updating from 8.0.304 to 8.0.306 SDK #44145

Open bh-sijtnic opened 1 month ago

bh-sijtnic commented 1 month ago

Describe the bug

After updating the .NET 8 SDK from 8.0.304 to the latest 8.0.306 some or our projects targeting framework net8.0-windows10.0.19041 fail with error. We use Visual Studio 2022 17.10.7.

CSC : error CS8785: Generator 'WinRTAotSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may as a result. Exception was of type 'IndexOutOfRangeException' with message 'Index was outside the bounds of the array.'

and

C:\Development\RepoName\.int\Debug\projectname\net8.0-windows10.0.19041\win-x64\WinRT.SourceGenerator\Generator.WinRTAotSourceGenerator\WinRTGlobalVtableLookup.g.cs(5,19): error CS0116: A namespace cannot directly contain members such as fields, methods or statements

We did find that adding this to our Directory.Build.props code base "resolves" the issue, though in the documentation we can read its not recommended to use this property. For now we can keep this as a workarround, but we would like to understand the proper fix.

<WindowsSdkPackageVersion>10.0.19041.34</WindowsSdkPackageVersion>

To Reproduce

I have not been able to reproduce this yet outside our own code base. We don't know where this code generator comes from and what it triggers.

Exceptions (if any)

CSC : error CS8785: Generator 'WinRTAotSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may as a result. Exception was of type 'IndexOutOfRangeException' with message 'Index was outside the bounds of the array.'

and

C:\Development\RepoName\.int\Debug\projectname\net8.0-windows10.0.19041\win-x64\WinRT.SourceGenerator\Generator.WinRTAotSourceGenerator\WinRTGlobalVtableLookup.g.cs(5,19): error CS0116: A namespace cannot directly contain members such as fields, methods or statements

Further technical details

.NET SDKs installed: 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.512 [C:\Program Files\dotnet\sdk] 3.1.423 [C:\Program Files\dotnet\sdk] 5.0.416 [C:\Program Files\dotnet\sdk] 6.0.310 [C:\Program Files\dotnet\sdk] 6.0.427 [C:\Program Files\dotnet\sdk] 8.0.304 [C:\Program Files\dotnet\sdk] 8.0.306 [C:\Program Files\dotnet\sdk]

Visual Studio 2022 - 17.10.7

Windows 11 Version 23H2

manodasanW commented 1 month ago

@Nico-1987 I saw you were able to workaround the issue. Can you also try the WindowsSdkPackageVersion for the latest Windows SDK projection update with the current known issues fixed to confirm whether it addresses your issue or not. If it doesn't, can you post a snippet of the generated code where the error happens in an issue that you open in the CsWinRT repo. If it does resolve it, this version will be the default in next month's .NET SDK servicing update at which point you can remove the WindowsSdkPackageVersion property.

bh-sijtnic commented 1 month ago

Thanks for the quick update, i just tested in our .NET 6 product with

    <WindowsSdkPackageVersion>10.0.19041.52</WindowsSdkPackageVersion>

This did not fix the issue, actually the whole WinRT.SourceGenerator folder, and thus Generator.WinRTAotSourceGenerator\WinRTGlobalVtableLookup.g.cs does not exist (anymore?) in our obj path (for which we use a custom path)

    <IntermediateFolder>$([MSBuild]::NormalizePath($(RootSourceFolder).int/))</IntermediateFolder>

Error: CSC : error CS8785: Generator 'WinRTAotSourceGenerator' failed to generate source. It will not contribute to t he output and compilation errors may occur as a result. Exception was of type 'IndexOutOfRangeException' with me ssage 'Index was outside the bounds of the array.'

ekalchev commented 1 month ago

I suffer from the same issue after .Net 8 SDK update to 8.0.403

25>        Microsoft (R) Visual C# Compiler version 4.11.0-3.24376.4 (5e3a11e2)
25>        Copyright (C) Microsoft Corporation. All rights reserved.
25>        CSC : error CS8785: Generator 'WinRTAotSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'IndexOutOfRangeException' with message 'Index was outside the bounds of the array.'.
25>        System.IndexOutOfRangeException: Index was outside the bounds of the array.
25>        at Generator.WinRTAotSourceGenerator.GetVtableAttributesToAddOnLookupTable(GeneratorSyntaxContext context, TypeMapper typeMapper, Func`3 isWinRTType, Func`2 isWinRTClass)
25>        at Generator.WinRTAotSourceGenerator.GetVtableAttributesToAddOnLookupTable(GeneratorSyntaxContext context, TypeMapper typeMapper)
25>        at Generator.WinRTAotSourceGenerator.<>c.<Initialize>b__0_17(ValueTuple`2 t, CancellationToken _)
25>        at Microsoft.CodeAnalysis.TransformNode`2.<>c__DisplayClass6_0.<.ctor>b__0(TInput i, CancellationToken token)
25>        at Microsoft.CodeAnalysis.TransformNode`2.UpdateStateTable(Builder builder, NodeStateTable`1 previousTable, CancellationToken cancellationToken)
25>        -----
25>        CompilerServer: server - server processed compilation - WordApp_d0yh1ael_wpftmp (net8.0-windows10.0.17763.0)

I tried all workarounds for this issue but nothing works

ekalchev commented 1 month ago

The only fix that works for me is:

dotnet-core-uninstall remove 8.0.403 --sdk

KarlKl commented 3 weeks ago

Have you tried the workaround to disable WinRT AOT optimizer in project file, that was proposed here?

<CsWinRTAotOptimizerEnabled>false</CsWinRTAotOptimizerEnabled>

At least for me it did work.

EDIT: Sorry, I just saw that you posted there that it didn't work for you.

baronfel commented 3 weeks ago

@ekalchev please provide a binlog - we think the issue should be fixed but without further details we can't dig into what's going on in your system.

ekalchev commented 3 weeks ago

@baronfel is this enough or you need the entire log?

Image

baronfel commented 3 weeks ago

Unfortunately It's not enough to double check if you applied the workaround described above and/or see if there's some other context in your working environment that triggers the issue.

If you can create a developer community ticket to securly upload the binlog we can privately investigate if that's alright with you.

ekalchev commented 3 weeks ago

@baronfel Here the ticket https://developercommunity.visualstudio.com/t/Building-with-SDK-80403-throws-CSC-err/10779441 I attached two binlog files with and without

<CsWinRTAotOptimizerEnabled>false</CsWinRTAotOptimizerEnabled>

baronfel commented 3 weeks ago

@manodasanW can you take a look at the binlogs and offer guidance?

manodasanW commented 3 weeks ago

I believe we have fixed this issue for our next update. But CsWinRTAotOptimizerEnabled set to false should have workaround this issue at least with the version mentioned here of the Windows SDK projection. I will take a look at the binlogs to see why it didn't help.