Skip to content

fix(cache): hide cache file as dotfile and honor beancount env vars (closes #939) - #945

Merged
robcohen merged 4 commits into
mainfrom
fix/cache-file-hidden-dotfile
Apr 28, 2026
Merged

robcohen merged 4 commits into
mainfrom
fix/cache-file-hidden-dotfile

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

Fixes issue #939 — rledger check main.beancount created a visible main.beancount.cache next to the source, which surprised users and cluttered ls / git status output.

The deeper question — should the cache live next to the source at all? — is already answered by Python beancount, which has been using .{filename}.picklecache (hidden dotfile) for years. This PR adopts that convention so rustledger users (especially those migrating from bean-check) get familiar, ecosystem-consistent behavior.

Changes

Behavior

  • Cache filename is now .<file>.cache (hidden dotfile) instead of <file>.cache.
  • New env var BEANCOUNT_DISABLE_LOAD_CACHE — opt out persistently (matches Python beancount).
  • New env var BEANCOUNT_LOAD_CACHE_FILENAME — redirect cache to a custom path. Supports {filename} substitution; relative paths resolve against the source directory; absolute paths are used as-is. Empty value treated as unset.
  • After writing the new cache, the legacy visible <file>.cache is silently deleted if present, so upgrading users don't have to clean up by hand.
  • The --no-cache / -C CLI flag is unchanged (per-invocation override).

Tests

  • Unit tests for resolve_cache_pattern (relative, absolute, no-substitution patterns) and legacy_cache_path.
  • New tests/cache_env_var_test.rs integration binary that exercises the full env-var path. Lives in a separate test binary because mutating process env requires unsafe since Rust 2024 and the workspace forbids unsafe_code; the test binary opts out with #![allow(unsafe_code)].
  • Updated test_load_cache_invalid_magic to write the malformed cache at the new dotfile path so it actually exercises the load path (not just the missing-file fallback).

Docs

  • docs/commands/check.md — new "Cache File" section covering naming, env vars, and migration note.
  • docs/reference/architecture.md — refreshed Binary Cache section with dotfile location, content-based (not mtime-based) invalidation note, and env var pointer.
  • crates/rustledger-loader/src/cache.rs module rustdoc updated to match.

Migration impact

Existing visible <file>.cache files left behind by earlier rustledger versions are silently removed on the next rledger check (when a new cache is written). If a user has *.beancount.cache in their .gitignore, the entry becomes a no-op but harmless.

bean-check users who were already setting BEANCOUNT_DISABLE_LOAD_CACHE or BEANCOUNT_LOAD_CACHE_FILENAME for their Python tooling now get the same behavior from rledger.

Verification

  • cargo test -p rustledger-loader -p rustledger: 488 + 372 tests pass (includes 4 new env-var tests, 4 refactored unit tests).
  • cargo clippy --all-features --all-targets -- -D warnings: clean.
  • cargo fmt --check: clean.

Test plan

  • CI: Clippy, Test, Doctests, Compatibility tests, Format
  • Manual: rledger check main.beancount writes .main.beancount.cache (hidden), not main.beancount.cache.
  • Manual: BEANCOUNT_DISABLE_LOAD_CACHE=1 rledger check main.beancount writes no cache.
  • Manual: BEANCOUNT_LOAD_CACHE_FILENAME=~/.cache/rledger/{filename}.cache rledger check main.beancount writes to ~/.cache/rledger/main.beancount.cache.
  • Manual: with a leftover main.beancount.cache in the dir, run rledger check; the legacy file disappears, the new dotfile appears.

Closes #939

🤖 Generated with Claude Code

…loses #939)

`rledger check` wrote a visible `<file>.cache` next to every ledger,
creating clutter that surprised users (#939). Match Python beancount's
established convention instead:

- Default cache filename is now `.<file>.cache` (hidden dotfile),
  mirroring beancount's `.{filename}.picklecache`.
- Honor `BEANCOUNT_DISABLE_LOAD_CACHE` to opt out persistently.
- Honor `BEANCOUNT_LOAD_CACHE_FILENAME` to redirect the cache; supports
  `{filename}` substitution and resolves relative paths against the
  source directory, matching beancount semantics.
- Empty `BEANCOUNT_LOAD_CACHE_FILENAME` is treated as unset to prevent
  every ledger's cache from collapsing onto the same file.
- After writing the new dotfile cache, opportunistically delete the
  legacy visible `<file>.cache` (silent on failure) so upgrading users
  don't have to clean up by hand.

The `--no-cache` / `-C` CLI flag continues to work for one-shot opt-out.

Tests:
- New unit tests for `resolve_cache_pattern` and `legacy_cache_path`.
- New `tests/cache_env_var_test.rs` integration test that exercises
  the env var path end-to-end. Lives in a separate test binary because
  mutating process env requires `unsafe` since Rust 2024 and the
  workspace forbids `unsafe_code`.
- Updated `test_load_cache_invalid_magic` to write the malformed cache
  at the new dotfile path so it actually exercises the load path.

Docs:
- `docs/commands/check.md` — added a Cache File section with naming,
  env vars, and migration note.
- `docs/reference/architecture.md` — refreshed the Binary Cache section
  with the dotfile location, content-based invalidation note, and env
  var pointer.
- `cache.rs` module-level rustdoc updated.

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

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 rustledger’s binary load-cache behavior to align with Python beancount conventions by hiding the cache as a dotfile, honoring beancount-compatible environment variables, and cleaning up legacy visible cache files created by earlier versions.

Changes:

  • Switch default cache filename from <file>.cache to .<file>.cache and remove legacy <file>.cache after successfully writing the new cache.
  • Add support for BEANCOUNT_DISABLE_LOAD_CACHE (disable caching) and BEANCOUNT_LOAD_CACHE_FILENAME (redirect cache path with {filename} substitution).
  • Add/adjust unit + integration tests and update user documentation for the new cache behavior.

Reviewed changes

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

Show a summary per file
File Description
docs/reference/architecture.md Updates architecture docs for cache location and invalidation behavior.
docs/commands/check.md Documents cache dotfile naming, env vars, and migration notes for rledger check.
crates/rustledger/src/cmd/check.rs Honors disable-cache env var in the check command and uses a unified cache_disabled gate.
crates/rustledger-loader/tests/cache_env_var_test.rs Adds integration tests for env-var-driven cache path behavior (with serialized env access).
crates/rustledger-loader/src/lib.rs Re-exports cache env constants and cache_path to support CLI/tests.
crates/rustledger-loader/src/cache.rs Implements dotfile default, env-var cache path patterns, legacy cleanup, and updates tests.

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

Comment thread crates/rustledger-loader/src/cache.rs Resolved
Comment thread crates/rustledger-loader/src/cache.rs Resolved
Comment thread docs/reference/architecture.md Resolved
Comment thread docs/commands/check.md Resolved
Comment thread crates/rustledger-loader/src/cache.rs Resolved
@codecov

codecov Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.87629% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/cache.rs 95.74% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 32.9ms ± 5.4ms 21.7 MB
beancount 881.9ms ± 5.5ms 50.1 MB
Comparison 26.8x faster 2.3x less

vs main branch (2026-04-28)

Metric main this PR Change
Time 35.9ms 32.9ms -8.4% 🟢
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

github-actions Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 99%
BQL Compatibility 85%
Full AST Match 99%
Total Files 782
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 and others added 3 commits April 28, 2026 16:12
Pick up two spots missed in the initial #939 fix:

- `.gitignore`: keep matching legacy `<file>.beancount.cache` (gitignored
  for users upgrading from earlier versions) and add an explicit dotfile
  pattern with a comment explaining the rationale. The bare `*` pattern
  already matched dotfiles per gitignore semantics, but the explicit
  pattern makes the intent clear.
- `performance-roadmap.md`: cache location example was still showing the
  visible filename — updated to the dotfile form with an env-var pointer.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Five substantive issues raised by the Copilot review on PR #945:

1. **Custom cache directories now created automatically.**
   `BEANCOUNT_LOAD_CACHE_FILENAME` patterns can resolve to a directory
   that doesn't exist (e.g. `~/.cache/rledger/{filename}.cache` on a
   fresh install). `save_cache_entry` now `create_dir_all`s the parent
   eagerly so caching isn't silently disabled.

2. **`BEANCOUNT_DISABLE_LOAD_CACHE` is now honored at the loader level.**
   Previously only the CLI checked it, so any external consumer of
   `rustledger-loader::{load,save}_cache_entry` would silently bypass
   the kill switch. Both functions now early-return when the env var
   is set to a non-empty value (matching Python beancount semantics).
   The CLI check stays as an early-out optimization.

3. **`default_cache_path` exposed as a pure helper.**
   `cache_path()` reads process env, which makes any test that calls
   `save_cache_entry`/`invalidate_cache` env-sensitive.
   `default_cache_path()` returns the dotfile path with no env lookup —
   useful for tests and library consumers that need determinism.

4. **Doc accuracy.** I had claimed cache invalidation was "content-based"
   in `docs/commands/check.md` and `docs/reference/architecture.md`,
   but `compute_hash` actually hashes file path + mtime + size.
   Reworded both pages to describe the real metadata-based behavior
   (and noted content-based hashing as a possible future improvement).

5. **Module rustdoc clarified** to state explicitly that the env vars
   are honored at the loader level, not just by CLI callers.

Tests:
- New integration test `save_creates_missing_parent_directory` covers
  the regression in #1.
- New integration test `disable_env_makes_load_return_none_and_save_no_op`
  covers the loader-level enforcement in #2.
- In-module roundtrip/invalidate tests now `assert_clean_cache_env()` at
  the top so a developer with locally-set cache env vars sees a clear
  failure rather than mysterious behavior.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…elf-review)

Self-review of PR #945 surfaced five issues; this commit addresses all of
them.

1. **Match Python beancount on `BEANCOUNT_DISABLE_LOAD_CACHE`.** Python's
   check is `os.getenv(...) is None`, which means *any* presence — empty
   string included — disables the cache. We required a non-empty value;
   that's a silent divergence for users who write `EXPORT FOO=` in their
   shell. Switched to `is_some()` to match.

2. **Normalize the disable check between loader and CLI.** Both now go
   through the new `cache_disabled_by_env()` helper, exported from the
   loader so any downstream consumer (LSP, MCP, future tools) gets the
   same semantics for free.

3. **`invalidate_cache` now also removes the legacy sidecar.**
   `save_cache_entry` already cleans up the pre-#939 visible file when
   it writes a new dotfile cache; `invalidate_cache` was missing the
   same logic, so a `rledger check --invalidate-cache` (hypothetical or
   programmatic invalidate) would leave the legacy file behind.

4. **`with_env` is now panic-safe.** Replaced the inline save/restore
   pattern with an `EnvGuard` Drop type. A test that panics mid-body no
   longer leaks its mutated env into the next test in the binary.

5. **`disable_env_makes_load_return_none_and_save_no_op` was vacuous.**
   It asserted `load_cache_entry → None`, but the cache file didn't
   exist (because save was a no-op), so the test would have passed even
   if `load_cache_entry` ignored the env entirely. Restructured to:
   (a) write a real cache with the env unset, (b) set the env and
   verify load returns None despite the cache being on disk, (c) verify
   save doesn't touch the existing file (mtime invariant).

Plus one new test: `empty_disable_env_value_still_disables` locks in
the Python-beancount-compatible empty-value semantics from #1.
And one new test: `test_invalidate_cache_removes_legacy_sidecar`
covers the new behavior in #3.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen merged commit 8c3f56c into main Apr 28, 2026
32 checks passed
@robcohen
robcohen deleted the fix/cache-file-hidden-dotfile branch April 28, 2026 22:12
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.

[Bug]: Unexpected .cache file after running rledger check

2 participants