•  


feature(cli): don't block forever waiting on stdin by bcmyers · Pull Request #5017 · swc-project/swc · GitHub
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

feature(cli): don't block forever waiting on stdin #5017

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bcmyers
Copy link

Description:

I use the swc cli via bazel. I noticed today that bazel is unable to supply input to the compile subcommand via stdin because it does not pass this check in the current code:

if atty::is(atty::Stream::Stdin) {
    return None;
}

It seems you probably do not want this check to remain in the long-run, as, as far as I understand it, it means that processes not invoked via a TTY will not be able to supply input for compilation via stdin , which seems like an unwanted limitation.

It got me thinking about how supplying input via stdin probably should work for the compile subcommand. I'm not at all sure that I got it right, but I wanted to try an alternative; so here it is.

In this PR I propose the following:

  • If files are supplied as arguments, stdin is ignored. The user has indicated he/she wants to compile the files listed explicitly as arguments.
  • If no files are supplied as arguments, the user is indicating that he/she wants input to be read from stdin .
  • However, because you don't want reading from stdin to block forever in the case where users accidentally call swc compile with no file arguments and without passing information over stdin , you probably want reading from stdin to timeout after a reasonable period of time and report an error.

It turns out that reading from stdin with a timeout is actually an interesting problem, but this PR accomplishes it using a mini event loop from mio on unix and the standard Windows API on windows.

An alternative design would be to use an asynchronous runtime such as tokio to add timeouts, but that would involve many other changes to the current structure. Since you don't currently have an architecture that uses an async runtime, I opted to implement "reading from stdin with a timeout" without using one, but depending on what you think I also could see benefits in re-architecting the program to use tokio .

Note: I have another "cleanup" PR outstanding ( #5003 ). This does not base itself off of that, but rather off of the current code. If #5003 lands (even partially), I'll rebase these changes on top of that.

BREAKING CHANGE:

This changes the API for reading input for compilation as described above.

Related issue (if exists):

@kdy1 kdy1 added this to the Planned milestone Jun 22, 2022
@kwonoj
Copy link
Member

kwonoj commented Jun 22, 2022

However, because you don't want reading from stdin to block forever in the case where users accidentally call swc compile with no file arguments and without passing information over stdin, you probably want reading from stdin to timeout after a reasonable period of time and report an error.

I'm not sure if I'm fully agreed with this proposal. stdin as input is an interesting challenage indeed, and my honest feeling is if it wasn't supported in v1 I'd rather not adapted in native cli even - but status quo is the world swc lives in now.

Anyway, I'd rather say it's user's responsibility to choose stdin to terminate process on their own if they decide to not to supply input later. After all, swc cannot assume any desire of user either input comes or not. Timeout is easy, implicit way to guess those but it is not correct measure - especially implementing timeout with stdin brings some amount of complexity to solve.

I'd rather to leave explicit messages when swc kicks in if it attempt to stdin, like swc now waiting input from stdin to tell its status explicitly. Or either (I think I thought this once but somehow didn't / couldn't make it work), having flags like --stdin for the compile subcommand so user manually opt in and owns complete responsibility.

@kdy1 kdy1 modified the milestones: Planned , v1.2.210 Jul 5, 2022
@kdy1 kdy1 marked this pull request as draft August 1, 2022 03:10
@kdy1 kdy1 removed this from the Planned milestone Aug 30, 2022
medfreeman added a commit to medfreeman/swc-plugin-add-import-extension that referenced this pull request Aug 30, 2022
medfreeman added a commit to medfreeman/swc-plugin-add-import-extension that referenced this pull request Sep 9, 2022
medfreeman added a commit to medfreeman/swc-plugin-add-import-extension that referenced this pull request Nov 18, 2022
Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants
- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본