-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
process: wait promise resolve before print result #52077
Conversation
Review requested:
|
Not sure what else this effects… but conceptually seems very reasonable to me! 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@H4ad some windows runs failed with a related issue: (I think it might be because of the space character ending the command?) 12:12:01 not ok 126 parallel/test-cli-eval
12:12:01 ---
12:12:01 duration_ms: 1887.13100
12:12:01 severity: fail
12:12:01 exitcode: 1
12:12:01 stack: |-
12:12:01 node:assert:1000
12:12:01 throw newErr;
12:12:01 ^
12:12:01
12:12:01 AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Command failed: "c:\workspace\node-test-binary-windows-js-suites\node\Release\node.exe" --print 'Promise.resolve().then(() => 10)'
12:12:01 [eval]:1
12:12:01
12:12:01 'Promise.resolve().then(()
12:12:01
12:12:01 ^^^^^^^^^^^^^^^^^^^^^^^^^^
12:12:01
12:12:01
12:12:01
12:12:01 SyntaxError: Invalid or unexpected token
12:12:01
12:12:01 at makeContextifyScript (node:internal/vm:185:14)
12:12:01
12:12:01 at node:internal/process/execution:109:22
12:12:01
12:12:01 at [eval]-wrapper:6:24
12:12:01
12:12:01 at runScript (node:internal/process/execution:103:62)
12:12:01
12:12:01 at evalScript (node:internal/process/execution:140:3)
12:12:01
12:12:01 at node:internal/main/eval_string:51:3
12:12:01
12:12:01
12:12:01
12:12:01 Node.js v22.0.0-pre
12:12:01
12:12:01
12:12:01 at genericNodeError (node:internal/errors:984:15)
12:12:01 at wrappedFn (node:internal/errors:538:14)
12:12:01 at ChildProcess.exithandler (node:child_process:422:12)
12:12:01 at ChildProcess.emit (node:events:519:28)
12:12:01 at maybeClose (node:internal/child_process:1105:16)
12:12:01 at Socket.<anonymous> (node:internal/child_process:457:11)
12:12:01 at Socket.emit (node:events:519:28)
12:12:01 at Pipe.<anonymous> (node:net:337:12) {
12:12:01 generatedMessage: false,
12:12:01 code: 'ERR_ASSERTION',
12:12:01 actual: Error: Command failed: "c:\workspace\node-test-binary-windows-js-suites\node\Release\node.exe" --print 'Promise.resolve().then(() => 10)'
12:12:01 [eval]:1
12:12:01
12:12:01 'Promise.resolve().then(()
12:12:01
12:12:01 ^^^^^^^^^^^^^^^^^^^^^^^^^^
12:12:01
12:12:01
12:12:01
12:12:01 SyntaxError: Invalid or unexpected token
12:12:01
12:12:01 at makeContextifyScript (node:internal/vm:185:14)
12:12:01
12:12:01 at node:internal/process/execution:109:22
12:12:01
12:12:01 at [eval]-wrapper:6:24
12:12:01
12:12:01 at runScript (node:internal/process/execution:103:62)
12:12:01
12:12:01 at evalScript (node:internal/process/execution:140:3)
12:12:01
12:12:01 at node:internal/main/eval_string:51:3
12:12:01
12:12:01
12:12:01
12:12:01 Node.js v22.0.0-pre
12:12:01
12:12:01
12:12:01 at genericNodeError (node:internal/errors:984:15)
12:12:01 at wrappedFn (node:internal/errors:538:14)
12:12:01 at ChildProcess.exithandler (node:child_process:422:12)
12:12:01 at ChildProcess.emit (node:events:519:28)
12:12:01 at maybeClose (node:internal/child_process:1105:16)
12:12:01 at Socket.<anonymous> (node:internal/child_process:457:11)
12:12:01 at Socket.emit (node:events:519:28)
12:12:01 at Pipe.<anonymous> (node:net:337:12) {
12:12:01 code: 1,
12:12:01 killed: false,
12:12:01 signal: null,
12:12:01 cmd: `"c:\\workspace\\node-test-binary-windows-js-suites\\node\\Release\\node.exe" --print 'Promise.resolve().then(() => 10)'`
12:12:01 },
12:12:01 expected: null,
12:12:01 operator: 'ifError'
12:12:01 }
12:12:01
12:12:01 Node.js v22.0.0-pre |
I have no idea but I will investigate tonight, thanks |
@H4ad actually this happens to me on a release as well with the single quotes, but works fine with double quotes 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should keep Promise { <result> }
notation. Also, we need to take the never-settling promises into account (i.e. node -p 'new Promise(()=>{})'
)
Hopefully, if --experimental-detect-module
becomes the default, we can enable node -p 'await promise'
and avoid the need for a new flag or for "magic" behavior.
This is an issue that you will have once we have top level await, so we can address this issue in other PR.
Well, we should keep it or not?
This is a good point, once we have
EditActually, my take about So, I still prefer to keep the final result WITHOUT the |
lib/internal/process/execution.js
Outdated
if (isPromise(result)) { | ||
PromisePrototypeThen(result, (resultValue) => log(resultValue)); | ||
} else { | ||
log(result); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's my suggestion: we're still logging the raw input, but we wait for the process to exit to do so. I see two upsides for doing this:
- We no longer need a special case for promises.
- it will still show the value with which the promise was fulfilled (if applicable).
- it handles never-settling promises just right, and other edge cases (e.g.
node -p 'setTimeout(process.exit,100, 0);timers.promises.setTimeout(200)'
)
if (isPromise(result)) { | |
PromisePrototypeThen(result, (resultValue) => log(resultValue)); | |
} else { | |
log(result); | |
} | |
process.on('exit', () => { | |
log(result); | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please consider copy pasting your console output rather than sharing screenshots, as screenshots might be harder to interpret for e.g. folks using screen readers, and it makes it much harder to copy/paste if someone wants to reproduce what you did.
do we really want to have different behavior with them in this utility?
I think we do.
If we all agree that my suggestion is an improvement, we could start by landing that, and let's start another discussion to try to find consensus – or if you prefer I can open a PR with my suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we all agree that my suggestion is an improvement, we could start by landing that
I liked the idea of putting the print on process.on('exit')
, I will keep that, but how we want to print the results is something we need to decide and I think we should decide in this PR to land everything together.
Initially, @mcollina raised the same concern but later dismissed their request change for that, currently, I think you are the only one who thinks we should print the Promise { result }
now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the point in waiting for a decision, on the contrary we should make small incremental improvements. I'll open a PR with my suggestion if that's OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you remove the Request Change for this PR, I'm okay with you creating another PR.
I push the changes with process.on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've opened #52137, I suggest we fast-track it, and you can rebase you PR on top of it and we can see better what diff your PR introduces for the various cases I suggested there.
dc57999
to
a808430
Compare
Force pushed/rebased just to include the tests added by @aduh95. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you keep the test cases in the same order so git shows the diff in a more straight forward way?
a808430
to
dbcf465
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like I said above, I don't think the current PR is improving things if there are cases where nothing at all get printed to the stdout.
I also think the auto-await feature is not desirable: -p
is used as a debugging feature, and therefor indicate when the value is a promise / thenable. Adding .then(console.log)
is an existing workaround for folks who try to get promise result from a script.
@@ -43,7 +43,7 @@ describe('--print with a promise', { concurrency: true }, () => { | |||
code: 0, | |||
signal: null, | |||
stderr: '', | |||
stdout: 'Promise { <pending> }\n', | |||
stdout: '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it's acceptable.
@@ -57,11 +57,11 @@ describe('--print with a promise', { concurrency: true }, () => { | |||
code: 0, | |||
signal: null, | |||
stderr: '', | |||
stdout: 'Promise { <pending> }\n', | |||
stdout: '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
@@ -72,11 +72,11 @@ describe('--print with a promise', { concurrency: true }, () => { | |||
code: 0, | |||
signal: null, | |||
stderr: '', | |||
stdout: 'Promise { <rejected> 1 }\n', | |||
stdout: '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. I would consider that change semver-major if we stick with it.
@@ -87,7 +87,32 @@ describe('--print with a promise', { concurrency: true }, () => { | |||
code: 0, | |||
signal: null, | |||
stderr: '', | |||
stdout: 'Promise { <pending> }\n', | |||
stdout: '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
|
||
assert.strictEqual(result.code, 1); | ||
assert.strictEqual(result.signal, null); | ||
assert.strictEqual(result.stdout, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
|
||
assert.strictEqual(result.code, 1); | ||
assert.strictEqual(result.signal, null); | ||
assert.strictEqual(result.stdout, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
process.on('exit', async () => { | ||
log(await result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concerns would be addressed if we removed the await
here.
process.on('exit', async () => { | |
log(await result); | |
process.on('exit', () => { | |
log(result); |
Closing in favor of #52172 |
Co-authored-by: Vinícius Lourenço <contact@viniciusl.com.br> Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #52172 Refs: #52077 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Co-authored-by: Vinícius Lourenço <contact@viniciusl.com.br> Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: nodejs#52172 Refs: nodejs#52077 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Co-authored-by: Vinícius Lourenço <contact@viniciusl.com.br> Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: nodejs#52172 Refs: nodejs#52077 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
Inspired by https://twitter.com/jarredsumner/status/1767795689575285214
Wait for Promise resolve when printing any code using
--print
or--eval
.When throwing an exception, the output is:
Since I'm changing the output, this probably should be marked as major, right?