Commit 822f404
Fix inline snapshot corruption with multiple snapshots in with_settings! (#858)
## 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:**
```rust
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
- [x] All existing tests pass (111 tests)
- [x] Added regression test
`test_find_snapshot_macro_multiple_in_with_settings`
- [x] Manually verified with the reproduction case from the issue
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-authored-by: Claude <noreply@anthropic.com>1 parent 7d27e3a commit 822f404
1 file changed
+72
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
| 129 | + | |
129 | 130 | | |
130 | 131 | | |
131 | 132 | | |
| |||
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
164 | | - | |
| 165 | + | |
165 | 166 | | |
166 | 167 | | |
167 | 168 | | |
168 | 169 | | |
169 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
170 | 174 | | |
171 | 175 | | |
172 | 176 | | |
173 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
174 | 183 | | |
175 | 184 | | |
176 | 185 | | |
| |||
185 | 194 | | |
186 | 195 | | |
187 | 196 | | |
188 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
189 | 204 | | |
190 | 205 | | |
191 | 206 | | |
| |||
216 | 231 | | |
217 | 232 | | |
218 | 233 | | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
224 | 243 | | |
225 | 244 | | |
226 | 245 | | |
| |||
272 | 291 | | |
273 | 292 | | |
274 | 293 | | |
275 | | - | |
| 294 | + | |
276 | 295 | | |
277 | 296 | | |
278 | 297 | | |
| |||
281 | 300 | | |
282 | 301 | | |
283 | 302 | | |
284 | | - | |
| 303 | + | |
285 | 304 | | |
286 | | - | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
287 | 312 | | |
288 | 313 | | |
289 | 314 | | |
| |||
664 | 689 | | |
665 | 690 | | |
666 | 691 | | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
667 | 727 | | |
0 commit comments