Repository navigation
feat: add REPL top level await support - #1383
Conversation
|
@cspotcode Could you please review if this is the right approach? |
154c118 to
ec5df21
Compare
|
Ok, upon inspecting I know why issue 1 was happening, it's due: Lines 233 to 235 in 5643ad6 For TLA, where the code it's wrapped in async IIFE, a single await cannot be exec without its wrapper or it will be return "await is only valid in async functions and the top level bodies of modules" error. So what I adjusted is to For 2nd issue, I've checked the output (compiled) code: await 1;
const x = await 2;output: "use strict";
(async () => {
await 1;
void (x = await 2);
})();this is a bit more puzzling, I don't know why the compiler is removing the EDIT: I found the cause with 2nd issue, and it's an upstream nodejs bug, it can be reproduced with: node --use_strict --experimental-repl-await
> const x = await 1;
Uncaught ReferenceError: x is not defined
at REPL1:1:24
at processTicksAndRejections (node:internal/process/task_queues:96:5)Problematic code is here: Since |
c451326 to
76437f0
Compare
|
This PR is now in a fully working state, but we may want until nodejs/node#39265 is merged upstream since this PR is using the same fix. Meanwhile, it's still needed to decide how to enable this feature (i'm leaning more towards a Also, I'd like to add this test suite https://github.com/nodejs/node/blob/master/test/parallel/test-repl-preprocess-top-level-await.js, but don't know where exactly should I add it. |
7a70799 to
435c4fc
Compare
|
Since there are many changes in upstream node PR, I'll mark this as draft until it's merged, so I can update this PR based on final version. |
435c4fc to
c0c979e
Compare
|
@ejose19 thanks for writing this! I've been on vacation so it will be a few more days till I have time to review this. If node has an upstream bugfix, I would say it's ok for us to release a version of ts-node with that bugfix ahead of node. |
Codecov Report
|
You said that node enables it via
How long will it take to run? Will it slow down our tests much? All our tests are declared in Our tests currently pack |
Yes, if that's the case it should be documented that the flag doesn't rely on node own TLA mechanism, but instead a based-mechanism, also I think an alias would be very useful since TLA has a lot of relevancy when used in REPL (as writting
If we're upstreaming the file, I doubt the tests will be relevant anymore, as at most we only will be changing imports. However these tests are very brief, doubt we will have any slowdown if including that and this other relevant test suite. |
Ok, yeah. I have been putting the word We can call this flag EDIT: the node flag is
I'll leave it up to you whether or not you want to add them to our test suite. I see what you mean, they should be fast to run. Are you thinking you can copy-paste them into |
We should keep in mind that This is one of the reasons I feel good keeping the feature labelled as "experimental" so that we can make breaking changes to it in non-major releases. |
I mean, I'm ok with just |
You're talking about the difference between |
Yes, this one, after all await usage in |
Sure, I agree to remove diagnostics that are not specific to TLA (like that one), could be done inside |
|
Let me know if I should take care of remaining issues, so we don't conflict on changes. |
…de's runtime effectively does the same thing
ejose19
left a comment
There was a problem hiding this comment.
Same way to suppress output as "use strict".
Sounds good. I'm still pushing changes; I'll tell you when I'm done. |
Co-authored-by: ejose19 <8742215+ejose19@users.noreply.github.com>
|
For the test that is failing with: |
This should be covered by
I'm done for today. Feel free to push fixes for those issues. |
Not right now, as EDIT: I've opted for the 2nd option, as we're only interested in doing this for interactive repl due TLA support (no longer need to set |
|
I have finally merged this. Thanks again for the great work on this pull request @ejose19 ! |
|
@cspotcode You're welcome! Glad I could contribute back to this great project, and thanks for your time to review and co-author this PR |
|
This is the PR of the decade! Thanks to everyone involved. |
* feat: add repl top level await support * refactor: add more files as dist-raw, adjust primordial imports * refactor: add flag support for experimental repl await * refactor: add support for Typescript input in experimental repl await * refactor: conditionally await eval result * chore: update node-repl-await file * refactor: dynamically exclude TLA diagnostics when --experimental-repl-await is set * refactor: exclude sourceMap when transpiling for experimentalReplAwait * refactor: add acorn & acorn-walk as dependencies and remove them from dist-raw * refactor: allow setting experimentalReplAwait via env & tsconfig * refactor: adjust evalCode signature to avoid being a breaking change * refactor: improve top level await mechanism * refactor: adjust ignored diagnostic codes related to top level await/for await * refactor: use evalCodeInternal and revert evalCode to previous signature * refactor: await evalAndExitOnTsError result during bin `main` * refactor: adjust node-primordials * chore: remove unused require * refactor: revert to previous implementation of node-primordials, add missing methods * refactor: adjust node-repl-await to use compatible syntax up to node 12 * fix: typo in node-repl-await * refactor: add unhandledRejection listener * chore: remove node-primordials dts * fix: typo in node version comparison * test: add top level await tests * test: fix tla test * test: add upstream test suite of tla * feat: allow REPL to be configured on start * refactor: return repl server on `start` * refactor: use a different context when useGlobal = false * test: adjust upstream tests to latest changes * refactor: adjust new line placement on top level await processing * refactor: adjust ignored codes related to TLA * test: adjust TLA tests * refactor: override target when experimental repl await is set * test: adjust tla test * test: adjust tla tests * test: adjust tla tests * test: move tla upstream tests to a separate file * refactor: lazy load processTopLevelAwait * refactor: correctly handle errors for async eval result * refactor: throw error if target is not compatible with experimental repl await * refactor: adjust main call in bin * refactor: don't exclude tla diagnostic codes when mode is "entrypoint" * refactor: move new repl start implementation to startInternal * fix: typo in config * test: adjust tla tests * test: normalize object usage in commandline * test: move upstream tla deps from testlib to its file * test: fix formatObjectCommandLine * refactor: adjust type assertion * refactor: adjust _eval to iterate changes using a for loop * refactor: adjust execution implementation in nodeEval * test: add tla test * refactor: fix processTopLevelAwait return type * refactor: restore `main` to sync, implement callback mechanism * refactor: small adjustments in repl * refactor: remove TLA support from [stdin] & [eval] * refactor: adjust tla tests * refactor: improve code reuse in repl tests * Add raw/node-repl-await.js for easier diffing in the future * Rename flag to --no-experimental-repl-await to match node; enable by default when target is high enough; avoid `process.exit()` in `create()`; change `await` detection heuristic to match node's * Minimize changes to bin, since the only async possibility is in REPL, so bin.ts does not need to handle its errors * Integrate with latest `main` branch * fix test * fix tests * fix * fix * fix tests * fix test; make test-local fix linting errors instead of blocking on them * fix * normalize paths passed to diagnosticFilters to hopefully fix windows tests * force repl's virtual file to be a module, which is safe as long as node's runtime effectively does the same thing * remove extraneous prop deletion in show-config output * remove some todos * Update src/repl.ts Co-authored-by: ejose19 <8742215+ejose19@users.noreply.github.com> * refactor: move forceToBeModule from createRepl to startRepl * test: adjust tests * test: set static target for TLA tests * refactor: show hint regarding tla errors when shouldReplAwait=false * test: small adjustments * Final cleanup * increase wait time in repl test to reduce flakiness Co-authored-by: Andrew Bradley <cspotcode@gmail.com>
EDIT from @cspotcode:
linking to the issues being closed by this PR
Implements and closes #245
For my records, was added to node here
Opt-out flag documented here
This is a port of node own REPL top level await mechanism, however it's still not fully working:
SyntaxError: await is only valid in async functions and the top level bodies of moduleseven if the second statement is not using await, example:Other notes:
compilecomment, it's necessary to decide if this functionability should be enabled using ats-nodeflag or check if node--experimental-repl-awaitis set