stryker-mutator / stryker-net

Mutation testing for .NET core and .NET framework!
https://stryker-mutator.io
Apache License 2.0
1.77k stars 183 forks source link

Stryker throwing ArgumentException (key conflict) after running tests in a project #2827

Closed mstoicmicrosoft closed 8 months ago

mstoicmicrosoft commented 8 months ago

Describe the bug /_/src/Stryker.CLI/Stryker.CLI/Program.cs:line 14 is throwing an Unhandled exception. System.ArgumentException: An element with the same key but a different value already exists. Key: 'CS8002' after running several tests in projects.

Logs Console output error:

Unhandled exception. System.ArgumentException: An element with the same key but a different value already exists. Key: 'CS8002'
   at System.Collections.Immutable.ImmutableDictionary`2.HashBucket.Add(TKey key, TValue value, IEqualityComparer`1 keyOnlyComparer, IEqualityComparer`1 valueComparer, KeyCollisionBehavior behavior, OperationResult& result)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 items, MutationInput origin, KeyCollisionBehavior collisionBehavior)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs, Boolean avoidToHashMap)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs)
   at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TKey,TValue](IEnumerable`1 source, IEqualityComparer`1 keyComparer, IEqualityComparer`1 valueComparer)
   at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TKey,TValue](IEnumerable`1 source)
   at Stryker.Core.Initialisation.Buildalyzer.IAnalyzerResultExtensions.GetDiagnosticOptions(IAnalyzerResult analyzerResult) in /_/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultExtensions.cs:line 187
   at Stryker.Core.Initialisation.Buildalyzer.IAnalyzerResultCSharpExtensions.GetCompilationOptions(IAnalyzerResult analyzerResult) in /_/src/Stryker.Core/Stryker.Core/Initialisation/Buildalyzer/IAnalyzerResultCSharpExtensions.cs:line 13
   at Stryker.Core.Compiling.CsharpCompilingProcess.GetCSharpCompilation(IEnumerable`1 syntaxTrees) in /_/src/Stryker.Core/Stryker.Core/Compiling/CsharpCompilingProcess.cs:line 136
   at Stryker.Core.Compiling.CsharpCompilingProcess.GetSemanticModels(IEnumerable`1 syntaxTrees) in /_/src/Stryker.Core/Stryker.Core/Compiling/CsharpCompilingProcess.cs:line 102
   at Stryker.Core.MutationTest.CsharpMutationProcess.Mutate(MutationTestInput input) in /_/src/Stryker.Core/Stryker.Core/MutationTest/CsharpMutationProcess.cs:line 59
   at Stryker.Core.MutationTest.MutationTestProcess.Mutate() in /_/src/Stryker.Core/Stryker.Core/MutationTest/MutationTestProcess.cs:line 88
   at Stryker.Core.Initialisation.ProjectMutator.MutateProject(StrykerOptions options, MutationTestInput input, IReporter reporters) in /_/src/Stryker.Core/Stryker.Core/Initialisation/ProjectMutator.cs:line 38
   at Stryker.Core.Initialisation.ProjectOrchestrator.MutateProjects(StrykerOptions options, IReporter reporters, ITestRunner runner)+MoveNext() in /_/src/Stryker.Core/Stryker.Core/Initialisation/ProjectOrchestrator.cs:line 68
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Stryker.Core.StrykerRunner.RunMutationTest(IStrykerInputs inputs, ILoggerFactory loggerFactory, IProjectOrchestrator projectOrchestrator) in /_/src/Stryker.Core/Stryker.Core/StrykerRunner.cs:line 61
   at Stryker.CLI.StrykerCli.RunStryker(IStrykerInputs inputs) in /_/src/Stryker.CLI/Stryker.CLI/StrykerCLI.cs:line 102
   at Stryker.CLI.StrykerCli.<>c__DisplayClass11_0.<Run>b__0() in /_/src/Stryker.CLI/Stryker.CLI/StrykerCLI.cs:line 74
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.<>c__DisplayClass143_0.<OnExecute>b__0(CancellationToken _)
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.ExecuteAsync(String[] args, CancellationToken cancellationToken)
   at McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Stryker.CLI.StrykerCli.Run(String[] args) in /_/src/Stryker.CLI/Stryker.CLI/StrykerCLI.cs:line 80
   at Stryker.CLI.Program.Main(String[] args) in /_/src/Stryker.CLI/Stryker.CLI/Program.cs:line 14

For us to best assist you in diagnosing the problem we would ask you to run stryker with the option --log-to-file or -L and to attach the resulting logs to the issue.

Expected behavior I expected Stryker to finish the analysis and show me the report. I was not expecting an unhandled exception.

Desktop (please complete the following information):

Additional context

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");

        Dictionary<string, string> x = new Dictionary<string, string>() { { "a", "a" }, { "b", "b" }, { "c", "c" } };
        Dictionary<string, string> y = new Dictionary<string, string>() { { "a", "a2" }, { "2", "b" }, { "3", "c" } };
        var w = x.Concat(y);
        Console.WriteLine(w.GetType() + "w = " + String.Join(" ", w));
        var wi = w.ToImmutableDictionary();
        Console.WriteLine(wi.GetType() + "wi = " + String.Join(" ", wi));
    }
}
Hello World
System.Linq.Enumerable+Concat2Iterator`1[System.Collections.Generic.KeyValuePair`2[System.String,System.String]]w = [a, a] [b, b] [c, c] [a, a2] [2, b] [3, c]
Unhandled exception. System.ArgumentException: An element with the same key but a different value already exists. Key: 'a'
   at System.Collections.Immutable.ImmutableDictionary`2.HashBucket.Add(TKey key, TValue value, IEqualityComparer`1 keyOnlyComparer, IEqualityComparer`1 valueComparer, KeyCollisionBehavior behavior, OperationResult& result)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 items, MutationInput origin, KeyCollisionBehavior collisionBehavior)
   at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs, Boolean avoidToHashMap)
   at Program.Main()
Command terminated by signal 6

When I run msbuild on my sln file I see 0 warnings and 0 errors but if I scroll up there is a section with this line but I don't know what it means: /warnaserror+:CS8002,NU1605 /warnaserror-:CA2229,NU5100,CS8002

dupdob commented 8 months ago

thanks for reporting this. This is indeed a bug, of rare occurrence, but it should be easy to fix. To answer your question about CS8002 and strong naming: Stryker does not have any requirements on strong naming. It should work anyway (with or without it). It appears you have conflicting settings in your project: CS8002 is flagged as to be treated as an error ( the /warnaserror+:CS8002,NU1605 part), but also as simple warnings (the /warnaserror-:CA2229,NU5100,CS8002 part).

Stryker tries to respect project settings to better match the desired output, but does not properly cover duplicate/conflicting settings such as this. A fix will be provided, but the fastest way to fix this, is to find in your settings where CS8002 is listed as to be treated as an error and remove it. Then Stryker should run smoothly

mstoicmicrosoft commented 8 months ago

Thank you for the super quick fix. And the workaround worked - I found the settings where CS8002 was marked as an error so I am unblocked.