Skip to content

fix(source-gen): correct source location for cross-project inherited tests#6133

Merged
thomhurst merged 1 commit into
thomhurst:mainfrom
slang25:slang25/inherited-test-source-location
May 31, 2026
Merged

fix(source-gen): correct source location for cross-project inherited tests#6133
thomhurst merged 1 commit into
thomhurst:mainfrom
slang25:slang25/inherited-test-source-location

Conversation

@slang25
Copy link
Copy Markdown
Contributor

@slang25 slang25 commented May 31, 2026

Problem

For a test inherited via [InheritsTests] from an abstract base class in a referenced assembly (a separate project), the generated source location pairs the base file path with the derived class's line range.

Concretely, a derived SqsChangeMessageVisibilityAsyncVerificationTests : SqsChangeMessageVisibilityAsyncTests reports every test method at SqsChangeMessageVisibilityAsyncTests.cs (base, correct) but lines 3–19 — which is the derived class's declaration span, not the method. Tooling consuming the test node location (e.g. the HTML report's Source tab) then opens the base file at unrelated lines.

Cause

In TestMetadataGenerator.GetTestMethodSourceLocation (inherited overload), when the base method symbol has no source location in the current compilation (method.Locations is in metadata, because the base class is in a referenced assembly), the fallback used classInfo.ClassSyntax.GetLineSpan() (the derived class span) while keeping attrFilePath (the [Test] attribute's CallerFilePath → the base file). The two don't correspond.

Fix

The [Test] attribute's CallerFilePath/CallerLineNumber are baked at the base method declaration and remain the correct pointer, so fall back to those instead of the derived class's syntax span. Same-compilation inheritance is unaffected — the base method is in source and its own span is still used (verified: existing InheritsTests* snapshot tests pass unchanged).

Test coverage gap

The existing SG test harness compiles base + derived in a single compilation (AdditionalFiles), so the base method is always in-source and can't reproduce the metadata-symbol path. A genuine regression test needs the harness extended to add a pre-compiled assembly reference. Flagging as a follow-up.

Regression range

This is a regression introduced between 1.45.8 and 1.47.0. Verified against the real project that reported it (LocalSqsSnsMessaging, cross-project [InheritsTests]):

  • 1.45.8 emits the correct LineNumber (the base method's [Test] line).
  • 1.47.0 emits the derived class's declaration span instead (e.g. LineNumber = 5, EndLineNumber = 17 for a method actually at line 90).
  • 1.47.0 + this fix restores the correct LineNumber = 90, and other inherited methods resolve to their real base-file lines.

🤖 Generated with Claude Code

…tests

When a test is inherited (via [InheritsTests]) from an abstract base class
in a *referenced assembly*, the base method symbol has no source location in
the current compilation (method.Locations is not in source). The fallback
then took the *derived class's* syntax line span but paired it with the base
file path (from the [Test] attribute's CallerFilePath), producing an
inconsistent location: the base file shown at the derived class's line range.
The Source view of the HTML report (and any IDE/tooling consuming the test
node location) jumped to unrelated lines in the base file.

The [Test] attribute's CallerFilePath/CallerLineNumber are baked at the base
method declaration and remain the correct pointer, so fall back to those
instead of the derived class's syntax span. Same-compilation inheritance is
unaffected (the base method is in source and its own span is used).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@slang25 slang25 temporarily deployed to Pull Requests May 31, 2026 13:27 — with GitHub Actions Inactive
@slang25 slang25 temporarily deployed to Pull Requests May 31, 2026 13:27 — with GitHub Actions Inactive
@slang25 slang25 temporarily deployed to Pull Requests May 31, 2026 13:27 — with GitHub Actions Inactive
@thomhurst thomhurst enabled auto-merge (squash) May 31, 2026 13:27
@thomhurst
Copy link
Copy Markdown
Owner

Thanks! 😄

@thomhurst thomhurst merged commit 14c400a into thomhurst:main May 31, 2026
9 of 10 checks passed
@claude claude Bot mentioned this pull request May 31, 2026
1 task
This was referenced Jun 1, 2026
This was referenced Jun 3, 2026
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.

2 participants