Repository navigation
fix(importer): resolve equal-length mappings deterministically in file order - #2497
Conversation
…e order `[importers.mappings]` was read into a HashMap and the rules were ordered only by pattern length, so two equally long patterns that both matched picked an account by hash order, differently from run to run. Mappings are now a list kept in file order (the `toml` deserializer yields keys sorted, so each key's span restores the written order), and the stable length sort makes the first-written pattern win a tie. Closes #2423 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Subtables, inline tables, dotted keys, literal and unicode keys, empty tables and several [[importers]] entries all keep file order through the span-based Mappings deserializer. Refs #2423 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Adversarial review passI checked whether file order survives every way an
Nothing needed fixing in this pass; the new test pins the behavior. 1179 tests pass across 37 binaries, and clippy and rustdoc are clean. All of it ran on a target dir used only by this worktree, since shared target dirs can reuse another worktree's build.
Second pass: cross-PR integration and docsI merged this PR together with #2495 and #2498 in five orders onto I re-read the docs ( Third pass: code quality and docsI found nothing to change in this pass.
Head ec7a672 is unchanged. |
…-deterministic # Conflicts: # CHANGELOG.md
What changed
[importers.mappings]was read into aHashMap<String, String>, and the rules were sorted only byReverse(len). When two patterns of the same length both matched ("foo"and"bar"againstfoobar), the account depended on hash order, which is randomized per map. The same input produced different accounts from one run to the next. The issue shows 14 runs out of 20 picking one account and 6 the other.The tie-break is now file order:
ImporterEntry::mappingsis a newMappingstype, a list of(pattern, account)pairs in the order the file wrote them.tomlcrate (withoutpreserve_order) yields table keys sorted, not in document order, so the deserializer reads each key astoml::Spanned<String>and orders the pairs by key span. I did not turn onpreserve_order: feature unification would apply it to every crate in the workspace that usestoml, which would reorder every other table the workspace serializes (for exampleentry_toml_from_inferred's output).build_config_from_entrykeeps its stablesort_by_key(Reverse(len)). Longer patterns still win, and among patterns of equal length the first one written wins.This tie-break is documented in
docs/guides/importing.md, in the doc comments, and in the CHANGELOG (Fixed).I did not add a warning for input that matches several equally ranked patterns, which the issue suggested as optional. With a deterministic, documented tie-break, a warning on every such row would be noise for configs that rely on the order on purpose.
How tested
equal_length_mappings_resolve_deterministically_in_file_order: TOML text →load_importers_config→build_config_from_entry→CsvImporter, 200 runs per file order. Both orders are tested (foo, bargivesExpenses:Foo, andbar, foogivesExpenses:Bar), which shows that file order is what decides.mappings_keep_file_order: a reverse-alphabetical table comes back in file order through the CLI loader.mappings_keep_file_order_through_build, inrustledger-importer: the bare-table form the component'simporter.extractparses keeps file order, and the built config orders by length and then file order.HashMap(the old behavior), both CLI tests fail. The determinism test gotExpenses:BarwhereExpenses:Foowas expected.next_entry::<String, String>), both tests failed with keys in sorted order, which is how thetomlkey-order behavior was found.cargo test -p rustledger-importer -p rustledger-ffi-component -p rustledger --all-features: 1178 tests pass across 37 test binaries.cargo clippy --workspace --all-features --all-targets -D warningsandcargo doc -D warnings -p rustledger-importerare clean.API note
ImporterEntry::mappingschanges type fromHashMap<String, String>toMappings, which offersget,len,is_empty,iterandFromIterator<(String, String)>. Deserializing the field requires thetomldeserializer, because it reads key spans. Every existing path (toml::from_strin the CLI and the component) does.Merging alongside #2495, #2497 and #2498
These three PRs touch the same importer and extract code. I merged them onto
origin/main(184e9d1) in five orders: A-B-C, A-C-B, B-A-C, C-B-A and C-A-B, where A is #2495, B is #2497 and C is #2498. Every order gives a byte-identical tree. The only conflicts are inCHANGELOG.md, where two PRs add a bullet at the top of the same section (### Fixed/### Breaking). Keep both bullets.toml_entry.rs,extract_cmd/mod.rsanddocs/guides/importing.mdall merge automatically. On the combined tree, 1300+ tests pass across the ops, importer, ffi-component and rustledger suites. A CSV entry with nocurrencythen takes EUR from theopendirective, and dedup compares in EUR. The #2498 testthe_resolved_currency_is_what_dedup_compareschecks this, and it fails on the combined tree when the old USD fallback is restored.Closes #2423
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr