Skip to content

Commit c635a0d

Browse files
authored
refactor(cli): do not capture and force colors of cargo build output (#4627)
1 parent 3b4ed97 commit c635a0d

File tree

2 files changed

+80
-62
lines changed

2 files changed

+80
-62
lines changed

.changes/cli-build-output.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Do not capture and force colors of `cargo build` output.

tooling/cli/src/interface/rust.rs

Lines changed: 74 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl Interface for Rust {
183183
.out_dir(Some(triple.into()), options.debug)
184184
.with_context(|| format!("failed to get {} out dir", triple))?;
185185
self
186-
.build_app_blocking(options)
186+
.build_production_app(options)
187187
.with_context(|| format!("failed to build {} binary", triple))?;
188188

189189
lipo_cmd.arg(triple_out_dir.join(&bin_name));
@@ -198,7 +198,7 @@ impl Interface for Rust {
198198
}
199199
} else {
200200
self
201-
.build_app_blocking(options)
201+
.build_production_app(options)
202202
.with_context(|| "failed to build app")?;
203203
}
204204

@@ -334,7 +334,7 @@ impl Rust {
334334
let app_child = Arc::new(Mutex::new(None));
335335
let app_child_ = app_child.clone();
336336

337-
let build_child = self.build_app(options, move |status, reason| {
337+
let build_child = self.build_dev_app(options, move |status, reason| {
338338
if status.success() {
339339
let bin_path =
340340
rename_app(&bin_path, product_name.as_deref()).expect("failed to rename app");
@@ -453,55 +453,32 @@ impl Rust {
453453
}
454454
}
455455

456-
fn build_app_blocking(&mut self, options: Options) -> crate::Result<()> {
457-
let (tx, rx) = channel();
458-
self.build_app(options, move |status, _| tx.send(status).unwrap())?;
459-
let status = rx.recv().unwrap();
460-
if status.success() {
461-
Ok(())
462-
} else {
463-
Err(anyhow::anyhow!("failed to build app"))
456+
fn build_production_app(&mut self, options: Options) -> crate::Result<()> {
457+
let mut build_cmd = self.build_command(options)?;
458+
let runner = build_cmd.get_program().to_string_lossy().into_owned();
459+
match build_cmd.piped() {
460+
Ok(status) if status.success() => Ok(()),
461+
Ok(_) => Err(anyhow::anyhow!("failed to build app")),
462+
Err(e) if e.kind() == ErrorKind::NotFound => Err(anyhow::anyhow!(
463+
"`{}` command not found.{}",
464+
runner,
465+
if runner == "cargo" {
466+
" Please follow the Tauri setup guide: https://tauri.app/v1/guides/getting-started/prerequisites"
467+
} else {
468+
""
469+
}
470+
)),
471+
Err(e) => Err(e.into()),
464472
}
465473
}
466474

467-
fn build_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>(
475+
fn build_dev_app<F: FnOnce(ExitStatus, ExitReason) + Send + 'static>(
468476
&mut self,
469477
options: Options,
470478
on_exit: F,
471479
) -> crate::Result<Arc<SharedChild>> {
472-
let runner = options.runner.unwrap_or_else(|| "cargo".into());
473-
474-
if let Some(target) = &options.target {
475-
if self.available_targets.is_none() {
476-
self.fetch_available_targets();
477-
}
478-
self.validate_target(target)?;
479-
}
480-
481-
let mut args = Vec::new();
482-
if !options.args.is_empty() {
483-
args.extend(options.args);
484-
}
485-
486-
let mut features = self.config_features.clone();
487-
if let Some(f) = options.features {
488-
features.extend(f);
489-
}
490-
if !features.is_empty() {
491-
args.push("--features".into());
492-
args.push(features.join(","));
493-
}
494-
495-
if !options.debug {
496-
args.push("--release".into());
497-
}
498-
499-
if let Some(target) = options.target {
500-
args.push("--target".into());
501-
args.push(target);
502-
}
503-
504-
let mut build_cmd = Command::new(&runner);
480+
let mut build_cmd = self.build_command(options)?;
481+
let runner = build_cmd.get_program().to_string_lossy().into_owned();
505482
build_cmd
506483
.env(
507484
"CARGO_TERM_PROGRESS_WIDTH",
@@ -517,30 +494,25 @@ impl Rust {
517494
.to_string(),
518495
)
519496
.env("CARGO_TERM_PROGRESS_WHEN", "always");
520-
build_cmd.arg("build").arg("--color").arg("always");
521-
build_cmd.args(args);
497+
build_cmd.arg("--color");
498+
build_cmd.arg("always");
522499

523500
build_cmd.stdout(os_pipe::dup_stdout()?);
524501
build_cmd.stderr(Stdio::piped());
525502

526503
let build_child = match SharedChild::spawn(&mut build_cmd) {
527-
Ok(c) => c,
528-
Err(e) => {
529-
if e.kind() == ErrorKind::NotFound {
530-
return Err(anyhow::anyhow!(
531-
"`{}` command not found.{}",
532-
runner,
533-
if runner == "cargo" {
534-
" Please follow the Tauri setup guide: https://tauri.app/v1/guides/getting-started/prerequisites"
535-
} else {
536-
""
537-
}
538-
));
504+
Ok(c) => Ok(c),
505+
Err(e) if e.kind() == ErrorKind::NotFound => Err(anyhow::anyhow!(
506+
"`{}` command not found.{}",
507+
runner,
508+
if runner == "cargo" {
509+
" Please follow the Tauri setup guide: https://tauri.app/v1/guides/getting-started/prerequisites"
539510
} else {
540-
return Err(e.into());
511+
""
541512
}
542-
}
543-
};
513+
)),
514+
Err(e) => Err(e.into()),
515+
}?;
544516
let build_child = Arc::new(build_child);
545517
let build_child_stderr = build_child.take_stderr().unwrap();
546518
let mut stderr = BufReader::new(build_child_stderr);
@@ -592,6 +564,46 @@ impl Rust {
592564

593565
Ok(build_child)
594566
}
567+
568+
fn build_command(&mut self, options: Options) -> crate::Result<Command> {
569+
let runner = options.runner.unwrap_or_else(|| "cargo".into());
570+
571+
if let Some(target) = &options.target {
572+
if self.available_targets.is_none() {
573+
self.fetch_available_targets();
574+
}
575+
self.validate_target(target)?;
576+
}
577+
578+
let mut args = Vec::new();
579+
if !options.args.is_empty() {
580+
args.extend(options.args);
581+
}
582+
583+
let mut features = self.config_features.clone();
584+
if let Some(f) = options.features {
585+
features.extend(f);
586+
}
587+
if !features.is_empty() {
588+
args.push("--features".into());
589+
args.push(features.join(","));
590+
}
591+
592+
if !options.debug {
593+
args.push("--release".into());
594+
}
595+
596+
if let Some(target) = options.target {
597+
args.push("--target".into());
598+
args.push(target);
599+
}
600+
601+
let mut build_cmd = Command::new(&runner);
602+
build_cmd.arg("build");
603+
build_cmd.args(args);
604+
605+
Ok(build_cmd)
606+
}
595607
}
596608

597609
/// The `workspace` section of the app configuration (read from Cargo.toml).

0 commit comments

Comments
 (0)