Skip to content
Merged
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
2 changes: 1 addition & 1 deletion crates/fastled-cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
31 changes: 4 additions & 27 deletions crates/fastled-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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"}"#,
);
}
}
}
Expand Down
76 changes: 75 additions & 1 deletion crates/fastled-cli/src/compile_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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::<String>(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"), "<html></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"), "<html></html>").unwrap();
let (tx, mut rx) = tokio::sync::broadcast::channel::<String>(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}");
}
}
66 changes: 62 additions & 4 deletions crates/fastled-cli/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const LOADING_PAGE: &str = r#"<!DOCTYPE html>
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; }
</style>
<script>
// Instant-launch flow (#148):
Expand All @@ -76,6 +79,11 @@ const LOADING_PAGE: &str = r#"<!DOCTYPE html>
const header = document.getElementById('header');
let buildStart = Date.now();
let elapsedTimer = null;
// Auto-scroll follows the log tail unless the user scrolled up to read.
let userScrolled = false;
log.addEventListener('scroll', () => {
userScrolled = log.scrollTop + log.clientHeight < log.scrollHeight - 10;
});

function startTimer() {
stopTimer();
Expand All @@ -92,17 +100,36 @@ const LOADING_PAGE: &str = r#"<!DOCTYPE html>
spinner.classList.remove('error');
status.textContent = message || 'Compiling...';
log.textContent = '';
userScrolled = false;
startTimer();
}
function setError(message) {
header.classList.add('error');
spinner.classList.add('error');
status.textContent = message || 'Compilation failed';
stopTimer();
// Jump to the first diagnostic: emcc errors are usually followed by
// long noise, so the bottom of the log is the wrong place to land.
const firstErr = log.querySelector('.err');
if (firstErr) {
log.scrollTop = Math.max(0, firstErr.offsetTop - log.offsetTop - 8);
}
}
function classifyLine(line, stream) {
const l = line.toLowerCase();
if (l.includes('error:') || l.includes('fatal error') ||
l.includes('undefined symbol') ||
(stream === 'stderr' && l.includes('failed'))) { return 'err'; }
if (l.includes('warning:')) { return 'warn'; }
return '';
}
function appendLog(line) {
log.textContent += line + '\n';
log.scrollTop = log.scrollHeight;
function appendLog(line, stream) {
const div = document.createElement('div');
div.textContent = line;
const cls = classifyLine(line, stream || 'stdout');
if (cls) { div.className = cls; }
log.appendChild(div);
if (!userScrolled) { log.scrollTop = log.scrollHeight; }
}

startTimer();
Expand All @@ -129,7 +156,7 @@ const LOADING_PAGE: &str = r#"<!DOCTYPE html>
try {
const d = JSON.parse(e.data);
if (d.type === 'log') {
appendLog(d.line);
appendLog(d.line, d.stream);
} else if (d.type === 'status') {
handleStatus(d.status, d.message);
if (d.status === 'success') { es.close(); }
Expand Down Expand Up @@ -700,6 +727,37 @@ mod tests {
);
}

/// Live compile log UX (#153): the loading page must classify and color
/// warning/error lines, follow the log tail, and never show a fake
/// progress bar (build step counts are unknowable).
#[test]
fn loading_page_colors_log_lines_and_has_no_progress_bar() {
assert!(
LOADING_PAGE.contains("classifyLine"),
"loading page must classify log lines"
);
assert!(
LOADING_PAGE.contains("warning:") && LOADING_PAGE.contains(".warn"),
"warnings must get the yellow .warn style"
);
assert!(
LOADING_PAGE.contains("error:") && LOADING_PAGE.contains(".err"),
"errors must get the red .err style"
);
assert!(
LOADING_PAGE.contains("spinner"),
"indeterminate spinner is the only activity indicator"
);
assert!(
!LOADING_PAGE.contains("<progress"),
"no progress bar: build step counts are unknowable"
);
assert!(
LOADING_PAGE.contains("userScrolled"),
"auto-scroll must pause when the user scrolls up"
);
}

// ------------------------------------------------------------------
// DWARF source endpoint tests
// ------------------------------------------------------------------
Expand Down
Loading
Loading