Skip to content

fix(commit-parser): fall back to header-only parsing when body is unparseable#2790

Open
narhen wants to merge 1 commit into
googleapis:mainfrom
narhen:fix-header-only-fallback
Open

fix(commit-parser): fall back to header-only parsing when body is unparseable#2790
narhen wants to merge 1 commit into
googleapis:mainfrom
narhen:fix-header-only-fallback

Conversation

@narhen
Copy link
Copy Markdown

@narhen narhen commented May 28, 2026

Fixes #2564 🦕

Problem

parseConventionalCommits silently drops commits whose body trips the @conventional-commits/parser grammar. The grammar requires that any ( on a body line have its matching ) on the same line. GitHub's PR-body line-wrap routinely produces commits that violate this — e.g. a function call whose closing paren ends up on the next line:

Body wraps a call:
computeValue(longArgumentName
= true)

This throws unexpected token '\n' at LINE:COL, valid tokens [)] from inside the parser's scope() rule. The header itself parses fine; only the body is broken.

The existing catch in parseConventionalCommits (src/commit.ts) logs at debug level and skips the commit, so the entry never reaches the release PR's changelog. #2564 is the canonical bug report for this symptom.

Root cause

Upstream in @conventional-commits/parser, scope() throws on a newline before ) rather than returning a recoverable error. An open PR (conventional-commits/parser#48) targets this exact case but has been stalled since March 2024; the parser has not had an npm release since January 2021. We can't realistically wait for an upstream fix.

Fix

When full-message parsing throws, retry with just the first line of the commit message. If the header parses, push it as a ConventionalCommit and log a warn so users know the body was discarded. If the header also fails to parse, fall through to the existing debug log and skip the commit (unchanged behavior).

Preserved on the fallback path: type, scope, bareMessage, breaking (via ! in the header), references from the header, pullRequest, sha, files.

Lost on the fallback path: body-derived BREAKING CHANGE notes, body-derived references, footer trailers. The user-visible improvement is "commit appears in the changelog" rather than "commit silently vanishes."

Test

Added a parseConventionalCommits test with a body that wraps a function call across lines. Without the fix it returns []; with the fix it returns one parsed commit with the correct type/scope/subject.

Checklist

  • Tests and linter pass locally (npm test: 1101 passing, 0 failing; npm run lint: no new errors)
  • Code coverage does not decrease (added test exercises the new branch)
  • No doc changes required (behavior change is internal recovery path)
  • CLA signed — pending; will follow up

…arseable

When the @conventional-commits/parser grammar throws on a commit body
(commonly an unbalanced `(` from GitHub's line-wrap of a PR body),
parseConventionalCommits currently logs at debug and skips the commit
entirely, so the entry never reaches the release PR changelog.

Retry the parse with just the first line; if the header parses, push it
as a ConventionalCommit and log a warn. If the header also fails, fall
through to the existing debug log.

Preserved on the fallback path: type, scope, bareMessage, header-level
breaking marker, header references, pullRequest, sha, files. Lost:
body-derived BREAKING CHANGE notes, body references, footer trailers.

Refs googleapis#2564
@narhen narhen requested review from a team as code owners May 28, 2026 10:22
@product-auto-label product-auto-label Bot added the size: m Pull request size is medium. label May 28, 2026
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 28, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

commit could not be parsed: ... unexpected token '('

2 participants