Repository navigation
fix(extract)!: take a CSV entry's missing currency from its open directive - #2498
Conversation
…ctive An `importers.toml` entry without `currency` got `USD` from inside the CSV importer, so a euro account's statement was booked in dollars with no warning. `rledger extract` now fills a missing entry currency from the account's `open` directive in `--ledger` (then `--existing`) when it names exactly one currency, and otherwise refuses with an error naming the importer and the fix. `CsvImporter` has no built-in currency any more: a config with neither `currency` nor `currency_column` is refused, and a blank currency cell with no default is a row error. The raw-flag `--currency` default and the component's documented USD default are unchanged. Refs #2464 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
When --ledger opens the importer account with no currency or several, that is the answer (an error), not a reason to consult --existing; --existing is read only when --ledger never opens the account. Tests pin included-file opens, a later close, and an unconstrained open, and the CHANGELOG says how to keep the old result (`currency = "USD"`). Refs #2464 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Adversarial review passI checked the "single
Every error starts The CHANGELOG Breaking entry now tells users how to keep the old result: add This PR still uses Fail without fix. With Second pass: cross-PR integration and docsI merged this PR together with #2495 and #2497 in five orders onto On the combined tree, an entry with no The new test I re-read the docs against the final behavior and corrected Third pass: code quality and docs
1181 tests pass across 37 binaries, both mutations still fail their tests, and clippy and rustdoc are clean, all on this worktree's own target. Head: 3798d10. Fourth pass: merged with
|
| Case | Result |
|---|---|
| No ledger | exit 1; names the importer and the fix |
open with EUR,CHF |
exit 1; "declares EUR, CHF, more than one" |
--ledger with a failing include |
exit 1 via the profile reader: "ledger … has 1 error(s) and cannot be read" |
| Include that loads but has a later broken entry | EUR is used |
| Blank currency cell with no default | row warning, then exit 1 with "no transactions were extracted" |
-o file |
written only on success |
--existing is a directory |
exit 1 |
Finding fixed in 6fad6b0. When --existing's open sat in an include that failed to load, the error said the ledger "has no open Assets:Bank directive", which sends the user to look in the wrong place. It now says no open Assets:Bank directive loaded from …, which has 1 load error(s) (first: …); the open may be in the part that failed. Fail without fix: with the load errors not named, the new test fails.
1186 tests pass across 37 binaries, both earlier mutations still fail their tests, and clippy is clean. All runs used this worktree's own target.
Sixth pass: misconfiguration of currency
I checked each case against a release build. Every finding below was fixed in 851d771.
- A
currencythat is not a commodity."usd"and"€"failed withcanonical formatter failed to re-parse the synthesized directive text, which names nothing the user configured.""was worse: it succeeded, emittingAssets:Bank -4.00with no commodity. All three now fail with an error naming the source:`currency` in the 'm' entry of importers.toml is "usd", which is not a valid commodity (commodities are upper-case, like USD or EUR), or the same with--currency. The check is the newrustledger_parser::is_valid_currency, which runs the lexer the same wayis_valid_account_namedoes, so it cannot drift from the parser. A non-string value (currency = 5) was already a TOML type error. - Precedence is now documented and conflicts are reported. The order is: a
--ledgerprofile, then--currency, then the entry'scurrency, then the account's singleopencurrency. The configured value always wins. Before this fix, a configured currency that the account'sopendoes not allow (EUR intoopen … CHF) was silent, andrledger checkwould then reject every posting. It now printswarning: currency in the 'm' entry … books EUR, but open Assets:Bank in … allows only CHF. A--currencythat a--ledgerprofile overrides used to be dropped silently; it now getswarning: --currency EUR is not used: the --ledger profile for … declares USD. - Fail without fix. Removing the validation, the conflict warning or the override warning each fails its test. The
main-behavior mutation still fails 6 CLI tests and the importer test.
1189 tests pass across 37 binaries, 664 parser tests pass, and clippy and the parser's rustdoc are clean. All runs used this worktree's own target.
One trade-off for review. The conflict check reads the open from --ledger/--existing even when currency is set. With --existing that means the ledger is loaded a second time on every import. I'm keeping this for correctness and listing it alongside the existing double-load follow-up.
Seventh pass: harness integrity and a literal docs walkthrough
Harness integrity. I re-ran every mutation from passes 1–6 through a harness that, for each mutation:
- asserts the exact match count for every replacement;
- checks the file on disk changed;
- requires
Compiling <crate>lines as proof of a rebuild; - adds up every
test result:line; - restores the files byte for byte and checks them again.
Tests run: -p rustledger-importer -p rustledger-parser --all-features --lib plus -p rustledger --all-features --lib. The baseline (no mutation) is 1107 tests, 0 failed. Every mutation applied, triggered a rebuild, and failed at least one test:
| Mutation | Applied (file changed, crates rebuilt) | Tests run | Failed |
|---|---|---|---|
| baseline (no mutation) | yes (7 crates rebuilt) | 1107 in 3 binaries | 0 |
| p1 main behavior: no resolution + importer USD fallback | yes (2 crates rebuilt) | 1107 in 3 binaries | 7 |
| p2 --existing may overrule an ambiguous --ledger | yes (2 crates rebuilt) | 1107 in 3 binaries | 1 |
| p5 load errors not named | yes (1 crates rebuilt) | 1107 in 3 binaries | 1 |
| p6 no currency validation | yes (1 crates rebuilt) | 1107 in 3 binaries | 1 |
| p6 no open-conflict warning | yes (1 crates rebuilt) | 1107 in 3 binaries | 1 |
| p6 no profile-override warning | yes (1 crates rebuilt) | 1107 in 3 binaries | 1 |
The the_resolved_currency_is_what_dedup_compares mutation only bites on a tree with #2495 merged in; it was verified on the combined tree in pass 2.
Docs walkthrough. I followed both docs literally against the PR binary.
- The
importing.mdChase example (rledger extract --importer chase chase-statement.csv) works as written. - Dropping
currencyand passing--ledgerwithopen Assets:Bank:Chase USDprintsUsing currency USD from the open Assets:Bank:Chase directive in main.beancountand imports in USD. - With no ledger, the error names the importer and the fix, as the docs say.
Nothing in this PR's docs needed fixing. The extract.md CSV Options block repeats date_column and amount_column, so it fails to parse as TOML when copied. That predates this PR and is fixed in #2495 (3fb32fb) rather than here, so the two PRs don't conflict. Head 851d771 is unchanged and green on the required checks.
An entry without `currency` resolves EUR from the open directive, and an EUR duplicate already in --existing is then skipped; on a tree with the #2421 commodity-scoped dedup this fails if the rows carry a stale USD. Docs describe the --ledger over --existing authority and includes. Refs #2464 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
… currency One helper builds the plugin-free, validation-free LoadOptions that both importer-profile reading and open-currency lookup use, and the CSV importer uses plain `context` for its no-currency row error. Refs #2464 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Refs #2464 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
…ilent-usd # Conflicts: # CHANGELOG.md
With the `open` inside an include that failed to load, the missing- currency error said the ledger had no `open` for the account, sending the user to look in the wrong place. It now says no `open` loaded, gives the load error count and the first error, and that the `open` may be in the part that failed. Refs #2464 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
- A `currency` (or --currency) that is not a commodity (`usd`, `€`, "") surfaced as "canonical formatter failed to re-parse", or for "" as amounts with no commodity. It is now an error naming its source, checked with the new `rustledger_parser::is_valid_currency` (runs the lexer, like `is_valid_account_name`). - A configured currency the account `open` does not allow now warns (`rledger check` would reject every posting); the configured value still wins. The precedence is documented. - A --currency that a --ledger profile overrides is reported instead of dropped silently. Refs #2464 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
…ilent-usd # Conflicts: # CHANGELOG.md
…ency one "currency_column X is V, which is not a valid commodity (commodities are upper-case, like `USD` or `EUR`)", matching #2498. Pins that the value is validated after upper-casing (full-width letters and an inner space stay refused) and that surrounding whitespace is trimmed first. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
… importer (#2531) * fix(extract): validate currency_column cells and --currency for every importer A currency_column cell that is not a commodity is now a row error naming the row, the column and the value, instead of a posting that fails to re-parse later; a lower- or mixed-case ISO code in a cell is upper-cased. --currency is checked once at the start of the run for every importer type, and an OFX/WASM entry's currency like a CSV entry's, through one shared check_currency. Closes #2516 Closes #2517 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr * fix(extract): word the currency_column error like the configured-currency one "currency_column X is V, which is not a valid commodity (commodities are upper-case, like `USD` or `EUR`)", matching #2498. Pins that the value is validated after upper-casing (full-width letters and an inner space stay refused) and that surrounding whitespace is trimmed first. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr * docs(changelog): drop the stale MIN/MAX entry the merge kept Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
What changed
An
importers.tomlentry that named nocurrency, run without--currency, got"USD"from deep inside the CSV importer (csv_importer.rs,default_currency). A euro or pound account's statement was booked in dollars with no warning, and because the currency was wrong,--existingcould no longer match the transactions the user had entered by hand (#2464). Correctness comes first here: a guessed currency corrupts the ledger silently.CLI (
rledger extract), for entries without acurrency, in a newresolve_entry_currency:--ledgerledger has anopendirective for the entry's account that names exactly one currency, that currency is used, and a line on stderr says so.--existingis consulted only when--ledgernever opens the account, so an ambiguousopenin--ledgeris not overruled by another file. Includes are followed, and a laterclosedoes not matter.openfor the account, or anopendeclaring none or several currencies), and points to the fix:An entry with a
currency_columnstill imports, because its rows carry their own currency.Importer library (
CsvImporter): there is no built-in currency any more. A config with neithercurrencynorcurrency_columnis refused up front. A row with a blank currency cell and nocurrencyis a row error instead of USD.Unchanged on purpose:
-a … -c …,--auto) keeps its documented--currency [default: USD].importer.extractkeeps the USD default that its WIT doc promises (currency defaults to USD exactly like the CLI's --currency flag). It setscurrencybefore building, so it does not reach the importer's new refusal. Changing that contract is a WIT-level decision, so it is left for a follow-up.This is a behavior change, so it goes in the CHANGELOG under Breaking. The entry tells users how to keep the old result: add
currency = "USD"to the entry, pass--currency USD, or, for library users, setImporterConfig.currency = Some("USD"). Docs:docs/commands/extract.mdanddocs/guides/importing.md.This PR uses
Refs #2464rather than closing it. The issue also asks for payee rewriting and reports a dedup miss that still needs a sample from the reporter. #2495 changes the dedup matching.Decisions (approved by @robcohen)
currencytakes the currency from the account'sopen.--ledgeris the authority.--existingis consulted only when--ledgernever opens the account. Anything ambiguous is an error that names the importer and the fix.importer.extractkeeps its documented USD default, and the raw-flag--currencydefault stays as it is.Refs #2464, notCloses. Payee rewriting is a separate feature, and the remaining dedup report is waiting on a sample.How tested
entry_without_currency_uses_the_open_directive: an entry with no currency, plusopen Assets:Bank:Euro EUR, produces-5.00 EURthrough both--existingand--ledger.entry_without_currency_and_no_single_open_currency_is_an_error: covers no ledger, anopenwithEUR,CHF, and a ledger with noopenfor the account. Each gives an error naming the importer and the reason.test_csv_import_without_a_currency_is_refused, at the importer level: no currency and no column gives an error, and a blank currency cell gives a row warning.resolve_entry_currencyremoved and the importer's USD fallback restored (main's behavior), all three tests fail. Main output wasAssets:Bank:Euro -5.00 USD.test_csv_import_default_currencypinned the USD default, so it is removed. Five existing CLI tests whoseimporters.tomlfixtures had nocurrency(and so silently relied on USD) now setcurrency = "USD", andtest_csv_importer_newsets it on its builder.cargo test -p rustledger-importer -p rustledger-ffi-component -p rustledger --all-features: 1177 tests pass across 37 test binaries.cargo clippy --workspace --all-features --all-targets -D warningsandcargo doc -D warnings(importer and rustledger) are clean.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.Refs #2464
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr