Skip to content

util: honor breakLength: Infinity for deep objects and arrays - #66471

Open
RohithPariki wants to merge 1 commit into
nodejs:mainfrom
RohithPariki:fix/util-inspect-breaklength-infinity
Open

RohithPariki wants to merge 1 commit into
nodejs:mainfrom
RohithPariki:fix/util-inspect-breaklength-infinity

Conversation

@RohithPariki

@RohithPariki RohithPariki commented Oct 3, 2026 •

Copy link
Copy Markdown

Problem

When using util.inspect(obj, { breakLength: Infinity }), the output is expected to always be formatted as a single line, as per the Node.js documentation. However, deeply nested objects and arrays with many items are incorrectly split across multiple lines if they trigger the compact depth limit or array column grouping.

Root Cause

Two separate heuristics in lib/internal/util/inspect.js were ignoring the breakLength === Infinity condition:

  1. reduceToSingleString(): Enforces line breaks if ctx.currentDepth - recurseTimes >= ctx.compact.
  2. groupArrayElements(): Splits arrays with more than 6 items into multiple columns of up to 15 items, causing newlines.

Solution

  1. In groupArrayElements, explicitly return early when ctx.breakLength === Infinity, preventing any column grouping.
  2. In reduceToSingleString, allow inline consolidation to bypass the depth check if ctx.breakLength === Infinity.

This guarantees that all elements (objects and arrays alike) are strictly formatted on a single line when breakLength is Infinity, fixing the bug correctly without edge case holes.

Testing

  • Updated regression tests in test/parallel/test-util-inspect.js.
  • Added tests for deep arrays and objects with depth: Infinity and finite depths to ensure they always render as a single line when breakLength: Infinity.

Fixes: #60475

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Oct 3, 2026
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

@MikeMcC399

This comment was marked as resolved.

@MikeMcC399

This comment was marked as resolved.

@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.43%. Comparing base (617e082) to head (5cf8250).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #66471   +/-   ##
=======================================
  Coverage   90.42%   90.43%           
=======================================
  Files         790      790           
  Lines      275435   275439    +4     
  Branches    52826    52822    -4     
=======================================
+ Hits       249070   249085   +15     
- Misses      16765    16768    +3     
+ Partials     9600     9586   -14     
Files with missing lines Coverage Δ
lib/internal/util/inspect.js 97.04% <100.00%> (+<0.01%) ⬆️

... and 25 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@RohithPariki
RohithPariki force-pushed the fix/util-inspect-breaklength-infinity branch from 6356053 to 1cb1836 Compare October 3, 2026 11:41
RohithPariki added a commit to RohithPariki/node that referenced this pull request Oct 3, 2026
When `util.inspect` is called with `breakLength: Infinity`, it should
always format the output as a single line. However, the existing logic
for compact arrays (`groupArrayElements`) and deep objects ignored this
when the depth was large or the array was long.

This commit updates `groupArrayElements` to return early if
`breakLength === Infinity`, preventing column grouping. It also
updates `reduceToSingleString` to bypass the depth checks when
`breakLength === Infinity`, ensuring all deep objects are consolidated.

Fixes: nodejs#60475
PR-URL: nodejs#66471
@RohithPariki RohithPariki changed the title util: honor breakLength: Infinity with deep objects in inspect util: honor breakLength: Infinity for deep objects and arrays Oct 3, 2026
@RohithPariki

RohithPariki commented Oct 3, 2026 •

Copy link
Copy Markdown
Author

Reworked the implementation around the underlying issue. The previous implementation has been replaced with a focused solution that properly handles arrays (disabling column grouping) and deep objects (bypassing compact depth constraints) when breakLength: Infinity is passed, which was previously failing for finite depths and large arrays. The commit message was also reformatted to pass the linter. The PR description has been updated to reflect the new implementation.

@MikeMcC399

This comment was marked as resolved.

When `util.inspect` is called with `breakLength: Infinity`, it should
always format the output as a single line. However, the existing logic
for compact arrays (`groupArrayElements`) and deep objects ignored this
when the depth was large or the array was long.

This commit updates `groupArrayElements` to return early if
`breakLength === Infinity`, preventing column grouping. It also
updates `reduceToSingleString` to bypass the depth checks when
`breakLength === Infinity`, ensuring all deep objects are consolidated.

Fixes: nodejs#60475
Signed-off-by: Rohith Pariki <rohithpariki@gmail.com>
@RohithPariki
RohithPariki force-pushed the fix/util-inspect-breaklength-infinity branch from 17a9e47 to 5cf8250 Compare October 3, 2026 14:30
@RohithPariki

Copy link
Copy Markdown
Author

I've fixed the test failure! It turned out that the test for the object structure was still expecting some nested newlines, which �reakLength: Infinity properly eliminates. The expected output is now correctly formatted as a single line inline string. The CI should pass now.

@MikeMcC399

Copy link
Copy Markdown
Contributor

The previous response appears to have been generated by AI. Please note the AI use policy and guidelines in the section When AI is used in communications, where it says:

Do not paste messages generated entirely by AI in pull requests, issues, or the project's communication channels. Such communication may be removed in accordance with the Node.js moderation policy.

Whether or not this was an AI comment, please correct the formatting of the response.

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

Labels

agentscan:mixed-signals needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

util.inspect() ignores breakLength: Infinity when depth: Infinity is also set in some cases

3 participants