fix: handle CRLF line endings in code frame positions - #23219
Open
nyxst4ck wants to merge 1 commit into
Open
Conversation
`posToNumber` and `generateCodeFrame` split the source with `splitRE` (`/\r?\n/g`) but then assumed every line terminator was a single character when converting back to character offsets. For a source using CRLF, the computed offset drifts by one character per preceding line, so an error reported with a character offset (as done by the SSR transform, terser, import analysis and the plugin container) is highlighted on the wrong line, or produces an empty frame entirely when the drift pushes the offset past the last line. Use the actual terminator length at the current offset instead. This also makes `posToNumber` round-trip with `numberToPos`, which already handled CRLF correctly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
posToNumber()andgenerateCodeFrame()split source text with/\r?\n/g, but added one character for every line terminator when converting line/column data back to absolute offsets. With CRLF input, the offset therefore drifted by one character for every preceding line, which could underline the wrong range or produce an empty frame.This change accounts for the actual terminator width at each source offset. It keeps LF behavior unchanged and makes
posToNumber()consistent withnumberToPos(), which already handles CRLF.The regression coverage checks:
Verification:
pnpm vitest run packages/vite/src/node/__tests__/utils.spec.ts— 123 passed.pnpm exec eslint packages/vite/src/node/utils.ts packages/vite/src/node/__tests__/utils.spec.ts— passed.pnpm --filter vite typecheck— passed across the package TypeScript projects.No public API or documentation changes are required; this only corrects source-position accounting for an existing supported line-ending form.