Skip to content

fix(lint): trim trailing whitespace off the message handed to ignore matchers - #4960

Merged
escapedcat merged 2 commits into
conventional-changelog:masterfrom
dylanpulver:fix/trim-edit-commit-before-ignores
Aug 21, 2026
Merged

escapedcat merged 2 commits into
conventional-changelog:masterfrom
dylanpulver:fix/trim-edit-commit-before-ignores

Conversation

@dylanpulver

Copy link
Copy Markdown
Contributor

Description

@commitlint/lint hands the raw message to the ignore matchers, and a message read from a file or
from git history still carries its trailing newlines. An ignores matcher that compares the message
by value therefore never matches, and the commit gets linted anyway.

@commitlint/read appends a newline to the contents of the commit message file in
get-edit-commit.ts, and git has already put one there, so a commit-msg hook hands
"Initialize project using Create React App\n\n" to the matcher. Commits read from history arrive
the same way, and a piped message keeps the newline the shell adds. This is the case @escapedcat
reproduced in the issue thread, where the matcher printed STARThello\n\nEND and returned false.

Where the fix goes

The trim happens in @commitlint/lint, on the value passed to isIgnored, and nowhere else.

Trimming in @commitlint/read/src/get-edit-commit.ts looked like the smaller change but it is the
wrong place. It changes the string every consumer of that package sees, and a commit message is
newline significant. parsed.raw carries the message through to the rules, and body-leading-blank
and footer-leading-blank index into its lines while signed-off-by and trailer-exists read it
too. Linting "\nfeat: thing\n\nbody\n\n" under body-leading-blank fails today and starts passing
if the read layer trims, which is a rule quietly changing its answer. Trimming before the ignore
check leaves the message that reaches the parser byte for byte identical, so no rule can move. It
also covers every input source at once, because they all reach this same call.

Only the end of the message is trimmed. The default matchers in @commitlint/is-ignored are
anchored to the start of the message, so trimming the start would make them match messages they do
not match today, and a linter that begins skipping commits it used to check is the worse failure.
Running the last 600 commit messages of this repository through isIgnored with a blank first line
added shows the size of that: trimming the end changes 0 of the 600 ignore decisions, trimming both
ends changes 28 of them. Those 28 are release commits and reverts that would silently stop being
linted. @commitlint/is-ignored itself is untouched, so anyone calling it directly keeps the
behavior they have.

Motivation and Context

Fixes #3162

The repository's own documentation contradicts the current behavior. docs/reference/configuration.md
presents ignores: [(commit) => commit === ""] as the worked example of the option, and no message
read through --edit or through --from/--to can ever equal "". The existing test in
@commitlint/cli/src/cli.test.ts passes only because its fixture uses commit.includes("WIP")
rather than a comparison by value, which is what hid this.

One clarification on the example in the issue title. A truly empty --edit message never reaches
lint() at all, because cli.ts drops messages that are blank after trimming before it lints. The
reachable form of the bug is the one above, where a matcher compares against real message text.

Usage examples

// commitlint.config.js
export default {
  rules: { "type-empty": [2, "never"], "subject-empty": [2, "never"] },
  ignores: [(commit) => commit === "Initialize project using Create React App"],
};
printf 'Initialize project using Create React App\n' > .git/COMMIT_EDITMSG
npx commitlint --edit .git/COMMIT_EDITMSG

# before: the matcher receives "Initialize project using Create React App\n\n",
#         the commit is linted, and the run exits 1 on type-empty and subject-empty
# after:  the matcher receives "Initialize project using Create React App",
#         the commit is skipped, and the run exits 0

How Has This Been Tested?

Unit tests in @commitlint/lint/src/lint.test.ts cover a matcher comparing by value against a
message with trailing newlines, the exact value a matcher receives, the documented empty-message
matcher firing on a whitespace-only message, leading whitespace being preserved, a semver message
behind a blank first line still being linted, and a multi-line message with a body and a footer
producing the same result with and without trailing newlines.

End-to-end tests in @commitlint/cli/src/cli.test.ts cover the --edit path and the stdin path
against a new fixtures/ignores-exact fixture whose matcher compares by value, plus the negative
case where the message does not match.

On the test commit alone, five of the new lint tests and two of the new cli tests fail on assertions
rather than on build or import errors, so the coverage is not vacuous.

Regression check against real messages: the last 600 commit messages of this repository were shaped
the way @commitlint/read hands them over, then linted against @commitlint/config-conventional on
master and on this branch. All 600 produce identical outcomes, with the same 6 invalid messages and
the same reported output on both sides.

What I ran locally on Node 22 and pnpm 11:

  • pnpm build
  • pnpm test, the full suite, 91 files and 1227 tests, no type errors
  • npx oxlint and npx oxfmt --check over the changed files, both clean
  • npx commitlint --from 013c3582 --to HEAD --verbose, 0 problems on both commits

One note on the last two. pnpm lint and pnpm format at the repository root also walk an
untracked .cache/ directory that corepack drops in the checkout, and they fail on master for the
same reason, so I ran oxlint and oxfmt against the changed files instead.

One thing left alone

lint("\n\n") still rejects with Expected a raw commit from the parser, because the guard above
parse checks message === "" and a whitespace-only message does not hit it. The CLI never reaches
that path since it drops blank messages before linting, and tightening the guard would change the
error that lint() throws when it is called with no arguments. That looked like its own change.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have verified that any documentation examples I added/changed actually work.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • For a feature/bug fix, my commits follow the test-driven flow: a failing-test commit, then the
    implementation.

No documentation change is needed here. The ignores example in docs/reference/configuration.md
already describes what the option is meant to do, and this change makes that description true, so I
verified the example works rather than editing it.

@greptile-apps

greptile-apps Bot commented Aug 20, 2026

Copy link
Copy Markdown

Confidence Score: 5/5

The PR appears safe to merge with no actionable defects identified.

The normalization is confined to ignore predicate evaluation, while parsing, rules, and reported input continue to receive the original message; the added unit and CLI tests cover the relevant positive, negative, and regression paths.

Important Files Changed

Filename Overview
@commitlint/lint/src/lint.ts Trims trailing whitespace only for ignore matching and leaves the original message unchanged for all subsequent parsing and reporting.
@commitlint/lint/src/lint.test.ts Thoroughly covers normalized matcher input, preserved leading whitespace and raw messages, whitespace-only matching, and rule-evaluation regressions.
@commitlint/cli/src/cli.test.ts Verifies exact ignore matching through edit and stdin paths, including a nonmatching negative case.
@commitlint/cli/fixtures/ignores-exact/commitlint.config.js Provides a focused strict-equality ignore fixture backed by intentionally failing lint rules.

Reviews (1): Last reviewed commit: "fix(lint): trim trailing whitespace off ..." | Re-trigger Greptile

@escapedcat
escapedcat added this pull request to the merge queue Aug 21, 2026
@escapedcat

Copy link
Copy Markdown
Member

Thanks

Merged via the queue into conventional-changelog:master with commit a6f279b Aug 21, 2026
12 checks passed
sys-support pushed a commit to 3caravelle/renovate that referenced this pull request Sep 21, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [@commitlint/cli](https://commitlint.js.org/) ([source](https://github.com/conventional-changelog/commitlint/tree/HEAD/@commitlint/cli)) | [`21.2.2` → `21.2.3`](https://renovatebot.com/diffs/npm/@commitlint%2fcli/21.2.2/21.2.3) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@commitlint%2fcli/21.2.3?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@commitlint%2fcli/21.2.2/21.2.3?slim=true) |
| [@commitlint/config-conventional](https://commitlint.js.org/) ([source](https://github.com/conventional-changelog/commitlint/tree/HEAD/@commitlint/config-conventional)) | [`21.2.2` → `21.2.3`](https://renovatebot.com/diffs/npm/@commitlint%2fconfig-conventional/21.2.2/21.2.3) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@commitlint%2fconfig-conventional/21.2.3?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@commitlint%2fconfig-conventional/21.2.2/21.2.3?slim=true) |

---

### Release Notes

<details>
<summary>conventional-changelog/commitlint (@&#8203;commitlint/cli)</summary>

### [`v21.2.3`](https://github.com/conventional-changelog/commitlint/blob/HEAD/@&#8203;commitlint/cli/CHANGELOG.md#2123-2026-09-19)

[Compare Source](conventional-changelog/commitlint@v21.2.2...v21.2.3)

##### Bug Fixes

- **lint:** trim trailing whitespace off the message handed to ignore matchers ([#&#8203;4960](conventional-changelog/commitlint#4960)) ([a6f279b](conventional-changelog/commitlint@a6f279b))

</details>

<details>
<summary>conventional-changelog/commitlint (@&#8203;commitlint/config-conventional)</summary>

### [`v21.2.3`](https://github.com/conventional-changelog/commitlint/blob/HEAD/@&#8203;commitlint/config-conventional/CHANGELOG.md#2123-2026-09-19)

[Compare Source](conventional-changelog/commitlint@v21.2.2...v21.2.3)

##### Bug Fixes

- **rules:** report the case that matched in case rule failure messages ([#&#8203;4962](conventional-changelog/commitlint#4962)) ([9f5f7bc](conventional-changelog/commitlint@9f5f7bc))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi44MS4zIiwidXBkYXRlZEluVmVyIjoiNDIuODEuMyIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Reviewed-on: https://git.3caravelle.net/3Caravelle/renovate/pulls/59
Co-authored-by: Renovate Bot <renovate-bot@3caravelle.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

"ignores" commit is not trimmed

2 participants