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
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 113 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion crates/swc_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ plugin = [

[dependencies]
anyhow = "1.0.53"
atty = "0.2.14"
clap = { version = "3.1.0", features = ["derive", "wrap_help"] }
glob = "0.3.0"
rayon = "1"
Expand All @@ -35,6 +34,7 @@ swc = { version = "0.190.0", path = "../swc" }
swc_common = { version = "0.18.0", path = "../swc_common" }
swc_plugin_runner = { version = "0.56.0", path = "../swc_plugin_runner", default-features = false, optional = true }
swc_trace_macro = { version = "0.1.0", path = "../swc_trace_macro" }
thiserror = "1.0.31"
tracing = "0.1.32"
tracing-chrome = "0.5.0"
tracing-futures = "0.2.5"
Expand All @@ -46,3 +46,15 @@ wasmer-wasi = { version = "2.3.0", optional = true }
[dependencies.path-absolutize]
features = ["once_cell_cache"]
version = "3.0.11"

[target.'cfg(unix)'.dependencies]
mio = { version = "0.8.4", features = ["os-ext"] }

[target.'cfg(windows)'.dependencies.windows]
features = [
"Win32_Foundation",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_System_Threading",
]
version = "0.37.0"
85 changes: 38 additions & 47 deletions crates/swc_cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{
fs::{self, File},
io::{self, BufRead, Write},
io::Write,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};

use anyhow::Context;
Expand All @@ -21,7 +22,7 @@ use swc_common::{
use swc_trace_macro::swc_trace;
use walkdir::WalkDir;

use crate::util::trace::init_trace;
use crate::{util, util::trace::init_trace};

/// Configuration option for transform files.
#[derive(Parser)]
Expand Down Expand Up @@ -238,21 +239,6 @@ fn emit_output(
Ok(())
}

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

Some(
io::stdin()
.lock()
.lines()
.map(|line| line.expect("Not able to read stdin"))
.collect::<Vec<String>>()
.join("\n"),
)
}

struct InputContext {
options: Options,
fm: Arc<SourceFile>,
Expand Down Expand Up @@ -296,34 +282,7 @@ impl CompileOptions {
fn collect_inputs(&self) -> anyhow::Result<Vec<InputContext>> {
let compiler = COMPILER.clone();

let stdin_input = collect_stdin_input();
if stdin_input.is_some() && !self.files.is_empty() {
anyhow::bail!("Cannot specify inputs from stdin and files at the same time");
}

if let Some(stdin_input) = stdin_input {
let options = self.build_transform_options(&self.filename.as_deref())?;

let fm = compiler.cm.new_source_file(
if options.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.filename.clone().into())
},
stdin_input,
);

return Ok(vec![InputContext {
options,
fm,
compiler,
file_path: self
.filename
.clone()
.unwrap_or_else(|| PathBuf::from("unknown")),
file_extension: self.out_file_extension.clone().into(),
}]);
} else if !self.files.is_empty() {
if !self.files.is_empty() {
let included_extensions = if let Some(extensions) = &self.extensions {
extensions.clone()
} else {
Expand Down Expand Up @@ -353,10 +312,42 @@ impl CompileOptions {
})
})
})
.collect::<anyhow::Result<Vec<InputContext>>>();
.collect();
}

anyhow::bail!("Input is empty");
let stdin = {
let mut buf = Vec::new();
util::io::read_from_stdin_with_timeout(
&mut buf,
Duration::from_millis(100), // poll_duration
Duration::from_secs(5), // timeout
)?;
let stdin =
String::from_utf8(buf).context("Input from stdin may not contain invalid utf-8")?;
if stdin.is_empty() {
anyhow::bail!("Input is empty");
}
stdin
};
let options = self.build_transform_options(&self.filename.as_deref())?;
let fm = compiler.cm.new_source_file(
if options.filename.is_empty() {
FileName::Anon
} else {
FileName::Real(options.filename.clone().into())
},
stdin,
);
Ok(vec![InputContext {
options,
fm,
compiler,
file_path: self
.filename
.clone()
.unwrap_or_else(|| PathBuf::from("unknown")),
file_extension: self.out_file_extension.clone().into(),
}])
}

fn execute_inner(&self) -> anyhow::Result<()> {
Expand Down
Loading