Skip to content

fix(inspect): allow single-line format when breakLength is Infinity#64238

Open
hamidrezaghavami wants to merge 1 commit into
nodejs:mainfrom
hamidrezaghavami:fix-inspect-infinity-clean
Open

fix(inspect): allow single-line format when breakLength is Infinity#64238
hamidrezaghavami wants to merge 1 commit into
nodejs:mainfrom
hamidrezaghavami:fix-inspect-infinity-clean

Conversation

@hamidrezaghavami

Copy link
Copy Markdown
Contributor

Description

When calling util.inspect() with a breakLength configuration explicitly set to Infinity, the internal layout formatting logic should bypass multi-line chunking and formatting constraints entirely, allowing the contents to naturally evaluate on a single line.

Currently, isBelowBreakLength goes through a character length loop calculation even when length checks are logically unnecessary due to the infinite upper bound. This change introduces an explicit early return branch within isBelowBreakLength when ctx.breakLength === Infinity, properly enabling a clean, un-wrapped single-line string formatting mode.

Checklist

  • Core internal utility verification

@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 Jul 1, 2026

@ljharb ljharb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this definitely needs tests to cover the changed behavior. ideally, you'd also provide the tests' output absent this change.

@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

Hello, @ljharb The single-line formatting behaviour when breakLength is set to Infinity is covered in a test case that I have provided.
Behaviour of output for context:
Without this modification, if it causes internal length constraint checks, the output is automatically converted to a multi-line format that spans many lines.
With this modification, multi-line chunking is completely avoided by the internal layout logic, which evaluates the contents into a single line format precisely as anticipated:
"a: 1, b: 2, c: 3, d: 4 }"

Comment thread test/parallel/test-util-inspect.js Outdated
{ // test case
const obj = { a: 1, b: 2, c: 3, d: 4 };
const result = util.inspect(obj, { breakLength: Infinity });
assert.strictEqual(result, '{ a: 1, b: 2, c: 3, d: 4 }');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i get this result on node already. we'd need a test case that breaks without the fix.

@hamidrezaghavami hamidrezaghavami force-pushed the fix-inspect-infinity-clean branch 6 times, most recently from 038a50a to b91c45e Compare July 5, 2026 21:36
Comment thread test/parallel/test-util-inspect.js Outdated
{
const obj = { long: 'a'.repeat(100), another: 'b'.repeat(100) };
const result = util.inspect(obj, { breakLength: Infinity });
assert.ok(!result.includes('\n'));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this test still passes on node already; your fix isn't related.

@hamidrezaghavami hamidrezaghavami force-pushed the fix-inspect-infinity-clean branch 2 times, most recently from 31523f9 to 3060df3 Compare July 6, 2026 07:06
@hamidrezaghavami hamidrezaghavami requested a review from a team as a code owner July 6, 2026 07:06
@hamidrezaghavami hamidrezaghavami force-pushed the fix-inspect-infinity-clean branch 3 times, most recently from ede2dd7 to 34da682 Compare July 6, 2026 08:48
@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

@ljharb All formatting, linting, and core test suites are now fully passing (29/30 checks successful). The single remaining check (test-macOS) failed on an unrelated debugger timeout (test-debugger-exceptions.js 15000ms exceeded), which appears to be a macOS CI runner flake. Could you please re-run that failed macOS job when you have a moment to review?
Thank you!

@ljharb

ljharb commented Jul 6, 2026

Copy link
Copy Markdown
Member

@hamidrezaghavami again, your test case already passes, so it's not a regression test.

@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

@ljharb To clarify my intent: this patch is a performance early exit fast path inside isBelowBreakLength when breakLength === Infinity, avoiding unnecessary string width calculation loops. Because observable output is identical to main, standard tests pass on both branches. Should we label/track this strictly as a performance optimization/cleanup rather than a bug fix?

@ljharb

ljharb commented Jul 6, 2026

Copy link
Copy Markdown
Member

ahh ok, that wasn't clear to me before

Comment thread test/parallel/test-util-inspect.js Outdated
d: { nested: { deep: true } }
};
const output = util.inspect(obj, { breakLength: Infinity });
assert.strictEqual(output.includes('\n'), false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's put the exact expected output here, not just check for a newline

Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com>
@hamidrezaghavami hamidrezaghavami force-pushed the fix-inspect-infinity-clean branch from 34da682 to 31719ca Compare July 6, 2026 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

3 participants