Skip to content
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

Add --on-return-key flag to conditionally enable Return key listener #639

Closed
wants to merge 2 commits into from

Conversation

iuroc
Copy link

@iuroc iuroc commented Aug 19, 2024

Add --on-return-key flag to conditionally enable Return key listener

  • Added --on-return-key flag to the watch command for optional Return key press handling.
  • Updated the command logic to conditionally register process.stdin.on('data') listener based on the flag.
  • Improved flag description and default behavior in the command configuration.
  • Note: Enabling --on-return-key may cause issues with concurrently "tsx watch some.ts" when some.ts imports/require process. Specifically, if some.ts includes import mysql from "mysql2" and console.log(mysql), it may block execution at the import statement and not correctly print the content.
    • You can temporarily resolve this issue by using concurrently -r.

This change allows users to control whether the script should trigger a re-run on Return key press, providing more flexibility in how the watch command operates.


Below is an example demonstrating why the onReturnKey parameter is needed.

Create 1.js:

console.log(1)

require('process')

console.log(2)

Create 2.js:

const { spawn } = require('child_process')

spawn('node', ['1.js'], {
    stdio: ['inherit', 'inherit', 'inherit'],
    shell: true
})
process.stdin.on('data', () => {
    console.log('log in 2.js')
})

Create 3.js:

const { spawn } = require('child_process')

const child = spawn('node', ['2.js'], {
    stdio: ['pipe', 'pipe', 'pipe'],
    shell: true
})
child.stdout.on('data', chunk => {
    console.log(chunk.toString())
})

Here are the execution results:

Running 1.js:

> node 1.js
1
2
(program ends)

Running 2.js:

> node 2.js
1
2
(waiting for input) aaaaa
log in 2.js
bbbbb
log in 2.js
ccccc
log in 2.js
ddddd
log in 2.js
(waiting for input)

Running 3.js:

node 3.js
1
(there is an extra blank line here)
(blocked, unable to input, Enter has no effect)

Analysis

  • Running 1.js and 2.js both output correctly.
  • The process.stdin.on('data',) event causes 2.js to enter a waiting input blocking state after printing.
  • When running node 3.js, the terminal only outputs 1 and not 2, indicating that execution is stuck at require('process') in 1.js, and the subsequent code does not execute.
  • Any of the following three actions allow all three files to execute normally:
    • Remove process.stdin.on('data',) from 2.js
    • Remove require("process") from 1.js
    • Change stdio in 3.js to ['inherit', 'inherit', 'inherit'] and remove process.stdin.on('data',)
  • tsx watch also uses process.stdin.on('data',) (src/watch/index.ts#L219), which is why concurrently "tsx watch xxx.ts" will cause xxx.ts and all its imported modules to be stuck at the require("process") line, with subsequent code not executing.
  • So, what is the underlying reason for this?

@kermitsxb
Copy link

How is this PR going ? I'm interested in using tsx in my CLI development process

@privatenumber
Copy link
Owner

This is a breaking change so it can't be released until the next major. But also, this work is already queued for v5. This PR was made without a heads up. The timeline for v5 is not known yet.

If you use tsx, the best way to get more development on it is to fund it. Currently, it barely gets funding so I struggle to justify investing more time into it.

@iuroc
Copy link
Author

iuroc commented Sep 11, 2024

I understand your point, and this is indeed a breaking change that needs to wait for the next major release. However, this change is currently causing issues when used alongside concurrently, so I'll have to find an alternative workaround until v5 is released.

@iuroc iuroc closed this Sep 11, 2024
@kermitsxb
Copy link

@privatenumber thank you for your answer, I totally understand your point.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants