{"record":{"id":"d4a1b3d49f29fdf5","repo":"dotnet/BenchmarkDotNet","slug":"the-variable-key-environment-variables-is-defi","errorCode":null,"errorMessage":"The '{variable.Key}' environment variables is defined twice","messagePattern":"The '(.+?)' environment variables is defined twice","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Jobs/JobExtensions.cs","lineNumber":231,"sourceCode":"            job.WithCore(j => j.Infrastructure.BuildConfiguration = buildConfiguration);\n\n        /// <summary>\n        /// Creates a new job based on the given job with specified environment variables.\n        /// It overrides the whole list of environment variables which were defined in the original job.\n        /// </summary>\n        /// <param name=\"job\">The original job</param>\n        /// <param name=\"environmentVariables\">The environment variables for the new job</param>\n        /// <exception cref=\"InvalidOperationException\">\n        /// Throws an exception if <paramref name=\"environmentVariables\"/> contains two variables with the same key.\n        /// </exception>\n        /// <returns>The new job with overriden environment variables</returns>\n        public static Job WithEnvironmentVariables(this Job job, params EnvironmentVariable[] environmentVariables)\n        {\n            var keys = new HashSet<string>();\n            foreach (var variable in environmentVariables)\n            {\n                if (keys.Contains(variable.Key))\n                    throw new InvalidOperationException($\"The '{variable.Key}' environment variables is defined twice\");\n                keys.Add(variable.Key);\n            }\n            return job.WithCore(j => j.Environment.EnvironmentVariables = environmentVariables);\n        }\n\n        /// <summary>\n        /// Creates a new job based on the given job with additional environment variable.\n        /// All existed environment variables of the original job will be copied to the new one.\n        /// If the original job already contains an environment variable with the same key, it will be overriden.\n        /// </summary>\n        /// <param name=\"job\">The original job</param>\n        /// <param name=\"environmentVariable\">The new environment variable which should be added for the new job</param>\n        /// <returns>The new job with additional environment variable</returns>\n        public static Job WithEnvironmentVariable(this Job job, EnvironmentVariable environmentVariable)\n            => job.WithCore(j => j.Environment.SetEnvironmentVariable(environmentVariable));\n\n        /// <summary>\n        /// Creates a new job based on the given job with additional environment variable.","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Jobs/JobExtensions.cs#L213-L249","documentation":"Thrown by WithEnvironmentVariables when the provided EnvironmentVariable[] contains two entries with the same Key. The method iterates the array, tracks keys in a HashSet, and throws InvalidOperationException on the first duplicate. This prevents ambiguous environment variable definitions where the host process would only honor one of the conflicting values.","triggerScenarios":"Calling job.WithEnvironmentVariables(var1, var2) where var1.Key == var2.Key. The HashSet.Contains(variable.Key) check catches the second occurrence.","commonSituations":"Merging environment variable lists from multiple sources (e.g., base config + overrides) without deduplication. Building variables in a loop or from a dictionary where duplicate keys slip in. Configuring the same variable name twice by copy-paste.","solutions":["Deduplicate by key before calling, keeping the last occurrence: envVars.GroupBy(v => v.Key).Select(g => g.Last()).ToArray()","Use WithEnvironmentVariable (singular) which overrides existing keys, instead of WithEnvironmentVariables (plural) which replaces all.","Audit the source dictionary/list for duplicate keys before constructing EnvironmentVariable[]."],"exampleFix":"// before\nvar vars = new[] {\n    new EnvironmentVariable(\"FOO\", \"1\"),\n    new EnvironmentVariable(\"FOO\", \"2\"),\n};\njob = job.WithEnvironmentVariables(vars);\n\n// after\nvar vars = new[] {\n    new EnvironmentVariable(\"FOO\", \"1\"),\n    new EnvironmentVariable(\"FOO\", \"2\"),\n}.GroupBy(v => v.Key)\n .Select(g => g.Last())\n .ToArray();\njob = job.WithEnvironmentVariables(vars);","handlingStrategy":"validation","validationCode":"var deduped = environmentVariables\n    .GroupBy(v => v.Key)\n    .Select(g => g.Last()) // keep last occurrence (override semantics)\n    .ToArray();\njob = job.WithEnvironmentVariables(deduped);","typeGuard":"static bool HasUniqueKeys(EnvironmentVariable[] vars) =>\n    vars.Select(v => v.Key).Distinct().Count() == vars.Length;","tryCatchPattern":null,"preventionTips":["Use WithEnvironmentVariable (singular) for incremental adds — it handles override semantics.","Deduplicate environment variable lists whenever merging from multiple sources.","Build from a Dictionary<string, string> which inherently prevents duplicate keys."],"tags":["argument-validation","duplicate-key","environment-variables","invalid-operation"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}