Skip to content

util: honor breakLength: Infinity with deep objects in inspect - #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

Copy link
Copy Markdown

Problem

When using util.inspect(obj, { breakLength: Infinity, depth: Infinity }) (or depth: null), deeply nested objects are unexpectedly split across multiple lines instead of being formatted on a single line. The Node.js documentation states:

breakLength <integer> The length at which input values are split across multiple lines. Set to Infinity to format the input as a single line (in combination with compact set to true or any number >= 1). Default: 80.

Root Cause

In reduceToSingleString() (lib/internal/util/inspect.js):

if (ctx.currentDepth - recurseTimes < ctx.compact &&
    entries === output.length) {

When inspecting an object with unlimited depth (depth: Infinity or depth: null), ctx.currentDepth increments as depth increases. Since compact defaults to 3, ctx.currentDepth - recurseTimes < ctx.compact fails for all parent/outer levels beyond depth 2. As a result, inline consolidation is skipped and newlines are inserted, despite breakLength: Infinity.

A previous closed attempt (#60783) disabled groupArrayElements when breakLength === Infinity, which broke compact array grouping with compact: 1.

Solution

In reduceToSingleString(), permit inline single-line consolidation when breakLength === Infinity and depth is unlimited (ctx.depth === null || ctx.depth === Infinity):

const isUnlimitedDepth = ctx.depth === null || ctx.depth === Infinity;
if (((ctx.breakLength === Infinity && isUnlimitedDepth) ||
     ctx.currentDepth - recurseTimes < ctx.compact) &&
    entries === output.length) {

This preserves array element grouping under compact: 1 and line wrapping for finite breakLength, while honoring the documented single-line contract for deep objects.

Testing

  • Added regression tests in test/parallel/test-util-inspect.js covering breakLength: Infinity with depth: Infinity and depth: null for deeply nested objects.
  • Validated compact array grouping (compact: 1), normal wrapping with finite breakLength, and default inspection behavior.

Fixes: #60475

When inspecting deeply nested objects with breakLength: Infinity and depth: Infinity (or depth: null), reduceToSingleString() was previously skipping inline consolidation because currentDepth exceeded compact (default 3), causing unexpected newlines.

Allow single-line consolidation when breakLength is Infinity and depth is unlimited, while preserving compact array grouping.

Fixes: nodejs#60475
Signed-off-by: Rohith Pariki <rohithpariki@gmail.com>
@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

Copy link
Copy Markdown
Contributor

The recommendations in:

advise you should tackle only one issue at a time and that you should not open any new PRs until your first PR has been approved.

This is for your benefit and for the benefit of the team assisting with PRs. In both PRs the commit messages are incorrectly formatted, so it makes double the amount of work to advise you to correct the mistake in 2 different places.

You have later also opened #66472.

@MikeMcC399

Copy link
Copy Markdown
Contributor

See Commit message guidelines according to the Pull requests documentation.

Please correct the problem with the text width.

@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.41%. Comparing base (7fab656) to head (6356053).
⚠️ Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66471      +/-   ##
==========================================
- Coverage   90.42%   90.41%   -0.01%     
==========================================
  Files         791      791              
  Lines      275569   275571       +2     
  Branches    52842    52842              
==========================================
- Hits       249185   249161      -24     
- Misses      16802    16806       +4     
- Partials     9582     9604      +22     
Files with missing lines Coverage Δ
lib/internal/util/inspect.js 97.04% <100.00%> (+<0.01%) ⬆️

... and 23 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.

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