Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior between util.formatWithOptions and util.inspect #29726

Closed
tiagonapoli opened this issue Sep 26, 2019 · 3 comments
Closed
Labels
question Issues that look for answers. util Issues and PRs related to the built-in util module.

Comments

@tiagonapoli
Copy link

tiagonapoli commented Sep 26, 2019

The following snippet shows a possible unexpected output for formatWithOptions. On console.log(util.formatWithOptions({ }, '%s', obj)) I expected a output { a: [ '123' ] }, but got { a: [Array] } :

const util = require('util')

const obj = { a: ['123'] }

/************ formatWithOptions ************/

// { a: [Array] }
console.log(util.formatWithOptions({ }, '%s', obj))

// { a: [ '123' ] } - Note: this is not respecting the api docs, the first argument has to be an object
console.log(util.formatWithOptions('%s', obj))

/******************************************/

/************ inspect ************/

// { a: [ '123' ] }
console.log(util.inspect(obj, { }))

// { a: [ '123' ] }
console.log(util.inspect(obj))

/*******************************/

Since util.formatWithOptions('%s', obj) doesn't respect the api docs, maybe this is an expected behavior for util.formatWithOptions, in this case maybe it could be changed to have similar behavior to inspect?

@tiagonapoli tiagonapoli changed the title Unexpected output with util.formatWithOptions Inconsistent behavior between util.formatWithOptions and util.inspect Sep 26, 2019
@tiagonapoli
Copy link
Author

Saw that the following code specifies that the depth for %s is garanteed to be 0, overriding the given inspectOptions, is this the behavior intended?

tempStr = inspect(tempArg, {
...inspectOptions,
compact: 3,
colors: false,
depth: 0
});

@BridgeAR
Copy link
Member

@tiagonapoli yes, that is intentional. The reason is that inspecting objects with %s was until recently not inspected at all but String(obj) was used and that would result in e.g. [object Object] for input like { a: true }.

If you want to pass through objects, please use the %O formatter. That way the depth is handled as with util.inspect().

@BridgeAR BridgeAR added question Issues that look for answers. util Issues and PRs related to the built-in util module. labels Sep 26, 2019
@BridgeAR
Copy link
Member

And util.formatWithOptions('%s', obj) should actually throw an error. You pass through '%s' as options.

BridgeAR added a commit to BridgeAR/node that referenced this issue Oct 3, 2019
This makes sure that the `inspectOptions` are validated. This could
otherwise cause confusion.

Fixes: nodejs#29726
@Trott Trott closed this as completed in 2664dac Oct 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants