parser icon indicating copy to clipboard operation
parser copied to clipboard

Parser throws "unexpected token '(' ... valid tokens [)]" on nested parentheses in commit body

Open benjamineckstein opened this issue 5 months ago • 2 comments

Summary

parser() throws unexpected token '(' at L:C, valid tokens [)] when a commit body (or a scope) contains nested parentheses. The body of a conventional commit is free-form text per the spec, so it should not be rejected for containing nested (...). This commonly happens when a body quotes code, e.g. z.array(z.boolean()).

Environment

  • @conventional-commits/parser: 0.4.1 (current npm latest)
  • Node.js: v25.6.1

Minimal reproduction

import { parser } from '@conventional-commits/parser';

// nested parentheses in the body
parser('fix: x\n\ncall(inner(arg))');
// => Error: unexpected token '(' at 3:11, valid tokens [)]

// real-world: a commit body quoting code
parser('fix: x\n\nz.array(z.boolean())');
// => Error: unexpected token '(' at 3:18, valid tokens [)]

Expected vs actual

  • Expected: the message parses; the body is free-form text.
  • Actual: parsing throws on the inner ( because the grammar appears to treat (...) in the body as a non-nestable group and expects ) where it finds (.

Characterization

It is specifically nested parens, and position matters (which points at a grammar issue rather than the input being genuinely invalid):

Input (after fix: x\n\n unless noted) Result
call(arg) — single parens PASS
call(inner(arg)) — nested FAIL 3:11
(()) — empty nested PASS
z.array(z.boolean()) FAIL 3:18
z.array(z.boolean()) done — trailing text FAIL
see z.array(z.boolean()) — leading text PASS
a(1) b(2) — two separate groups PASS
fix: call(inner(arg)) — nested in subject description PASS
fix(a(b)): x — nested in scope FAIL 1:6

Downstream impact

This surfaces through release-please, which depends on this parser. When a commit that landed on the default branch has a body quoting code with nested parens (e.g. z.array(z.boolean())), release-please logs commit could not be parsed ... error message: Error: unexpected token '(' ..., silently skips that commit, finds zero releasable commits, and opens no release PR. The fix never ships and there is no failing check to signal it, so the breakage is easy to miss.

Happy to help with a fix / test case if useful.

benjamineckstein avatar Jun 22 '26 06:06 benjamineckstein

I believe I ran into the same bug today, and I have linked the exact commit that was the victim of the bug. https://github.com/googleapis/google-cloud-go/commit/3666568e7d16aacb1a3acacac735719fd259b2ee

codyoss avatar Jul 10 '26 17:07 codyoss

Hit this too, and since the thread doesn't yet pin down when it fires, here's what I found narrowing it. Reproduced on @conventional-commits/[email protected] through [email protected] (the version release-please-action@v5 ships).

The trigger is narrower than "nested parentheses in the body"

A body line only breaks the parse when it begins with a run of non-whitespace followed by (, and then contains another ( before the matching ). The line-initial word( is what puts the scanner into a scope, and the second paren is illegal there.

Anything before the opening paren keeps the scanner out of that state, so the same characters parse fine one column to the right:

DROPPED  foo(bar(1))                     at column 0
DROPPED  foo(1                           unterminated
DROPPED  /\/+$/.exec("a" + "/".repeat(1)) at column 0
DROPPED  `foo(bar(1))`                   inline code span
DROPPED  ```js\nfoo(bar(1))\n```         inside a fence

kept       foo(bar(1))                   indented one space
kept     x = foo(bar(1))                 anything at all before the paren
kept     a (b (c))                       space between word and paren
kept     foo(1)   foo("a")   foo(50_000) single, balanced
kept     fix: call foo(bar(1))           in the subject, parsed as the header

Two consequences worth calling out:

Markdown fences give no protection. The parser doesn't read markdown, so wrapping a code sample in ``` changes nothing. That's why this keeps surfacing through Renovate PR bodies and squash merges, where code samples land at column 0 inside fences. It also explains why googleapis/release-please#2020, filed as "ignores commits which contain fenced codeblocks" and closed as completed, kept being reported afterwards: the fence was never the variable.

There is a workaround available today. Indent the offending line by a single space. That is the entire fix, and it needs no change here.

What it costs downstream

release-please catches the throw, logs commit could not be parsed at debug level, and drops the whole commit from the changelog. The workflow exits 0 and every check stays green, so the only symptom is an entry that silently isn't there. In our repo one commit went missing from two consecutive release PRs before anyone noticed.

Because it's silent, we ended up adding a pre-merge check that scans a PR title and body for the pattern and fails the build. Happy to share it if it's useful to anyone here, though obviously the real fix is for the grammar to stop treating a line-initial word( in the body as a scope.

Related: googleapis/release-please#2564, and #48 here, which added a failing test for the same class in March 2024.

svyatov avatar Jul 21 '26 16:07 svyatov