Skip to content

Add [StackTraceHidden] to assertion API internals#8853

Merged
Evangelink merged 3 commits into
microsoft:mainfrom
Evangelink:dev/amauryleve/stack-trace-hidden-assertions
Jun 5, 2026
Merged

Add [StackTraceHidden] to assertion API internals#8853
Evangelink merged 3 commits into
microsoft:mainfrom
Evangelink:dev/amauryleve/stack-trace-hidden-assertions

Conversation

@Evangelink
Copy link
Copy Markdown
Member

Addresses feedback from @Youssef1313 on #8826 (#8826 (comment)):

Why not use StackTraceHidden instead of trying to alter stack trace manually.

This PR adds [StackTraceHidden] to nested helper types inside the assertion API that previously could leak into user-facing failure stack traces:

  • NonGenericEqualityComparerAdapter (Assert.AreAllDistinct)
  • TypeMismatch (Assert.AreAllOfType)
  • EquivalenceComparer partial (Assert.AreEquivalent.Comparer and sibling partials)
  • DictionaryView, DictionaryLookup, NonGenericDictionaryView, GenericDictionaryView, GenericDictionaryAccessors (Assert.AreEquivalent.DictionaryView)
  • ReferenceObjectComparer, MemberLookup, MemberAccessor (Assert.AreEquivalent.MemberAccessor)
  • EquivalenceMismatch (Assert.AreEquivalent.Mismatch)
  • ThrowsExceptionState (Assert.ThrowsException)
  • ObjectComparer (CollectionAssert.Helpers)

Top-level assertion types (Assert, CollectionAssert, StringAssert, AssertExtensions, AssertScope, etc.) and all InterpolatedStringHandler structs were already annotated, so this PR fills in the remaining nested helpers within src/TestFramework/TestFramework/Assertions/.

Enum-typed helpers (IEquatableOutcome, ThrowsFailureKind) are skipped because [StackTraceHidden] does not target enums.

Notes / out of scope

  • MTP / adapter plumbing frames that may also appear in user failure traces are out of scope for this PR.
  • Compiler-generated iterator/lambda state-machine frames inside annotated types may still appear in some cases — that is a known runtime limitation of [StackTraceHidden] and not addressed here.

Addresses feedback from @Youssef1313 on microsoft#8826: use [StackTraceHidden] instead of manually filtering stack frames.

Applies the attribute to nested helper types inside the assertion API that previously could leak into user-facing failure stack traces:

- NonGenericEqualityComparerAdapter (Assert.AreAllDistinct)
- TypeMismatch (Assert.AreAllOfType)
- EquivalenceComparer partial (Assert.AreEquivalent.Comparer)
- DictionaryView, DictionaryLookup, NonGenericDictionaryView, GenericDictionaryView, GenericDictionaryAccessors (Assert.AreEquivalent.DictionaryView)
- ReferenceObjectComparer, MemberLookup, MemberAccessor (Assert.AreEquivalent.MemberAccessor)
- EquivalenceMismatch (Assert.AreEquivalent.Mismatch)
- ThrowsExceptionState (Assert.ThrowsException)
- ObjectComparer (CollectionAssert.Helpers)

Enum-typed helpers (IEquatableOutcome, ThrowsFailureKind) are skipped because [StackTraceHidden] does not target enums.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 5, 2026 09:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends [StackTraceHidden] coverage within the MSTest assertion implementation (src/TestFramework/TestFramework/Assertions/*) by annotating additional nested/internal helper types that can otherwise appear in user-facing failure stack traces.

Changes:

  • Add [StackTraceHidden] to additional nested assertion helper types in Assert.AreEquivalent.* (comparer, mismatch, member accessor, dictionary view).
  • Add [StackTraceHidden] to helper types used by Assert.AreAllDistinct, Assert.AreAllOfType, and Assert.ThrowsException.
  • Add [StackTraceHidden] to CollectionAssert’s internal ObjectComparer.
Show a summary per file
File Description
src/TestFramework/TestFramework/Assertions/CollectionAssert.Helpers.cs Hides ObjectComparer helper frames from stack traces.
src/TestFramework/TestFramework/Assertions/Assert.ThrowsException.cs Hides ThrowsExceptionState helper frames from stack traces.
src/TestFramework/TestFramework/Assertions/Assert.AreEquivalent.Mismatch.cs Hides EquivalenceMismatch helper frames from stack traces.
src/TestFramework/TestFramework/Assertions/Assert.AreEquivalent.MemberAccessor.cs Hides member-access and reference-compare helper frames from stack traces.
src/TestFramework/TestFramework/Assertions/Assert.AreEquivalent.DictionaryView.cs Hides dictionary abstraction/lookup/accessor helper frames from stack traces.
src/TestFramework/TestFramework/Assertions/Assert.AreEquivalent.Comparer.cs Hides EquivalenceComparer internal comparer frames from stack traces.
src/TestFramework/TestFramework/Assertions/Assert.AreAllOfType.cs Hides TypeMismatch helper frames from stack traces.
src/TestFramework/TestFramework/Assertions/Assert.AreAllDistinct.cs Hides comparer adapter frames from stack traces.

Copilot's findings

  • Files reviewed: 8/8 changed files
  • Comments generated: 2

Comment thread src/TestFramework/TestFramework/Assertions/Assert.AreAllDistinct.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 5, 2026 09:19
@Evangelink Evangelink enabled auto-merge (squash) June 5, 2026 09:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 8/8 changed files
  • Comments generated: 1

Comment thread src/TestFramework/TestFramework/Assertions/Assert.AreAllDistinct.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot's findings

  • Files reviewed: 8/8 changed files
  • Comments generated: 1

Comment thread src/TestFramework/TestFramework/Assertions/Assert.AreAllDistinct.cs
@Evangelink Evangelink merged commit 7a5b967 into microsoft:main Jun 5, 2026
31 checks passed
@Evangelink Evangelink deleted the dev/amauryleve/stack-trace-hidden-assertions branch June 5, 2026 11:49
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.

3 participants