Open jcbhmr opened 9 months ago
Not sure if this is directly implemented by V8 instead. Can anyone from @nodejs/v8 confirm?
I don't think console nor the formatting logic is native to V8. I think it's here: https://github.com/nodejs/node/blob/main/lib/internal/console/constructor.js for the console.log() function impls which then calls... https://github.com/nodejs/node/blob/main/lib/internal/util/inspect.js#L2179 the format() function from the node:util which actually does the %s %i %d %f stuff
according to the console spec: https://console.spec.whatwg.org/#formatter
but node.js doesn't follow that exactly. here's the current console.log() backing inspect() code: https://github.com/chenyuyang2022/node/blob/fix/console-log/lib/internal/util/inspect.js#L2196 that's used when running console.log().
specifically: %s specifier doesn't do String(tempArg) #50909 %d, %i doesn't do parseInt() https://github.com/nodejs/node/pull/48246 https://github.com/nodejs/node/pull/23321
Why is this not good? causes interop issues with other js runtimes. Conforming to the standard is good! Here's a specific example that demonstrates how a problem may arise:
notice how Bun works! normally
parseInt()
and by extension%d
returns a number and that number can be rehydrated viaNumber()
. this doesn't work with the n suffix from bigints. (yes i know; bigint => number is lossy.)Here's another example:
This WORKS PROPERLY right now in Node.js, Chrome, Firefox... but fails in Bun and Deno. That's a divergence! Not good for isomorphic interoperable code.
Here's another example of Node.js erroneous handling: from #50909
in another case, if i were using
%s
to try and be "no i actually don't want inspect()-style formatting, just coerce things to a string" then this would diverge across implementations.why would i want to coerce everything to a string when logging? maybe i replaced Object.prototype.toString or some other weird thing. who knows. again, it's just Node.js that does something different which creates edge cases like
if (isNode) { } else { }
. not ideal.related https://github.com/nodejs/node/issues/10292
related deno issues https://github.com/denoland/deno/issues/21427 https://github.com/denoland/deno/issues/21428
related bun issues: https://github.com/oven-sh/bun/issues/7400 https://github.com/oven-sh/bun/issues/7401
related firefox issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1846606
i am of the opinion that achieving better interoperability on format specifiers is a good idea. to that end, i suggest trying to conform to the console spec. https://console.spec.whatwg.org/#formatter
sorry if this has been discussed in individual issues. this is intended to be a push not for fixing a specific case like the above examples, but instead to push to fix all of them by conforming to the spec. i understand that this is a breaking change and that there is some pushback.