Skip to content

Fix inline snapshot corruption with multiple snapshots in with_settings!#858

Merged
max-sixty merged 1 commit intomitsuhiko:masterfrom
max-sixty:857
Jan 8, 2026
Merged

Fix inline snapshot corruption with multiple snapshots in with_settings!#858
max-sixty merged 1 commit intomitsuhiko:masterfrom
max-sixty:857

Conversation

@max-sixty
Copy link
Copy Markdown
Collaborator

Summary

Fixes #857

When find_snapshot_macro(line) was called for a line inside a macro like with_settings!, the scan_nested_macros function would scan all nested tokens and find all @"..." patterns. Since try_extract_snapshot always overwrote the result, the last snapshot found would win regardless of which line we were searching for.

This caused:

  1. All searches for lines inside with_settings! returned the same (last) snapshot position
  2. The duplicate detection logic discarded all but the first pending snapshot
  3. The first pending snapshot's content got written to the last snapshot's position

Example from the issue:

insta::with_settings!({filters => vec![...]}, {
    assert_snapshot!(":12345\n\nabc", @"");  // line 10
    assert_snapshot!(":12345\n\nabc", @"");  // line 11
    assert_snapshot!("", @"");               // line 12
    assert_snapshot!("", @"");               // line 13 - incorrectly got content from line 10!
});

The Fix

Instead of filtering during scanning, we now:

  1. Collect all snapshots with their macro boundaries (start/end line)
  2. Filter at the end to find the one whose macro span contains the target line

This approach is more general because:

  • The scanning logic collects everything uniformly
  • The line-matching logic is centralized in one place
  • It works at any nesting depth

Test plan

  • All existing tests pass (111 tests)
  • Added regression test test_find_snapshot_macro_multiple_in_with_settings
  • Manually verified with the reproduction case from the issue

🤖 Generated with Claude Code

Fixes mitsuhiko#857

When `find_snapshot_macro(line)` was called for a line inside a macro like
`with_settings!`, the `scan_nested_macros` function would scan all nested
tokens and find all `@"..."` patterns. Since `try_extract_snapshot` always
overwrote the result, the last snapshot found would win regardless of which
line we were searching for.

This caused:
1. All searches for lines inside `with_settings!` returned the same (last)
   snapshot position
2. The duplicate detection logic discarded all but the first pending snapshot
3. The first pending snapshot's content got written to the last snapshot's
   position

The fix collects all found snapshots with their macro boundaries (start/end
line), then filters at the end to find the one whose macro span contains the
target line. This is more general than filtering during scanning because:
- The scanning logic collects everything uniformly
- The line-matching logic is centralized in one place
- It works at any nesting depth

Co-Authored-By: Claude <noreply@anthropic.com>
@max-sixty max-sixty merged commit 822f404 into mitsuhiko:master Jan 8, 2026
15 checks passed
@max-sixty max-sixty mentioned this pull request Jan 15, 2026
2 tasks
max-sixty added a commit that referenced this pull request Jan 15, 2026
## Summary

- Fix inline snapshot corruption when multiple snapshots appear inside
`with_settings!` macro #858

## Test plan

- [x] All tests pass
- [x] Pre-commit passes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude <noreply@anthropic.com>
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.

Messed up inline snapshots b/c of filtering and/or with_settings! (?)

1 participant