You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
specifically:
%s specifier doesn't do String(tempArg) #50909
%d, %i doesn't do parseInt() #48246#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:
jcbhmr@PIG-2016:~/Documents$ node -e 'console.log("%d", process.hrtime.bigint())'| node -e 'console.log(Number(fs.readFileSync(0, "utf8")))'
NaN
jcbhmr@PIG-2016:~/Documents$ bun -e 'console.log("%d", process.hrtime.bigint())'| bun -e 'console.log(Number(fs.readFileSync(0, "utf8")))'
16716400
notice how Bun works! normally parseInt() and by extension %d returns a number and that number can be rehydrated via Number(). this doesn't work with the n suffix from bigints. (yes i know; bigint => number is lossy.)
Here's another example:
jcbhmr@PIG-2016:~/Documents$ cat testconsole.js
class Frac {
constructor(num, den) {
this.num = num
this.den = den
}
[Symbol.toPrimitive]() {
return this.num / this.den
}
}
console.log("%f", new Frac(1, 2))
console.log("%f", 0.5)
jcbhmr@PIG-2016:~/Documents$ node testconsole.js
0.5
0.5
jcbhmr@PIG-2016:~/Documents$ bun testconsole.js
[Number (Frac): 0.5]
0.5
jcbhmr@PIG-2016:~/Documents$ deno run testconsole.js
NaN
0.5
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.
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.
The text was updated successfully, but these errors were encountered:
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() #48246 #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 #10292
related deno issues
denoland/deno#21427
denoland/deno#21428
related bun issues:
oven-sh/bun#7400
oven-sh/bun#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.
The text was updated successfully, but these errors were encountered: