Skip to content

Fix empty source window in CssSyntaxError for long lines without end position - #2184

Merged
ai merged 1 commit into
postcss:mainfrom
simpleqt:bugfix/error-long-line-window
Nov 2, 2026
Merged

ai merged 1 commit into
postcss:mainfrom
simpleqt:bugfix/error-long-line-window

Conversation

@simpleqt

@simpleqt simpleqt commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Problem

CssSyntaxError#showSourceCode() cuts error lines longer than 160 characters to a
20-character window around the error position (added in #1965 for minified CSS).
The window end is computed as:

let subLineEnd = Math.max(this.column + padding, this.endColumn + padding)

Errors thrown without an end position have endColumn === undefined. This is the
case for every error created through the two-argument Input#error(message, line|offset)
form, which includes all tokenizer errors (Unclosed string, Unclosed comment,
Unclosed bracket) and parser errors (Unclosed block, Missed semicolon).
For those errors this.endColumn + padding is NaN, Math.max returns NaN, and
line.slice(subLineStart, NaN) returns ''. The reported error.line/error.column
are still correct, but the rendered snippet shows an empty error line with a caret
pointing at nothing:

let e
try {
  postcss.parse('a{b:c}\n' + 'x'.repeat(300) + '{content:"\n')
} catch (err) { e = err }
console.log(e.showSourceCode(false))

Output on main:

CssSyntaxError: <css input>:2:310: Unclosed string

  1 | a{b:c}
> 2 |
    |                    ^
  3 |

Output with this fix:

  1 | a{b:c}
> 2 | xxxxxxxxxx{content:"
    |                    ^
  3 |

Errors with a range (for example Unknown word) keep working because endColumn
is a number there — the existing cut minified css tests only cover that case.

Root cause

The long-line windowing in lib/css-syntax-error.js assumes endColumn always
exists. It does not for errors created from a single position.

Fix

Fall back to the error column when endColumn is missing:

let endColumn = typeof this.endColumn === 'number' ? this.endColumn : this.column
let subLineStart = Math.max(0, this.column - padding)
let subLineEnd = Math.max(this.column + padding, endColumn + padding)

slice() already clamps to the line length, so the same code path now shows the last
20 columns before the error plus everything up to its end.

Reproduction and red/green evidence

New regression test cuts long line for error without range in
test/css-syntax-error.test.ts (css = 'a{b:c}\n' + 'x'.repeat(300) + '{content:"\n',
Unclosed string at 2:310, expects the windowed line xxxxxxxxxx{content:" and a
caret at column 310).

  • RED on clean main (2faedf9) + test only, second worktree:
    ./node_modules/.bin/uvu -r ts-node/register/transpile-only test css-syntax-error
    → Total: 26 / Passed: 25, actual > 2 | (empty window).
  • GREEN with the fix:
    • scoped: same command → Total: 26 / Passed: 26
    • full suite: pnpm unit → Total: 732 / Passed: 732
  • eslint lib/css-syntax-error.js test/css-syntax-error.test.ts → clean
  • check-dts → clean

Notes

Behavior for errors that have an end position is unchanged; the extra test locks the
no-range path that previously produced the empty window.

Disclosure: prepared and validated by an autonomous AI agent (simpleqt); the defect,
root cause, fix and test were reviewed by the submitting human operator.

Summary by CodeRabbit

  • Bug Fixes
    • Long-line syntax error snippets now display a focused excerpt when no end position is available, including the error marker and following line.

CssSyntaxError#showSourceCode() cuts lines longer than 160 characters
to a window around the error position. The window end was computed as
Math.max(column + padding, endColumn + padding), but errors thrown
without an end position (for example tokenizer errors like
"Unclosed string" or parser errors like "Unclosed block" and
"Missed semicolon") have undefined endColumn. The computation became
NaN and line.slice(subLineStart, NaN) returned an empty string, so the
output showed an empty error line with the caret pointing at nothing.

Fall back to the error column when endColumn is missing.

Signed-off-by: simpleqt <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 2, 2026 06:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a32b43c7-2d90-4742-9510-e88e53f99f5f
Commits

Reviewing files that changed from the base of the PR and between 2faedf9 and 9e05716.

Files selected for processing: 2
  • lib/css-syntax-error.js
  • test/css-syntax-error.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

For long source lines, showSourceCode uses the error column when endColumn is not numeric. A test covers an error without a range.

Changes: Syntax error excerpt rendering

Layer / File(s) Summary
Calculate and test the excerpt window
lib/css-syntax-error.js, test/css-syntax-error.test.ts
showSourceCode falls back to column when endColumn is not numeric. The test checks that an error without a range displays a truncated excerpt, the error marker, and the following line.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: ai

Merge Risk: Minimal · up to 9e057

The reported long-line rendering issue is addressed with focused regression coverage, and no merge-blocking risk is established.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: fixing an empty source window for long lines when CssSyntaxError lacks an end position.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@ai
ai merged commit 17e54b7 into postcss:main Nov 2, 2026
1 check passed
@ai

ai commented Nov 2, 2026

Copy link
Copy Markdown
Member

Thanks

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants