sdk icon indicating copy to clipboard operation
sdk copied to clipboard

CS8785 error after updating from 8.0.304 to 8.0.306 SDK

Open bh-sijtnic opened this issue 1 year ago • 4 comments

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

bh-sijtnic avatar Oct 14 '24 09:10 bh-sijtnic

@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.

manodasanW avatar Oct 15 '24 08:10 manodasanW

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.'

bh-sijtnic avatar Oct 15 '24 09:10 bh-sijtnic

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 avatar Oct 21 '24 16:10 ekalchev

The only fix that works for me is:

dotnet-core-uninstall remove 8.0.403 --sdk

ekalchev avatar Oct 21 '24 17:10 ekalchev

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.

KarlKl avatar Oct 28 '24 13:10 KarlKl

@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.

baronfel avatar Oct 29 '24 19:10 baronfel

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

Image

ekalchev avatar Oct 29 '24 20:10 ekalchev

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.

baronfel avatar Oct 29 '24 21:10 baronfel

@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>

ekalchev avatar Oct 30 '24 07:10 ekalchev

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

baronfel avatar Oct 30 '24 18:10 baronfel

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.

manodasanW avatar Oct 31 '24 02:10 manodasanW