Skip to content

fix: align include-cycle error wording with Python beancount - #767

Merged
robcohen merged 2 commits into
mainfrom
fix/issue-765-duplicate-filename-wording
Apr 11, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/issue-765-duplicate-filename-wording

Conversation

@robcohen

Copy link
Copy Markdown
Member

Closes #765.

Summary

rustledger reported circular includes as "include cycle detected: a -> b -> a". Python beancount reports the same condition as "Duplicate filename parsed: \"a\"". The pta-standards include-cycle-detection conformance test asserts on the substring "Duplicate filename", so rustledger's wording didn't match even though both tools correctly detect the cycle.

Before:

error: include cycle detected: /path/a.beancount -> /path/b.beancount -> /path/a.beancount

After:

error: Duplicate filename parsed: "/path/a.beancount" (include cycle: /path/a.beancount -> /path/b.beancount -> /path/a.beancount)

The new wording leads with beancount's substring (satisfying conformance) and keeps the cycle path in a trailing parenthetical (preserving rustledger's debugging advantage).

What changed

  1. LoadError::IncludeCycle #[error(...)] in crates/rustledger-loader/src/lib.rs — updated to the new format. The duplicate filename comes from cycle.last(), which is always equal to one of the earlier entries in the cycle (that's how it got detected). Docstring explains why the wording is load-bearing.

  2. crates/rustledger/src/cmd/check.rs LoadError::IncludeCycle handling — collapsed two hardcoded strings (JSON output and stdout output) to use load_error.to_string(). This makes the #[error(...)] attribute the single source of truth and prevents the library error and the CLI output from drifting apart again. Pattern matches what fix: restore "not enough" phrasing for InsufficientUnits booking error #749 did for InsufficientUnits.

Regression test

test_include_cycle_display_contains_duplicate_filename_issue_765 in crates/rustledger-loader/tests/loader_test.rs pins three substrings that matter:

  • "Duplicate filename" — the conformance assertion
  • cycle_a.beancount — the file name is still shown
  • "include cycle:" — the debuggability guarantee is preserved

If a future refactor drops any of these, the test fails loudly with the actual rendered string. Same pattern as #749's test_insufficient_units_display_contains_not_enough.

Test plan

  • cargo test -p rustledger-loader — 57 + 31 + 2 pass, including the new regression test
  • cargo test --workspace --all-features — all suites green, 0 failures
  • cargo clippy --all-features --all-targets -- -D warnings — clean
  • cargo fmt --all -- --check — clean
  • pta-standards include-cycle-detection should flip from failing to passing once this lands

Note for reviewers

The existing test_load_include_cycle_detection test only inspects the cycle vec, not the Display string, so it continues to pass unchanged. The new test is additive and specifically targets the wording.

rustledger previously reported circular includes as
`"include cycle detected: a -> b -> a"`. Python beancount reports
the same condition as `"Duplicate filename parsed: \"a\""`. The
pta-standards `include-cycle-detection` conformance test asserts on
the substring `"Duplicate filename"`, so rustledger's wording didn't
match even though both tools correctly detect the cycle. See #765.

Change `LoadError::IncludeCycle`'s Display to lead with beancount's
phrasing while preserving the cycle path for debuggability:

    Duplicate filename parsed: "/path/a.beancount" \
    (include cycle: /path/a.beancount -> /path/b.beancount -> \
    /path/a.beancount)

The duplicate filename is `cycle.last()`, which is the path
re-encountered during traversal and is equal to one of the earlier
entries.

Also collapse the two hardcoded CLI strings in
`crates/rustledger/src/cmd/check.rs` (JSON and stdout paths) to use
`load_error.to_string()` instead of reconstructing the message. This
makes the `#[error(...)]` attribute on the variant the single source
of truth and prevents future wording drift between the library error
and the CLI output.

Regression test: `test_include_cycle_display_contains_duplicate_filename_issue_765`
in `rustledger-loader/tests/loader_test.rs` pins the exact substrings
the conformance suite depends on (`"Duplicate filename"`, the cycle
file name, and `"include cycle:"` for the debuggability guarantee).

Closes #765

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 11, 2026 17:45
@robcohen
robcohen requested a review from a team as a code owner April 11, 2026 17:45

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 aligns rustledger’s circular-include (LoadError::IncludeCycle) error wording with Python beancount by leading with Duplicate filename parsed, while preserving the include-cycle path for debugging, to satisfy pta-standards conformance (issue #765).

Changes:

  • Updated LoadError::IncludeCycle’s Display format string to include Duplicate filename parsed: "<file>" plus the cycle path in parentheses.
  • Updated rustledger check to use the canonical LoadError Display string for both JSON diagnostics and stdout output.
  • Added a regression test that pins the required substrings in the rendered IncludeCycle display.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
crates/rustledger/src/cmd/check.rs Routes IncludeCycle diagnostics/printing through LoadError::to_string() to keep wording centralized.
crates/rustledger-loader/src/lib.rs Changes IncludeCycle Display text to match beancount substring requirements while retaining cycle path.
crates/rustledger-loader/tests/loader_test.rs Adds a regression test asserting the new display string contains required substrings.

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

Comment thread crates/rustledger/src/cmd/check.rs Resolved
@github-actions

github-actions Bot commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 40.1ms ± 1.2ms 23 MB
beancount 913.6ms ± 12.5ms 50.1 MB
Comparison 22.8x faster 2.2x less

vs main branch (2026-04-11)

Metric main this PR Change
Time 41.9ms 40.1ms -4.3% ✅
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

@codecov

codecov Bot commented Apr 11, 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

✅ Compatibility Test Results

Metric Result
Check Compatibility 99%
BQL Compatibility 100%
Full AST Match 98%
Total Files 784
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

Copy link
Copy Markdown
Member Author

Acknowledging @Copilot's inline comment on check.rs:379 but pushing back: this is a false positive. The two branches are mutually exclusive (if json_mode { ... } else if !args.quiet { ... }), so only one runs per call, and Rust's borrow checker handles the move-in-one-branch / borrow-in-another pattern correctly in that case. The writeln!(..., "error: {message}") interpolation takes message by reference (&message), not by value, so even within a hypothetical single branch it wouldn't consume the binding.

The code compiles cleanly, which you can verify from the CI status: cargo check, cargo clippy --all-features --all-targets -- -D warnings, and the full cargo test --workspace all ran green on this PR. If there were a real use-after-move here, cargo check would have rejected it before any of those.

Copilot's suggestion (adding message: message.clone()) would introduce a pointless clone on the hot path.

No code changes.

@robcohen
robcohen added this pull request to the merge queue Apr 11, 2026
Merged via the queue into main with commit 63baf6a Apr 11, 2026
39 of 41 checks passed
@robcohen
robcohen deleted the fix/issue-765-duplicate-filename-wording branch April 11, 2026 19:57
robcohen added a commit that referenced this pull request Apr 11, 2026
Bumps the whole workspace from 0.11.0 to 0.12.0 (minor version bump
under pre-1.0 semver because the cycle since 0.11.0 includes
user-visible breaking changes that release-plz's auto-detection
missed).

release-plz auto-generated a v0.11.1 patch bump from the `fix:` /
`feat:` commits since 0.11.0. That's too conservative. The cycle
since 0.11.0 shipped the following breaking changes, all merged as
`refactor:` / `fix:` without the `!` conventional-commit marker:

- **`rustledger_booking::BookingError`** collapsed three variants
  (`InsufficientUnits`, `NoMatchingLot`, `AmbiguousMatch`) into a
  single `Inventory(AccountedBookingError)` variant (#751). Out-of-tree
  consumers pattern-matching on the old variants break.

- **`rustledger_core::AccountedBookingError`** added as a new
  canonical user-facing type with `BookingError::with_account()` as
  the constructor (#751). Not a break on its own, but part of the
  unification.

- **`rustledger_lsp::handlers::diagnostics::all_diagnostics`**
  signature changed to take an additional `other_buffer_overlays:
  &[(u16, &[Spanned<Directive>])]` parameter for multi-buffer LSP
  overlays (#760 / #764).

- **`rustledger_lsp::handlers::diagnostics::validation_errors_to_diagnostics`**
  signature changed to take an owned `Vec<Spanned<Directive>>`
  instead of borrowed slices (#758). Optimizes a hot path but breaks
  the function's public signature.

- **`rustledger_loader::LoadError::IncludeCycle`** Display format
  rewritten to match Python beancount's `"Duplicate filename
  parsed: ..."` wording (#767). Substring-matching consumers break.

Under pre-1.0 semver, any breaking change should bump the minor
version (0.x → 0.(x+1)). So 0.11.0 → 0.12.0 is correct.

Highlights since 0.11.0:

### Breaking

- Unified `BookingError` under a single `Inventory` variant with one
  canonical `Display` impl, preventing the wording drift that
  produced #748 (#751).
- `LoadError::IncludeCycle` Display now begins with `"Duplicate
  filename parsed: "` to match beancount's wording for conformance
  (#767).
- LSP `all_diagnostics` and `validation_errors_to_diagnostics`
  signatures changed to support the multi-buffer overlay and avoid
  double-cloning directive lists (#758 / #764).

### Features

- LSP live overlay so in-memory buffer edits reflect in diagnostics
  before save, for both the current file (#685 / #758) and any other
  open buffer in a multi-file ledger (#760 / #764).
- Parser `Vec::with_capacity` hints with DoS caps on `source.len()`
  heuristics (#754).
- `working_inventories` cloning in the booking engine now only
  touches accounts actually referenced in the transaction, and
  dedupes repeated account references (#756).

### Fixes

- `InsufficientUnits` booking error wording restored to `"Not enough
  units in ..."` so pta-standards `reduction-exceeds-inventory`
  conformance assertion passes (#748 / #749).
- `noduplicates` plugin now hashes flag, tags, links, cost, and
  price in addition to date and narration — complete beancount
  `hash_entry` parity (#746 / #747).
- Parser test suite: 14 fake-coverage tests replaced with real tests
  that exercise the functions they claim to cover (#766).
- All Copilot inline review comments on #754-#763, #766, #767
  addressed in fix commits.
- Closed PRs #768 (dead incremental parser) and #769 (hallucinated
  simdutf8 API) — no code shipped from these.

### Internal

- Forbid-unsafe-code invariant now has a dedicated CI check and
  pre-push hook (#770).
- New pre-push `cargo check` hook to catch compile errors before PRs
  are opened (#770).

This commit supersedes release-plz's auto-generated v0.11.1 proposal
to produce the minor bump the breaking changes require.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@robcohen robcohen mentioned this pull request Apr 11, 2026
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.

Circular include error message differs from beancount ("include cycle detected" vs "Duplicate filename parsed")

2 participants