Skip to content

Fix unhandled exception when running invalid type-based benchmark with arguments - #2880

Merged
timcassell merged 3 commits into
dotnet:masterfrom
Shrinidhi203:fix-issue-2724
Nov 29, 2025
Merged

Fix unhandled exception when running invalid type-based benchmark with arguments#2880
timcassell merged 3 commits into
dotnet:masterfrom
Shrinidhi203:fix-issue-2724

Conversation

@Shrinidhi203

Copy link
Copy Markdown
Contributor

Fix #2724

Problem

When running 'BenchmarkRunner.Run()' or 'BenchmarkRunner.Run(Type)' with arguments on an invalid benchmark type (no [Benchmark] methods), the runner threw an unhandled 'InvalidOperationException: Sequence contains no elements' instead of returning a validation error.

Solution

  • Replace '.Single()' with '.SingleOrDefault()' in 'RunWithDirtyAssemblyResolveHelper'
  • Return 'Summary.ValidationFailed' with a descriptive message when no benchmarks are found

Changes

  • 'src/BenchmarkDotNet/Running/BenchmarkRunnerDirty.cs' - Fix the exception handling
  • 'tests/BenchmarkDotNet.Tests/Running/RunningEmptyBenchmarkTests.cs' - Enable and update tests

…th arguments

  Fixes dotnet#2724

  When running BenchmarkRunner.Run<T>() or BenchmarkRunner.Run(Type) with
  arguments on an invalid benchmark type (no [Benchmark] methods), the runner
  threw an unhandled InvalidOperationException instead of returning a validation error.

  Changes:
  - Replace .Single() with .SingleOrDefault() in RunWithDirtyAssemblyResolveHelper
  - Return Summary.ValidationFailed when no benchmarks are found
  - Enable and update tests for args scenarios in RunningEmptyBenchmarkTests.cs"
@Shrinidhi203

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@Shrinidhi203

Copy link
Copy Markdown
Contributor Author

There were StyleCop errors , thats also fixed.

Comment thread tests/BenchmarkDotNet.Tests/Running/RunningEmptyBenchmarkTests.cs
Comment thread tests/BenchmarkDotNet.Tests/Running/RunningEmptyBenchmarkTests.cs
@Shrinidhi203

Copy link
Copy Markdown
Contributor Author

Have asserted the failure.

@timcassell timcassell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @Shrinidhi203

@timcassell
timcassell merged commit dde78c5 into dotnet:master Nov 29, 2025
9 checks passed
@timcassell timcassell added this to the v0.15.8 milestone Nov 29, 2025
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.

Unhandled Exception Thrown When Running an invalid Type-Based Benchmark with Arguments

2 participants