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

KeyNotFoundException thrown when compiling optional generic partial methods (no modifier) #72431

Closed
datvm opened this issue Mar 7, 2024 · 6 comments · Fixed by #73060
Closed
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@datvm
Copy link

datvm commented Mar 7, 2024

Version Used: Microsoft (R) Visual C# Compiler version 4.9.0-3.24121.1 (a98c90d)

Steps to Reproduce:

Unfortunately I cannot reproduce it in a new project so I do not know what causes it. I hope you can find out from the stack trace. Originally reported at dotnet/sdk#39033:

I have a class with this declaration:

partial void AddPlatformDailyRewards<T>(MauiAppBuilder builder) where T : DailyRewardItem;

According to the documentation, I should not need to implement this mehtod. However, if I do not implement it for each platform, I would encounter this issue when building (the exception is from CSC.exe):

16:18:31:714	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error : Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at System.ThrowHelper.ThrowKeyNotFoundException()
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.FullMetadataWriter.GetMethodDefinitionHandle(IMethodDefinition def)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.GetMethodDefinitionOrReferenceHandle(IMethodReference methodReference)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MethodSpecComparer.GetHashCode(IGenericMethodInstanceReference methodInstanceReference)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.InstanceAndStructuralReferenceIndex`1.TryGetValue(T item, Int32& index)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.FullMetadataWriter.GetOrAddMethodSpecificationHandle(IGenericMethodInstanceReference reference)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.GetMethodHandle(IMethodReference methodReference)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.GetHandle(Object reference)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.ResolveEntityHandleFromPseudoToken(Int32 pseudoSymbolToken)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.WriteInstructions(Blob finalIL, ImmutableArray`1 generatedIL, UserStringHandle& mvidStringHandle, Blob& mvidStringFixup)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.SerializeMethodBody(MethodBodyStreamEncoder encoder, IMethodBody methodBody, StandaloneSignatureHandle localSignatureHandleOpt, UserStringHandle& mvidStringHandle, Blob& mvidStringFixup)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.SerializeMethodBodies(BlobBuilder ilBuilder, PdbWriter nativePdbWriterOpt, Blob& mvidStringFixup)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.MetadataWriter.BuildMetadataAndIL(PdbWriter nativePdbWriterOpt, BlobBuilder ilBuilder, BlobBuilder mappedFieldDataBuilder, BlobBuilder managedResourceDataBuilder, Blob& mvidFixup, Blob& mvidStringFixup)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.Cci.PeWriter.WritePeToStream(EmitContext context, CommonMessageProvider messageProvider, Func`1 getPeStream, Func`1 getPortablePdbStreamOpt, PdbWriter nativePdbWriterOpt, String pdbPathOpt, Boolean metadataOnly, Boolean isDeterministic, Boolean emitTestCoverageData, Nullable`1 privateKeyOpt, CancellationToken cancellationToken)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.Compilation.SerializePeToStream(CommonPEModuleBuilder moduleBeingBuilt, DiagnosticBag metadataDiagnostics, CommonMessageProvider messageProvider, Func`1 getPeStream, Func`1 getMetadataPeStreamOpt, Func`1 getPortablePdbStreamOpt, PdbWriter nativePdbWriterOpt, String pdbPathOpt, RebuildData rebuildData, Boolean metadataOnly, Boolean includePrivateMembers, Boolean isDeterministic, Boolean emitTestCoverageData, Nullable`1 privateKeyOpt, CancellationToken cancellationToken)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.Compilation.SerializeToPeStream(CommonPEModuleBuilder moduleBeingBuilt, EmitStreamProvider peStreamProvider, EmitStreamProvider metadataPEStreamProvider, EmitStreamProvider pdbStreamProvider, RebuildData rebuildData, Func`2 testSymWriterFactory, DiagnosticBag diagnostics, EmitOptions emitOptions, Nullable`1 privateKeyOpt, CancellationToken cancellationToken)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CommonCompiler.CompileAndEmit(TouchedFileLogger touchedFilesLogger, Compilation& compilation, ImmutableArray`1 analyzers, ImmutableArray`1 generators, ImmutableArray`1 additionalTextFiles, AnalyzerConfigSet analyzerConfigSet, ImmutableArray`1 sourceFileAnalyzerConfigOptions, ImmutableArray`1 embeddedTexts, DiagnosticBag diagnostics, ErrorLogger errorLogger, CancellationToken cancellationToken, CancellationTokenSource& analyzerCts, AnalyzerDriver& analyzerDriver, Nullable`1& generatorTimingInfo)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CommonCompiler.RunCore(TextWriter consoleOutput, ErrorLogger errorLogger, CancellationToken cancellationToken)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CommonCompiler.Run(TextWriter consoleOutput, CancellationToken cancellationToken)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CSharp.CommandLine.Csc.<>c__DisplayClass1_0.<Run>b__0(TextWriter tw)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CommandLine.ConsoleUtil.RunWithUtf8Output[T](Func`2 func)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CSharp.CommandLine.Csc.Run(String[] args, BuildPaths buildPaths, TextWriter textWriter, IAnalyzerAssemblyLoader analyzerLoader)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CommandLine.BuildClient.RunLocalCompilation(String[] arguments, BuildPaths buildPaths, TextWriter textWriter)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CommandLine.BuildClient.RunCompilation(IEnumerable`1 originalArguments, BuildPaths buildPaths, TextWriter textWriter, String pipeName)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CommandLine.BuildClient.Run(IEnumerable`1 arguments, RequestLanguage language, CompileFunc compileFunc, CompileOnServerFunc compileOnServerFunc, ICompilerServerLogger logger)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.MainCore(String[] args)
16:18:31:767	2>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CSharp.Core.targets(84,5): error :    at Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(String[] args)
16:18:34:793	2>Done building project "MyProjectName.csproj" -- FAILED.

VS Error List also goes crazy and list each exception line as an error:

image

If I run dotnet build from the CLI instead of VS, at least I get the detailed info of which causes the problem:

C:\Program Files\dotnet\sdk\8.0.200\Roslyn\Microsoft.CSharp.Core.targets(84,5): error : Unhandled exception. System.Col
lections.Generic.KeyNotFoundException: The given key '<ProjectName>.SetupOptions.AddPlatformDailyRewards<T>(Microsoft.Maui.Hosting.MauiAppBuilder)' was not present in the dictionary.

By adding an empty implementation to all files (iOS/Windows etc), the error disappears.

Diagnostic Id: N/A

Expected Behavior: The build should be successful as the method is optional. At least it should tell me where the problem is.

Actual Behavior: Exception is thrown by CSC.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 7, 2024
@jaredpar
Copy link
Member

jaredpar commented Mar 8, 2024

Unfortunately I'm not able to reproduce this issue. Do you have a smaller sample project where this repros that we could look at?

@datvm
Copy link
Author

datvm commented Mar 8, 2024

@jaredpar unfortunately not. I tried recreating a new project but the issue didn't happen. I was hoping you could find out what causes it from the stack trace. The project is private so if you give me a private channel I can send it over to you.

@jaredpar
Copy link
Member

jaredpar commented Mar 8, 2024

@datvm can you file a VS feedback bug? That will give us a channel where we can share private files. Choose "Report a Problem" from this menu in Visual Studio

image

@jaredpar
Copy link
Member

jaredpar commented Apr 1, 2024

Closing as we haven't heard back on this. If you're able to submit a VS Feedback bug or have more insights into what is causing the issue we will re-open.

@jaredpar jaredpar closed this as completed Apr 1, 2024
@datvm
Copy link
Author

datvm commented Apr 3, 2024

@jaredpar Hello, sorry about the long delay. I am just back from a long vacation. I have submitted the feedback and attached the problematic project here: https://developercommunity.visualstudio.com/t/KeyNotFoundException-thrown-when-compili/10630851

@jaredpar
Copy link
Member

jaredpar commented Apr 3, 2024

Thanks for uploading the repro. I've assigned out the VS feedback bug for investigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants