Repository navigation
fix(cache): hide cache file as dotfile and honor beancount env vars (closes #939) - #945
Merged
Merged
Conversation
…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]>
Contributor
There was a problem hiding this comment.
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>.cacheto.<file>.cacheand remove legacy<file>.cacheafter successfully writing the new cache. - Add support for
BEANCOUNT_DISABLE_LOAD_CACHE(disable caching) andBEANCOUNT_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.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
📊 Benchmark Results
vs main branch (2026-04-28)
What do these numbers mean?
|
Contributor
|
| 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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes issue #939 —
rledger check main.beancountcreated a visiblemain.beancount.cachenext to the source, which surprised users and clutteredls/git statusoutput.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 frombean-check) get familiar, ecosystem-consistent behavior.Changes
Behavior
.<file>.cache(hidden dotfile) instead of<file>.cache.BEANCOUNT_DISABLE_LOAD_CACHE— opt out persistently (matches Python beancount).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.<file>.cacheis silently deleted if present, so upgrading users don't have to clean up by hand.--no-cache/-CCLI flag is unchanged (per-invocation override).Tests
resolve_cache_pattern(relative, absolute, no-substitution patterns) andlegacy_cache_path.tests/cache_env_var_test.rsintegration binary that exercises the full env-var path. Lives in a separate test binary because mutating process env requiresunsafesince Rust 2024 and the workspace forbidsunsafe_code; the test binary opts out with#![allow(unsafe_code)].test_load_cache_invalid_magicto 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.rsmodule rustdoc updated to match.Migration impact
Existing visible
<file>.cachefiles left behind by earlier rustledger versions are silently removed on the nextrledger check(when a new cache is written). If a user has*.beancount.cachein their.gitignore, the entry becomes a no-op but harmless.bean-checkusers who were already settingBEANCOUNT_DISABLE_LOAD_CACHEorBEANCOUNT_LOAD_CACHE_FILENAMEfor their Python tooling now get the same behavior fromrledger.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
rledger check main.beancountwrites.main.beancount.cache(hidden), notmain.beancount.cache.BEANCOUNT_DISABLE_LOAD_CACHE=1 rledger check main.beancountwrites no cache.BEANCOUNT_LOAD_CACHE_FILENAME=~/.cache/rledger/{filename}.cache rledger check main.beancountwrites to~/.cache/rledger/main.beancount.cache.main.beancount.cachein the dir, runrledger check; the legacy file disappears, the new dotfile appears.Closes #939
🤖 Generated with Claude Code