Skip to content

Verify that a key is in the warningsAsErrors dictionary before using it#6269

Merged
benvillalobos merged 1 commit intodotnet:mainfrom
Forgind:missing-key-fix
Mar 16, 2021
Merged

Verify that a key is in the warningsAsErrors dictionary before using it#6269
benvillalobos merged 1 commit intodotnet:mainfrom
Forgind:missing-key-fix

Conversation

@Forgind
Copy link
Copy Markdown
Contributor

@Forgind Forgind commented Mar 16, 2021

Customers reported issues like:

Error The "ResolveAssemblyReference" task failed unexpectedly.
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Concurrent.ConcurrentDictionary`2.get_Item(TKey key)
   at Microsoft.Build.BackEnd.Logging.LoggingService.GetWarningsToBeLoggedAsErrorsByProject(BuildEventContext context)
   at Microsoft.Build.BackEnd.Logging.TaskLoggingContext.GetWarningsAsErrors()
   at Microsoft.Build.BackEnd.TaskHost.get_WarningsAsErrors()
   at Microsoft.Build.BackEnd.TaskHost.ShouldTreatWarningAsError(String warningCode)
   at Microsoft.Build.Utilities.TaskLoggingHelper.LogWarning(String subcategory, String warningCode, String helpKeyword, String helpLink, String file, Int32 lineNumber, Int32 columnNumber, Int32 endLineNumber, Int32 endColumnNumber, String message, Object[] messageArgs)
   at Microsoft.Build.Utilities.TaskLoggingHelper.LogWarningWithCodeFromResources(String messageResourceName, Object[] messageArgs)
   at Microsoft.Build.Tasks.ReferenceTable.LogHigherVersionUnresolveDueToAttribute(Boolean displayPrimaryReferenceMessage, AssemblyNameExtension assemblyName, Reference reference, ITaskItem referenceItem, String targetedFramework)
   at Microsoft.Build.Tasks.ReferenceTable.RemovePrimaryReferenceMarkedForExclusion(LogExclusionReason logExclusionReason, Boolean removeOnlyNoWarning, String subsetName, List`1 removedReferences, AssemblyNameExtension assemblyName, Reference assemblyReference)
   at Microsoft.Build.Tasks.ReferenceTable.RemoveReferencesMarkedForExclusion(Boolean removeOnlyNoWarning, String subsetName)
   at Microsoft.Build.Tasks.ResolveAssemblyReference.Execute(FileExists fileExists, DirectoryExists directoryExists, GetDirectories getDirectories, GetAssemblyName getAssemblyName, GetAssemblyMetadata getAssemblyMetadata, GetRegistrySubKeyNames getRegistrySubKeyNames, GetRegistrySubKeyDefaultValue getRegistrySubKeyDefaultValue, GetLastWriteTime getLastWriteTime, GetAssemblyRuntimeVersion getRuntimeVersion, OpenBaseKey openBaseKey, GetAssemblyPathInGac getAssemblyPathInGac, IsWinMDFile isWinMDFile, ReadMachineTypeFromPEHeader readMachineTypeFromPEHeader)
   at Microsoft.Build.Tasks.ResolveAssemblyReference.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() NetFxOldStyle.Test

Looking at it, we access a dictionary here without verifying that the key (project ID) is in the dictionary. This fixes that and defaults to "no warnings as errors" if relevant.

Currently untested.

Copy link
Copy Markdown
Member

@benvillalobos benvillalobos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick patch.

int key = GetWarningsAsErrorOrMessageKey(context);

HashSet<string> warningsAsErrorsExcludingMessages = new HashSet<string>(_warningsAsErrorsByProject[key]);
if (!_warningsAsErrorsByProject.TryGetValue(key, out ISet<string> warningsAsErrorsExcludingMessages))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest checking !_warningsAsErrorsByProject.ContainsKey(key), but containskey actually calls trygetvalue.

@benvillalobos
Copy link
Copy Markdown
Member

Related issue: dotnet/sdk#16350

@benvillalobos benvillalobos linked an issue Mar 16, 2021 that may be closed by this pull request
@benvillalobos benvillalobos merged commit 1767736 into dotnet:main Mar 16, 2021
@Forgind Forgind deleted the missing-key-fix branch March 18, 2021 17:56
@jonathanpeppers
Copy link
Copy Markdown
Member

@Forgind @benvillalobos should this fix be in 6.0.100-preview.3.21180.3?

We still hit this here: dotnet/maui-samples#111

Log: Release.zip

@Forgind
Copy link
Copy Markdown
Contributor Author

Forgind commented Mar 31, 2021

@rokonec pointed out that there were two places that could have had the problem, and that fix happened to be the right one for the person who originally reported the problem. I suspect your problem would be resolved by #6304, but @benvillalobos suggested we should wait and merge #6308 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyNotFoundException in GetWarningsToBeLoggedAsErrorsByProject

4 participants