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

fix(cli): Skip empty stdin in non tty environments #6714

Merged
merged 2 commits into from Jan 5, 2023

Conversation

realtimetodie
Copy link
Contributor

Fixes an issue where an error is thrown when using the compile command with a file input and a stdin of length zero.

The atty::is function only validates whether a stream source is connected to a tty. If no tty is connected, the function returns true and the stdin gets processed.

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

Example (in a non tty environment)

$ swc compile input.js --out-file=output.js

Actual behavior

Error: Cannot specify inputs from stdin and files at the same time

Related

@realtimetodie
Copy link
Contributor Author

Alternative implementation. Please clarify which one you prefer.

fn collect_stdin_input() -> Option<String> {
    if atty::is(atty::Stream::Stdin) {
        return None;
    }

    let mut lines = io::stdin()
        .lock()
        .lines()
        .map(|line| line.expect("Not able to read stdin"));

    if lines.next().is_some() {
        Some(lines.collect::<Vec<String>>().join("\n"))
    } else {
        None
    }
}

@kdy1 kdy1 requested a review from kwonoj December 25, 2022 05:22
@kwonoj
Copy link
Member

kwonoj commented Dec 25, 2022

I'm actually thinking if we should be explicit for tty like swcx --stdin, as current behavior is mostly legacy from current cli.

@realtimetodie
Copy link
Contributor Author

@kwonoj This is currently blocking aspect-build/rules_swc#88. Please let me know how to proceed.

@realtimetodie
Copy link
Contributor Author

realtimetodie commented Dec 31, 2022

I would like to offer my own opinion. I find the way the cli works at the moment more intuitive and user-friendly. Reading from stdin is the default mode and a user does not need to set an option to enable stdin. This is the case for most unix command line programs. Why complicate things. The proposed change only verifies that the stdin buffer is not empty.

@kdy1

@kdy1
Copy link
Member

kdy1 commented Jan 4, 2023

@kwonoj Thoughts?

@kwonoj
Copy link
Member

kwonoj commented Jan 4, 2023

I'm still intrigued to have an explicit option, however it doesn't have to be in this PR and probably think later. PR as-is probably ok to go for now.

@kdy1 kdy1 added this to the Planned milestone Jan 5, 2023
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset ci

Thank you!


swc-bump:

  • dbg-swc

@kdy1 kdy1 enabled auto-merge (squash) January 5, 2023 02:17
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review comment generated by auto-rebase script

@kdy1 kdy1 merged commit 0076418 into swc-project:main Jan 5, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.25 Jan 5, 2023
@swc-project swc-project locked as resolved and limited conversation to collaborators Feb 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants