Skip to content

fix(extract): fail loudly when no transactions are extracted - #1452

Merged
robcohen merged 2 commits into
mainfrom
fix/extract-zero-rows-exit
Jun 20, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/extract-zero-rows-exit

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

extract printed Extracted 0 transactions and exited 0 regardless of the result, so a garbage / wrong-type / empty file silently produced nothing:

$ printf 'garbage not a csv\nsecond line\n' > foo.txt
$ rledger extract foo.txt -a Assets:Bank; echo $?
Extracted 0 transactions from foo.txt
0

A scripted extract … >> ledger.beancount therefore appends nothing silently on a malformed or wrong-path-but-existing file (only a nonexistent path exited non-zero).

How

Bail with an actionable message when the result is empty (before writing output), so the exit code is non-zero:

error: no transactions were extracted from foo.txt
  the file may not match a recognized importer format, be empty, or use unexpected columns
  try: --auto, an explicit importer (--importer), or column flags (--date-column, --amount-column, …)

Verify

printf 'garbage not a csv\n' > foo.txt
rledger extract foo.txt -a Assets:Bank; echo $?   # before: exit 0 | after: error + non-zero

A valid CSV still extracts normally (regression guarded). Regression test test_extract_empty_result_exits_nonzero asserts a non-importable file exits non-zero with the explanation.

🤖 Generated with Claude Code

`extract` printed "Extracted 0 transactions" and returned Ok regardless
of the result, so a garbage / wrong-type / empty file yielded exit 0.
A scripted `extract … >> ledger.beancount` then silently appended
nothing (only a nonexistent path exited non-zero).

Bail with an actionable message (likely causes + what to try) when the
result is empty, before writing output, so the exit code is non-zero.

Regression test asserts a non-importable file exits non-zero with the
explanation.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI review requested due to automatic review settings June 20, 2026 14:37
@robcohen
robcohen requested a review from a team as a code owner June 20, 2026 14:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the rledger extract CLI to stop silently succeeding when an input file produces an empty extraction result, which helps prevent scripts from appending nothing to a ledger without noticing.

Changes:

  • Make extract return a non-zero exit code with an actionable error message when the extraction result is empty.
  • Add a CLI regression test asserting non-importable input fails with an explanatory message.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
crates/rustledger/src/cmd/extract_cmd/mod.rs Adds an empty-result guard that bails with guidance instead of exiting 0 after extracting nothing.
crates/rustledger/tests/cli_commands_test.rs Adds a regression test verifying extract exits non-zero and explains the empty extraction case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Resolve cli_commands_test.rs conflict: keep both the #11 broken-pipe
  test (from main) and the new #18 empty-extract test.
- Count EXTRACTED transactions right after importer.extract() and bail
  only when that is zero — before duplicate filtering (--existing) or
  --balance augmentation, which could otherwise mask the cause (all
  duplicates, or a lone balance directive).
- Base the "Extracted N transactions" count on Directive::Transaction
  values rather than directives.len().
- Use OsStr-safe .arg(file.path()) in the regression test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen

Copy link
Copy Markdown
Member Author

Addressed both findings: the empty-result check now counts EXTRACTED transactions right after importer.extract() and bails only when that is zero — before --existing dedup or --balance augmentation can mask the cause; the "Extracted N" line now counts Directive::Transaction values, not directives.len(). The regression test uses OsStr-safe .arg(file.path()). Also merged main (resolving the cli_commands_test.rs append conflict).

@codecov

codecov Bot commented Jun 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 38ms ± 0.9ms 24.4 MB
beancount 938.2ms ± 4.3ms 50.7 MB
Comparison 24.7x faster 2.1x less

vs main branch (2026-06-20)

Metric main this PR Change
Time 41.3ms 38ms -8.0% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen merged commit 18cb0db into main Jun 20, 2026
40 checks passed
@robcohen
robcohen deleted the fix/extract-zero-rows-exit branch June 20, 2026 15:03
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