tsx icon indicating copy to clipboard operation
tsx copied to clipboard

Can't use stdin in watch mode

Open grind-t opened this issue 2 years ago • 12 comments

Bug description

What did you do? (Provide reproduction code in next section) Run npx tsx watch for console app

What did you expect to happen? I was expecting the ability to enter text into the console

What happened instead? Entering text into the console starts rerun

Do you have an error stack-trace or screenshots? image

Reproduction

index.ts:

import { createInterface } from 'readline'

const rl = createInterface({
  input: process.stdin,
  output: process.stdout,
})

rl.on('line', async (line) => {
  console.log(line)
  rl.prompt()
})

command: npx tsx watch --clear-screen=false ./index.ts

Environment

System: OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish) CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz Shell: 5.1.16 - /bin/bash Binaries: Node: v18.12.1 npm: 9.2.0 npmPackages: tsx: ^3.12.1 => 3.12.1

Can you contribute a fix?

  • [ ] I’m interested in opening a pull request for this issue.

grind-t avatar Dec 12 '22 20:12 grind-t

We can fix this in a breaking change release by removing https://github.com/esbuild-kit/tsx/blob/be0478990305fc0052d9c6c982a20d3990658cc2/src/watch/index.ts#L166

but tsx would need another way to manually reload watch.

Will have to look into how other watchers handle this.

privatenumber avatar Dec 24 '22 14:12 privatenumber

+1

Why did you do that 😢 ?

ben12 avatar May 25 '23 15:05 ben12

We can fix this in a breaking change release by removing

https://github.com/esbuild-kit/tsx/blob/be0478990305fc0052d9c6c982a20d3990658cc2/src/watch/index.ts#L166

but tsx would need another way to manually reload watch.

Will have to look into how other watchers handle this.

To avoid breaking change, a command line option to disable this feature could be added. --no-keyboard-rerun for example ?

ben12 avatar May 31 '23 19:05 ben12

I’m having the same issue. Would the extra command line option be acceptable? If so, I can work on a PR

tizmagik avatar Jun 01 '23 01:06 tizmagik

For now, I'm open to a breaking change PR so we can agree on an end-goal. Then, another PR to back-port that to the current major version behind a flag to avoid breaking change.

privatenumber avatar Jun 01 '23 01:06 privatenumber

I actually think the --no-keyboard-rerun is a good option in general, not just to avoid the breaking change.

If we did want an option for manual reloads, I know for example nodemon manual reloads on rs+enter, but technically even that keyboard combination could be problematic for CLI apps and full ctrl+c manual restart doesn't seem so bad to me. So opting out of keyboard rerun behavior with the extra CLI option seems flexible enough. What are your thoughts?

tizmagik avatar Jun 01 '23 02:06 tizmagik

Ah I see this PR implements the flag and would work for the CLI app use case and is non-breaking. Can we start with that and add a manual reload option in a future PR?

  • https://github.com/esbuild-kit/tsx/pull/220

tizmagik avatar Jun 01 '23 02:06 tizmagik

I'm so frustrated to restart manually :(

ben12 avatar Feb 08 '24 13:02 ben12

Was there any solution for this, its very frustrating, I wanted to use tsx with inquirer but anytime i input anything in the terminal, it restarts

shahzaib-cmyk avatar Feb 19 '24 11:02 shahzaib-cmyk

My health is safe, I have changed for the experimental node watch mode + ts-node: node --trace-deprecation --abort-on-uncaught-exception --watch -r ts-node/register index.ts

ben12 avatar Feb 19 '24 12:02 ben12

Hacky solution:

cd my-project
sed -i.bak 's/process.stdin.on/void/' node_modules/tsx/dist/cli.mjs
sed -i.bak 's/process.stdin.on/void/' node_modules/tsx/dist/cli.cjs

Should work in both bash and cmd (assuming sed is there, e.g. through msys2).


Nodemon's rs<enter> may seem half-baked, but in practice you can avoid typing it in console at watch/dev time. It boils down to this:

  • You type rs<enter> as a test input and remember to type something else next time
  • You have rs<enter> as a sort of a manually-entered fixed/enum value and either:
    • Resort to another char combination for it
    • Or go much more complex route of replacing/avoiding your dev tools

sou-long avatar Feb 25 '24 00:02 sou-long

The issue is acknowledged, and the next steps are outlined above. PR contributions are welcome.

To maintain focus, I'm locking this thread but encourage constructive dialogue through PRs.

privatenumber avatar Feb 27 '24 03:02 privatenumber