From 31d46f61d79779c39834e978536b8489ba708e53 Mon Sep 17 00:00:00 2001 From: zackees Date: Thu, 11 Jun 2026 14:30:55 -0700 Subject: [PATCH] feat: stream build tool output live to the browser with colored diagnostics Build subprocesses previously ran with inherited stdio, so the loading page's SSE log panel only received a one-line summary after the build finished. Spawn them with piped stdout/stderr instead, forwarding each line through a LogSink to both the terminal and the SSE broadcast as it is produced (PYTHONUNBUFFERED keeps the Python-driven tools line-buffered). The loading page now renders per-line log entries with warnings in yellow and errors in red, follows the tail unless the user scrolls up, and jumps to the first diagnostic on failure. Success is only reported when index.html actually exists on disk, so a fatal compile or link error can never reload the page into a blank view; the SSE connection stays open so a later fixed rebuild recovers in the same session. Closes #153 Co-Authored-By: Claude Opus 4.7 --- crates/fastled-cli/src/build.rs | 2 +- crates/fastled-cli/src/commands.rs | 31 +--- crates/fastled-cli/src/compile_stream.rs | 76 +++++++- crates/fastled-cli/src/server.rs | 66 ++++++- crates/fastled-cli/src/wasm_build.rs | 213 ++++++++++++++++++++--- 5 files changed, 327 insertions(+), 61 deletions(-) diff --git a/crates/fastled-cli/src/build.rs b/crates/fastled-cli/src/build.rs index 8500ed79..12e51fc1 100644 --- a/crates/fastled-cli/src/build.rs +++ b/crates/fastled-cli/src/build.rs @@ -3,4 +3,4 @@ //! The Rust CLI owns the build backend directly. Python remains a compatibility //! API layer and is not part of the primary compile path. -pub use crate::wasm_build::{run_build, BuildMode, BuildRequest, BuildResult}; +pub use crate::wasm_build::{run_build, run_build_streaming, BuildMode, BuildRequest, BuildResult}; diff --git a/crates/fastled-cli/src/commands.rs b/crates/fastled-cli/src/commands.rs index 4feaf1fe..9c16a95c 100644 --- a/crates/fastled-cli/src/commands.rs +++ b/crates/fastled-cli/src/commands.rs @@ -5,8 +5,8 @@ use std::sync::{Arc, RwLock}; use crate::build; use crate::cli::{requested_init_ref, validate_init_ref_flags, Cli}; use crate::compile_stream::{ - ensure_compile_prerequisites, purge_fastled_cache, run_native_compile_streaming, - selected_build_mode, send_sse, write_build_status, + ensure_compile_prerequisites, purge_fastled_cache, report_build_outcome, + run_native_compile_streaming, selected_build_mode, send_sse, write_build_status, }; use crate::debug_symbols; use crate::dwarf_smoke::run_dwarf_source_smoke; @@ -96,20 +96,7 @@ pub(crate) fn compile_and_serve(dir: &str, cli: &Cli) -> ExitCode { let success = run_native_compile_streaming(cli, &sketch_dir, cli.purge, &tx, &debug_symbols); - - if success { - write_build_status(&output_dir, "success", "Done"); - send_sse( - &tx, - r#"{"type":"status","status":"success","message":"Done"}"#, - ); - } else { - write_build_status(&output_dir, "error", "Compilation failed"); - send_sse( - &tx, - r#"{"type":"status","status":"error","message":"Compilation failed"}"#, - ); - } + report_build_outcome(&output_dir, &tx, success); // --- Watch mode --------------------------------------------------------- println!("\nWill recompile on sketch changes or space bar press."); @@ -147,20 +134,10 @@ pub(crate) fn compile_and_serve(dir: &str, cli: &Cli) -> ExitCode { let success = run_native_compile_streaming(cli, &sketch_dir, false, &tx, &debug_symbols); - if success { + if report_build_outcome(&output_dir, &tx, success) { println!("Recompilation successful!"); - write_build_status(&output_dir, "success", "Done"); - send_sse( - &tx, - r#"{"type":"status","status":"success","message":"Done"}"#, - ); } else { eprintln!("Recompilation failed."); - write_build_status(&output_dir, "error", "Compilation failed"); - send_sse( - &tx, - r#"{"type":"status","status":"error","message":"Compilation failed"}"#, - ); } } } diff --git a/crates/fastled-cli/src/compile_stream.rs b/crates/fastled-cli/src/compile_stream.rs index 31754151..074c0830 100644 --- a/crates/fastled-cli/src/compile_stream.rs +++ b/crates/fastled-cli/src/compile_stream.rs @@ -173,6 +173,41 @@ pub(crate) fn emit_build_result_logs( } } +/// Publish the build outcome to polling (`build-status.json`) and SSE +/// clients. `success` is only reported when the build claims success *and* +/// `index.html` actually exists on disk — the loading page reloads on +/// `success`, and reloading without artifacts would wipe the error log (#153). +/// Returns the effective success that was reported. +pub(crate) fn report_build_outcome( + output_dir: &Path, + tx: &tokio::sync::broadcast::Sender, + build_ok: bool, +) -> bool { + let index_exists = output_dir.join("index.html").is_file(); + if build_ok && index_exists { + write_build_status(output_dir, "success", "Done"); + send_sse( + tx, + r#"{"type":"status","status":"success","message":"Done"}"#, + ); + return true; + } + let message = if build_ok { + "Build completed but no index.html was produced" + } else { + "Compilation failed" + }; + write_build_status(output_dir, "error", message); + send_sse( + tx, + &format!( + r#"{{"type":"status","status":"error","message":"{}"}}"#, + json_escape(message) + ), + ); + false +} + /// Run the native build path and mirror its output to the terminal + SSE. pub(crate) fn run_native_compile_streaming( cli: &Cli, @@ -193,7 +228,8 @@ pub(crate) fn run_native_compile_streaming( force_clean, }; - match build::run_build(&request) { + let log = |line: &str, stream: &str| emit_build_log(tx, line, stream); + match build::run_build_streaming(&request, &log) { Ok(result) => { emit_build_result_logs(&result, tx); if result.success { @@ -235,3 +271,41 @@ pub(crate) fn update_debug_symbol_resolver( *guard = Some(resolver); } } + +#[cfg(test)] +mod tests { + use super::*; + + fn read_status(dir: &Path) -> String { + std::fs::read_to_string(dir.join("build-status.json")).unwrap() + } + + #[test] + fn report_build_outcome_success_requires_index_html() { + let dir = tempfile::tempdir().unwrap(); + let (tx, mut rx) = tokio::sync::broadcast::channel::(8); + + assert!(!report_build_outcome(dir.path(), &tx, true)); + assert!(read_status(dir.path()).contains("\"error\"")); + let event = rx.try_recv().unwrap(); + assert!(event.contains("\"status\":\"error\""), "got: {event}"); + + std::fs::write(dir.path().join("index.html"), "").unwrap(); + assert!(report_build_outcome(dir.path(), &tx, true)); + assert!(read_status(dir.path()).contains("\"success\"")); + let event = rx.try_recv().unwrap(); + assert!(event.contains("\"status\":\"success\""), "got: {event}"); + } + + #[test] + fn report_build_outcome_failure_is_error_even_with_stale_index() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("index.html"), "").unwrap(); + let (tx, mut rx) = tokio::sync::broadcast::channel::(8); + + assert!(!report_build_outcome(dir.path(), &tx, false)); + assert!(read_status(dir.path()).contains("\"error\"")); + let event = rx.try_recv().unwrap(); + assert!(event.contains("Compilation failed"), "got: {event}"); + } +} diff --git a/crates/fastled-cli/src/server.rs b/crates/fastled-cli/src/server.rs index eb991a8c..eda538fe 100644 --- a/crates/fastled-cli/src/server.rs +++ b/crates/fastled-cli/src/server.rs @@ -59,6 +59,9 @@ const LOADING_PAGE: &str = r#" overflow-y: auto; font-size: 0.85em; color: #aaa; white-space: pre-wrap; text-align: left; line-height: 1.4; padding: 8px; background: #1a1a1a; border-radius: 4px; } + #log div { min-height: 1.2em; } + #log .warn { color: #ffd54f; } + #log .err { color: #ef5350; }