Skip to content

Fix format producing undefined edits at 200 indent characters - #152

Open
Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
microsoft:mainfrom
guru-bharadwaj20:format-deep-indent
Open

Guru R Bharadwaj (guru-bharadwaj20) wants to merge 1 commit into
microsoft:mainfrom
guru-bharadwaj20:format-deep-indent

Conversation

@guru-bharadwaj20

Copy link
Copy Markdown

Fixes microsoft/vscode-json-languageservice#262

format() can return edits whose content is undefined. Anything that applies them then throws, which is the Cannot read properties of undefined (reading 'length') in applyEdits from the issue.

The cause is an off-by-one in newLinesAndIndent(). The cached line break strings have 200 entries (indexes 0 to 199), but the fallback to repeat() only kicked in when amountOfSpaces > length. With exactly 200 indent characters the lookup read index 200 and got undefined. That is nesting level 100 with tabSize: 2, level 50 with tabSize: 4, or level 200 with tabs. I changed the check to >=.

I added a formatter test that formats 250 nested arrays with spaces and with tabs. It fails without the change and passes with it.

What I ran:

  • npm test (compile, eslint, node tests): 89 tests pass.
  • I ran the tmp.json file attached to the issue through format(). Before the change, with tabSize: 2 and spaces, 307 of the 11404 edits had undefined content. After the change none do for tabSize 2 or 4 with spaces or for tabs, and the formatted output parses to the same value as the input.

newLinesAndIndent() looked up cachedBreakLinesWithSpaces[...][amountOfSpaces] when amountOfSpaces was equal to the cache length, which is one past the last entry. The edit content was then undefined, and applying the edits fails. This happens with deeply nested documents, for example at nesting level 100 with two-space indentation.
Copilot AI balanced review requested due to automatic review settings November 28, 2026 16:57

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.

🟢 Approval recommended

No active review issues are reported for the indentation-cache boundary fix.

No open issues 🎉

What changed in this PR

Fixes an off-by-one error in the formatter’s indentation cache that could produce edits with undefined content at exactly 200 indentation characters.

Changes:

  • Uses the fallback when indentation reaches or exceeds the cache length.
  • Adds a regression test for 250 nested arrays using both spaces and tabs.
File Description
src/​test/​format.test.ts Tests deeply nested formatting at and beyond the cache boundary.
src/​impl/​format.ts Changes the cache boundary check from > to >=.

Review effort: Balanced

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.

[Bug report] Cannot format large json document

2 participants