SBoudrias / Inquirer.js

A collection of common interactive command line user interfaces.
MIT License
20.09k stars 1.3k forks source link

bug: hook functions can only be called from within a prompt #1426

Closed paul-thebaud closed 3 months ago

paul-thebaud commented 3 months ago

Description

When using inside Stackblitz, input prompt will crash with the following error after user validate its choice for input: HookError: [Inquirer] Hook functions can only be called from within a prompt

Error does not show inside a "classical" environment. Error does not seems to show for all available prompts (tested select: no error).

Reproduction

Go to this Stackblitz playground, run node index.js inside terminal and validate any answer for the input prompt.

SBoudrias commented 3 months ago

That's quite weird. Did you reach out to Stackblitz?

I would assume they're not just running node - even though it looks like it when running node --version. There's something different with the runtime leading to this issue.

paul-thebaud commented 3 months ago

@SBoudrias, thanks for your quick reply. As stated on the StackBlitz docs, web containers should provide "a native Node.js environment", so maybe this is not an issue on their side. I opened https://github.com/stackblitz/webcontainer-core/issues/1467 in addition to this issue.

SBoudrias commented 3 months ago

It could be an issue with their environment support of Node.js async_hooks. It's been a recurring issue this year where support was lacking on platforms like Bun.

Specifically, async_hooks are tricky around timers. And so, core utilities to bind a callback to an async hooks context would be missing or broken in some platforms. Specifically, I think AsyncResource.bind might be either missing or broken in their containers running Node.

Unrelated to this issue, I'm releasing performance improvements to Inquirer's loaders right right now. This will delay the start of animation, which maybe will fix that very specific issue you're running into 🤷🏻 (until you start doing async validation.)

paul-thebaud commented 3 months ago

Thank you a lot for that complete explication! 🙏 I'll try as soon as it get released!

paul-thebaud commented 3 months ago

According to this issue https://github.com/stackblitz/webcontainer-core/issues/1169#issuecomment-1698872854, you're right about async hooks.

paul-thebaud commented 3 months ago

@SBoudrias, Stackblitz issue has been closed in favor of already tracked https://github.com/stackblitz/webcontainer-core/issues/1169.

I suggested to leave this issue open so I can check out performance improvements and keep in touch 😉

paul-thebaud commented 3 months ago

@SBoudrias, I updated dependencies on the Stackblitz reproduction to check @inquirer/core v8.2.3, and error still appear :confused: Is there any workaround?

SBoudrias commented 3 months ago

The new inquirer (@inquirer/) are heavily relying on AsyncLocalStorage for everything; it won't work without.

The older API inquirer is larger and less ergonomic, but it works without the hooks. So you could use that instead. This version is in maintenance mode.