Skip to content

Commit

Permalink
Don't use Dictionary<K,V> to avoid duplicate value exception (dotnet#…
Browse files Browse the repository at this point in the history
…7340)

Fixes: dotnet#7302
Context: dotnet@7117414

When building .NET6 app on .NET7, sometimes the build fails with:

    C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\32.0.451\tools\Xamarin.Android.Common.targets(1438,3): error XAGJS7004: System.ArgumentException: An item with the same key has already been added.
     at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
     at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
     at Xamarin.Android.Tasks.TypeMapGenerator.GenerateRelease(Boolean skipJniAddNativeMethodRegistrationAttributeScan, List`1 javaTypes, String outputDirectory, ApplicationConfigTaskState appConfState)
     at Xamarin.Android.Tasks.GenerateJavaStubs.WriteTypeMappings(List`1 types, TypeDefinitionCache cache)
     at Xamarin.Android.Tasks.GenerateJavaStubs.Run(DirectoryAssemblyResolver res)
     at Xamarin.Android.Tasks.GenerateJavaStubs.RunTask()
     at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in /Users/runner/work/1/s/xamarin-android/external/xamarin-android-tools/src/Microsoft.Android.Build.BaseTasks/AndroidTask.cs:line 17

Avoid that by switching the data structure to `List<T>`, since we no
longer use the Dictionary key for anything.
  • Loading branch information
grendello authored and jonathanpeppers committed Sep 6, 2022
1 parent d6224ca commit 7bcc88f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Utilities/TypeMapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal sealed class ModuleReleaseData
public byte[] MvidBytes;
public AssemblyDefinition Assembly;
public TypeMapReleaseEntry[] Types;
public Dictionary<uint, TypeMapReleaseEntry> DuplicateTypes;
public List<TypeMapReleaseEntry> DuplicateTypes;
public string AssemblyName;
public string AssemblyNameLabel;
public string OutputFilePath;
Expand Down Expand Up @@ -382,7 +382,7 @@ bool GenerateRelease (bool skipJniAddNativeMethodRegistrationAttributeScan, List
Assembly = td.Module.Assembly,
AssemblyName = td.Module.Assembly.Name.Name,
TypesScratch = new Dictionary<string, TypeMapReleaseEntry> (StringComparer.Ordinal),
DuplicateTypes = new Dictionary<uint, TypeMapReleaseEntry> (),
DuplicateTypes = new List<TypeMapReleaseEntry> (),
};
tempModules.Add (moduleUUID, moduleData);
}
Expand Down Expand Up @@ -410,7 +410,7 @@ bool GenerateRelease (bool skipJniAddNativeMethodRegistrationAttributeScan, List
// build) and has no value for the end user. The message is left here because it may be useful to us
// in our devloop at some point.
//logger ($"Warning: duplicate Java type name '{entry.JavaName}' in assembly '{moduleData.AssemblyName}' (new token: {entry.Token}).");
moduleData.DuplicateTypes.Add (entry.Token, entry);
moduleData.DuplicateTypes.Add (entry);
} else
moduleData.TypesScratch.Add (entry.JavaName, entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void WriteMapModules (NativeAssemblyGenerator generator, StreamWriter mapOutput,
if (mapOutput != null) {
WriteManagedMaps (generator, mapOutput, mapName, data.Types);
if (data.DuplicateTypes.Count > 0) {
WriteManagedMaps (generator, mapOutput, duplicateMapName, data.DuplicateTypes.Values);
WriteManagedMaps (generator, mapOutput, duplicateMapName, data.DuplicateTypes);
}
}
}
Expand Down

0 comments on commit 7bcc88f

Please sign in to comment.