Prompts only receive the first line of piped input (e.g. `hardhat keystore set` in CI exits with code 13)
Version of Hardhat
3.18.0 (@nomicfoundation/hardhat-keystore 3.1.0)
What happened?
When input is piped, only the first prompt gets an answer. The following prompts never receive their line, and the process exits with Node's unsettled top-level await code 13. This makes the keystore impossible to use from a script or CI, since there is no other way to provide the password:
$ printf 'Passw0rd!Long\nPassw0rd!Long\nmy-secret-value\n' | npx hardhat keystore set MY_KEY
[hardhat-keystore] Enter the password: [hardhat-keystore] Please confirm your password: Warning: Detected unsettled top-level await at file:///.../node_modules/hardhat/dist/src/cli.js:21
await main(process.argv.slice(2), {
^
$ echo $?
13No keystore is created. The same happens for any task that asks more than one question through hre.interruptions.
The default handlers in packages/hardhat/src/internal/core/user-interruptions.ts create a new readline interface for each prompt and close it after the answer. When stdin is a pipe, the first interface receives all the piped lines in one chunk; readline still emits the remaining lines as line events after close(), but nothing listens for them, so they are lost and the next interface waits for input that never comes.
Minimal reproduction steps
npm i hardhat @nomicfoundation/hardhat-keystorein an ESM project and add the plugin tohardhat.config.ts.- Run
printf 'Passw0rd!Long\nPassw0rd!Long\nmy-secret-value\n' | npx hardhat keystore set MY_KEY.
Or, without the plugin, two requestInput calls with printf 'a\nb\n' piped in.
Search terms
keystore, pipe, stdin, non-interactive, requestSecretInput, user interruptions
Source: NomicFoundation/hardhat