Repository navigation
fix(parser): reject tags and links on directives that do not take them - #1953
Conversation
beancount allows `#tag` / `^link` on TRANSACTIONS, and in v3 on `note` and `document` - nowhere else. rledger accepted them everywhere, silently: each `convert_*` reads the fields it wants and ignores the rest, so a trailing token was never objected to by anything. 2018-06-01 open Assets:A #tag loaded clean here, parse error there Seven directives now object - open, close, balance, commodity, event, price, pad - and `note` / `document` continue to accept, because they should. WHY THIS IS NOT ONE RULE IN THE DISPATCHER The obvious fix is "reject trailing tokens on non-transaction directives", and it is wrong: `note` and `document` genuinely take tags and links in beancount v3, and we already agreed with beancount on both. A blanket rule would have broken the two cases that were already right. Hence a per-directive call. The check scans DIRECT child tokens only. Metadata lives in child NODES and a metadata VALUE may legitimately be a tag (`k: #x`), so a descendant walk would reject valid input - the opposite mistake and the worse one. Both directions are pinned, and I verified each fails under its own sabotage: disabling the check fails the reject half, widening it to `descendants_with_tokens` fails the accept half. CORRECTING THE ISSUE'S OWN EVIDENCE #1949 originally claimed we were inconsistent with ourselves - that `close` rejected what `open` accepted. That was confounded and I have corrected the issue. Three of its four probes measured something else: the `close` rejection was E1001 for an unopened account, `pad` rejected with E2003 even with no trailing token, and `document` rejected only because the referenced file did not exist. With clean controls, rledger accepted the trailing token on every one of them. Six divergent directives, not one, and no internal inconsistency. VERIFIED AGAINST BEANCOUNT, PER DIRECTIVE Every case has a control confirming both tools accept the directive WITHOUT the trailing token, so nothing below is measuring an unrelated error. After this change every tested directive agrees with beancount in both directions. `pad` could not be tested through a clean control (beancount reports PadError on the control either way), so it was verified separately: beancount gives ParserSyntaxError for the tag and PadError without it - different errors, so the tag is genuinely rejected. CORPUS One baseline drifted: `parser-lima/examples_data_full.beancount`, which really does contain `commodity NZD #downunder ^money.co.nz`, `open ... #money ^www.bank.com` and `close ... #seeya`. beancount already reports 9 "unexpected TAG/LINK" errors on it; we now report 10. The extra one is granularity - we flag both the tag and the link on a line where beancount stops at the first - not a difference in accept/reject. Updated surgically. Caches: loader 20 -> 21, wasm 6 -> 7. A file that previously loaded clean can now carry errors, so a stale cache would serve the old parse. SEPARATE, PRE-EXISTING, NOT FIXED HERE A LINK as a metadata VALUE (`ref: ^inv-1`) is accepted by us and rejected by beancount. Confirmed pre-existing against a pre-change binary. A tag in the same position is accepted by both. Left alone deliberately - it is a different rule in a different place, and folding it in would make this change harder to review. Closes #1949.
There was a problem hiding this comment.
Pull request overview
Aligns rustledger’s parser with beancount’s rules for where #tag / ^link are valid by emitting syntax errors on directives that do not accept them, while preserving v3 behavior for note and document. This closes a silent-acceptance hole where extra trailing tokens were previously ignored by directive converters.
Changes:
- Add a per-directive CST conversion check to reject
TAG/LINKtokens onopen,close,balance,commodity,event,price, andpad. - Add integration tests covering reject/accept behavior (including ensuring tag-valued metadata still parses).
- Bump loader + wasm cache versions and update the parser corpus manifest baseline to reflect new error output.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/baselines/parser-corpus.manifest | Updates corpus hash to reflect the new parser error behavior on trailing tags/links. |
| crates/rustledger-wasm/src/cache.rs | Bumps wasm parsed-ledger cache version to invalidate caches affected by the parser behavior change. |
| crates/rustledger-parser/tests/parser_integration_test.rs | Adds integration tests asserting rejection only where beancount rejects (and acceptance for note/document + metadata-value tag case). |
| crates/rustledger-parser/src/cst/convert.rs | Implements reject_tags_and_links and applies it to the affected directive converters. |
| crates/rustledger-loader/src/cache.rs | Bumps loader cache version (and fixture version constants) to invalidate caches impacted by new parse errors. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📊 Benchmark Results
vs main branch (2026-08-04)
What do these numbers mean?
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…e directive Found reviewing my own PR. beancount treats a trailing tag as a parser syntax error, so the directive never exists and later references cascade: beancount ParserSyntaxError + ValidationError: unknown account Assets:N rledger the tag error alone; the account is still opened Both reject the file, so this is a divergence in the error SET rather than in accept/reject. Keeping the directive gives the user one error naming the real problem instead of that error plus a cascade of unopened-account noise pointing at innocent lines. Worth writing down specifically because the compat oracle CANNOT catch it - its error axis compares only whether a file errs, not which errors - so nothing would surface the choice later.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Suppressed comments (2)
crates/rustledger-parser/src/cst/convert.rs:449
- The new error message claims tags and links “belong to transactions, and to note and document directives”, but tags are also valid in
pushtag/poptagdirectives (seespec/core/grammar.peg). Consider rewording so it doesn’t incorrectly suggest only those directives can contain tags.
crate::ParseErrorKind::SyntaxError(format!(
"a {directive} directive does not take a {what} ({}); \
tags and links belong to transactions, and to note and \
document directives",
t.text()
crates/rustledger-parser/src/cst/convert.rs:392
- The doc comment says tags/links are allowed on transactions and (v3) note/document “— nowhere else”, but this repo’s grammar and AST include
pushtag/poptagdirectives that take a#tagtoken (e.g.spec/core/grammar.peg). Reword to avoid implying tags never appear on other directives.
This issue also appears on line 445 of the same file.
/// beancount allows tags and links on TRANSACTIONS, and in v3 on `note` and
/// `document` — nowhere else. rledger accepted them everywhere, silently: each
/// `convert_*` reads the fields it wants and ignores the rest, so a trailing
/// token was never objected to by anything. `2018-06-01 open Assets:A #tag`
/// loaded clean here and is a parse error there (#1949).
|
| Metric | Result |
|---|---|
| Check Compatibility | 100% |
| BQL Compatibility | 95% (raw 89%, 162 masked) |
| Full AST Match | 100% |
| Total Files | 812 |
What's tested
- Check: Exit codes and error counts match
bean-check - BQL: Query results match
bean-query
The headline is the EFFECTIVE rate, after pairs where beanquery
itself is wrong are masked. raw is the same run with nothing
masked. A widening gap means the registry grew, not that rledger
changed. - Files: ~800 test files downloaded from 10+ beancount repositories
Full results available in workflow artifacts
Two review points. The message read "a open directive does not take a tag" - wrong article for open, event and every other vowel-initial directive name. Dropped the article entirely rather than special-casing: "the open directive does not take a tag" reads correctly for all seven. And clippy failed in CI on the doc comment I added while REVIEWING this PR: doc_markdown wanted backticks on the type names, and a line beginning with a dash was read as a doc list item. I had run clippy before writing that comment and did not re-run it after - the whole workspace at 1.97.0 is clean now, which is how it should have been checked the first time.
The corpus baseline hashes parser output including error TEXT, so changing "a open directive" to "the open directive" moved the hash for the one file that exercises it. Same file, same accept/reject, different wording. I committed the wording change with this test red - it was caught by the next full run rather than before the push, which is the check I should have done first.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Suppressed comments (2)
crates/rustledger-parser/tests/parser_integration_test.rs:1261
- The rejection assertions only check that some parse error occurred. This can produce false positives (e.g., if a different syntax/date error is introduced, the test will still pass even if trailing-tag/link rejection regresses). It would be more robust to assert that a
SyntaxErrormentioning the unexpected#tag/^lnkis present.
let parsed = rustledger_parser::parse(&format!("{pre}{body}"));
assert!(
!parsed.errors.is_empty(),
"{what}: must be a parse error (beancount rejects it)",
);
crates/rustledger-parser/src/cst/convert.rs:392
- The PR description/title says tags/links are only valid on transactions (and on
note/document), but this helper is only invoked for 7 directives. Other directives in this file (e.g.query, and the top-level keyword directives likeoption/include/plugin) still appear to ignore trailing#tag/^linktokens, which would leave the original silent-accept behavior in place for those directive types.
/// Reject `#tag` / `^link` tokens on a directive that does not take them.
///
/// beancount allows tags and links on TRANSACTIONS, and in v3 on `note` and
/// `document` — nowhere else. rledger accepted them everywhere, silently: each
/// `convert_*` reads the fields it wants and ignores the rest, so a trailing
/// token was never objected to by anything. `2018-06-01 open Assets:A #tag`
/// loaded clean here and is a parse error there (#1949).
`ref: ^inv-1` parsed here and is a syntax error in beancount - its grammar has no production for a link in value position at all. A `#tag` in the same place IS valid there, and we already agreed on that. THE ASYMMETRY IS THE WHOLE POINT Tags and links lex as sibling kinds and are handled as a pair almost everywhere in this file, so rejecting both is the natural reach and the wrong answer - it would break input beancount accepts. Same mistake #1953 avoided in the mirror-image case, where `note` and `document` legitimately take both. SCOPE `META_ENTRY` nodes only. A link on a TRANSACTION is a direct child of the transaction, not of a metadata entry, so it is untouched - that being the one place links belong. NOT extended to `custom` / `pushmeta`, which run through `value_tokens_to_meta` rather than this path. beancount rejects BOTH a tag and a link there, so it is a different rule needing its own evidence. Measured but deliberately left: filing separately rather than folding a second rule into this change. The scan lives in `parse_via_cst_inner`, which both the green and red paths share, so there is no dual-path divergence to mirror. Guarded by `stripped.contains('^')`, the same trick the adjacent unclosed-cost-brace scan uses: no caret in the source means no LINK token can exist. THE FIRST VERSION OF THE TEST WAS VACUOUS Sabotaging the scan to reject TAG as well still passed. The reason is that guard: the tag fixture contained no `^`, so the scan never ran, and the "a tag must still be accepted" assertion said nothing about the scan's contents. The accept case now uses a file that DOES contain a link elsewhere, so the scan runs and the tag has to survive it. Both sabotages are caught now - disabling the scan, and widening it to tags. CORPUS One baseline drifted: `parser-lima/examples_data_full.beancount`, which carries `key-for-link: ^mylink` at line 84. beancount reports exactly one "unexpected LINK" error on that file and so do we now. Updated surgically. Caches: loader 21 -> 22, wasm 7 -> 8. Closes #1954.
* fix(parser): a link is not a valid metadata value `ref: ^inv-1` parsed here and is a syntax error in beancount - its grammar has no production for a link in value position at all. A `#tag` in the same place IS valid there, and we already agreed on that. THE ASYMMETRY IS THE WHOLE POINT Tags and links lex as sibling kinds and are handled as a pair almost everywhere in this file, so rejecting both is the natural reach and the wrong answer - it would break input beancount accepts. Same mistake #1953 avoided in the mirror-image case, where `note` and `document` legitimately take both. SCOPE `META_ENTRY` nodes only. A link on a TRANSACTION is a direct child of the transaction, not of a metadata entry, so it is untouched - that being the one place links belong. NOT extended to `custom` / `pushmeta`, which run through `value_tokens_to_meta` rather than this path. beancount rejects BOTH a tag and a link there, so it is a different rule needing its own evidence. Measured but deliberately left: filing separately rather than folding a second rule into this change. The scan lives in `parse_via_cst_inner`, which both the green and red paths share, so there is no dual-path divergence to mirror. Guarded by `stripped.contains('^')`, the same trick the adjacent unclosed-cost-brace scan uses: no caret in the source means no LINK token can exist. THE FIRST VERSION OF THE TEST WAS VACUOUS Sabotaging the scan to reject TAG as well still passed. The reason is that guard: the tag fixture contained no `^`, so the scan never ran, and the "a tag must still be accepted" assertion said nothing about the scan's contents. The accept case now uses a file that DOES contain a link elsewhere, so the scan runs and the tag has to survive it. Both sabotages are caught now - disabling the scan, and widening it to tags. CORPUS One baseline drifted: `parser-lima/examples_data_full.beancount`, which carries `key-for-link: ^mylink` at line 84. beancount reports exactly one "unexpected LINK" error on that file and so do we now. Updated surgically. Caches: loader 21 -> 22, wasm 7 -> 8. Closes #1954. * fix(parser): repair the rebase and keep the cost-spec rustdoc with its function Two fixes, both mine. The new scan was inserted directly above , which put it BETWEEN that function and its doc comment: the cost-spec rustdoc then documented the link scan, and the cost-spec helper had none. Anchoring an insertion on a line orphans whatever rustdoc sits above it, and nothing in the build or tests notices. And resolving the rebase conflict in the test file, I cut through the middle of the #1955 test and dropped its closing brace - the conflict boundary sat inside the function, not between the two. Caught by clippy reporting an unclosed delimiter, not by me reading the merge. Both tests are present and the file has 63 passing again.
…1959) Two rules, not one, and that is the whole difficulty: position #tag ^link metadata value valid rejected (fixed in #1954) pushmeta k: ... valid rejected custom "t" ... rejected rejected `pushmeta` follows the METADATA rule - it pushes a metadata key/value, so a tag is fine and only a link is not. `custom` is stricter than both and takes neither. Verified per position against beancount, each with a control on the same directive (custom with string/number/bool, date, account; pushmeta with a number). WHY IT IS NOT ONE CHECK `value_tokens_to_meta` serves BOTH callers - `extract_custom_values` and `pushmeta_value`. A single check inside that shared helper would either let a tag through in `custom` or wrongly reject one in `pushmeta`. The scan therefore keys on the DIRECTIVE node kind and carries a per-kind flag. Third time in this sequence the correct answer has split a pair that lexes and reads as one: #1953 (note/document DO take both), #1954 (a tag is valid where a link is not), and now this. The test makes the collapse fail: sabotaging the pushmeta flag to `true` fails on `pushmeta: tag`. Lives in `parse_via_cst_inner` alongside the #1954 scan, so green and red share it and there is no dual-path mirror to keep in sync. A TEST WAS PINNING THE DIVERGENCE `test_custom_directive_preserves_sign_and_tag_link` asserted that a custom directive CARRIES Tag/Link values. beancount rejects that exact fixture with `syntax error, unexpected TAG` and produces no directive at all - checked before the expectation was touched. The test had two separable claims. The MINUS-sign regression it was written for (`-50.00` once emitted `+50.00`) is real and kept, with a tag-free fixture that beancount accepts. Only the tag/link half is removed, and the reasoning is recorded in place. CORPUS One baseline drifted: `parser-lima/examples_data_custom.beancount`, whose first line packs every value kind including `#tag1 ^link1`. Both tools rejected that file before AND after - beancount stops at an earlier `unexpected CURRENCY`, which we already objected to as well - so only our error set grew, by the two new objections. Updated surgically. Caches: loader 23 -> 24, wasm 9 -> 10. Closes #1958.
beancount allows
#tag/^linkon transactions, and in v3 onnoteanddocument— nowhere else. rledger accepted them everywhere, silently: eachconvert_*reads the fields it wants and ignores the rest, so a trailing token was never objected to by anything.Seven directives now object —
open,close,balance,commodity,event,price,pad— andnote/documentcontinue to accept, because they should.Why this isn't one rule in the dispatcher
The obvious fix is "reject trailing tokens on non-transaction directives," and it's wrong:
noteanddocumentgenuinely take tags and links in beancount v3, and we already agreed with beancount on both. A blanket rule would have broken the two cases that were already right. Hence a per-directive call.The check scans direct child tokens only. Metadata lives in child nodes and a metadata value may legitimately be a tag (
k: #x), so a descendant walk would reject valid input — the opposite mistake and the worse one.Both directions are pinned, and each verified to fail under its own sabotage:
Correcting the issue's own evidence
#1949 originally claimed we were inconsistent with ourselves — that
closerejected whatopenaccepted. That was confounded, and I've corrected the issue. Three of its four probes measured something else: thecloserejection was E1001 for an unopened account,padrejected with E2003 even with no trailing token, anddocumentrejected only because the referenced file didn't exist. With clean controls, rledger accepted the trailing token on every one. Six divergent directives, not one, and no internal inconsistency.Verified against beancount, per directive
Every case has a control confirming both tools accept the directive without the trailing token, so nothing is measuring an unrelated error. After this change every tested directive agrees with beancount in both directions.
padcouldn't be tested through a clean control (beancount reportsPadErroreither way), so it was verified separately:ParserSyntaxErrorwith the tag vsPadErrorwithout — different errors, so the tag is genuinely rejected.Corpus
One baseline drifted:
parser-lima/examples_data_full.beancount, which really does containcommodity NZD #downunder ^money.co.nz,open ... #money ^www.bank.comandclose ... #seeya. beancount already reports 9 "unexpected TAG/LINK" errors on it; we now report 10. The extra one is granularity — we flag both the tag and the link on a line where beancount stops at the first — not a difference in accept/reject. Updated surgically.Caches: loader 20 → 21, wasm 6 → 7. A file that previously loaded clean can now carry errors, so a stale cache would serve the old parse.
Separate, pre-existing, not fixed here
A link as a metadata value (
ref: ^inv-1) is accepted by us and rejected by beancount. Confirmed pre-existing against a pre-change binary; a tag in the same position is accepted by both. Left alone deliberately — different rule, different place, and folding it in would make this harder to review.Closes #1949.