mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-20 09:05:51 +00:00
Merge main into fix/plugin-library-file-url-unc
This commit is contained in:
commit
765fd76552
798 files changed
+91231
-51980
No files matched your search
@@ -4,14 +4,28 @@
|
||||
//! panics leaves the project as it was. This is a safety net, not a substitute for returning
|
||||
//! errors (AGENTS.md, "Never crash").
|
||||
|
||||
use std::cell::Cell;
|
||||
|
||||
use crate::{EngineError, Result};
|
||||
|
||||
thread_local! {
|
||||
/// Panics [`guarded`] caught on this thread (see [`caught`]).
|
||||
static CAUGHT: Cell<u64> = const { Cell::new(0) };
|
||||
}
|
||||
|
||||
/// How many panics [`guarded`] has caught on this thread so far: compare two readings to learn
|
||||
/// whether a call panicked (crash repros, `effectcraft-cli replay`).
|
||||
pub fn caught() -> u64 {
|
||||
CAUGHT.with(Cell::get)
|
||||
}
|
||||
|
||||
/// Run `f`; a panic inside it becomes an error naming `what`. The panic itself is logged by
|
||||
/// the panic hook ([`crate::logging::install_panic_hook`]).
|
||||
pub fn guarded<T>(what: &str, f: impl FnOnce() -> Result<T>) -> Result<T> {
|
||||
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) {
|
||||
Ok(r) => r,
|
||||
Err(payload) => {
|
||||
CAUGHT.with(|c| c.set(c.get().saturating_add(1)));
|
||||
let msg = payload
|
||||
.downcast_ref::<&str>()
|
||||
.map(|s| s.to_string())
|
||||
|
||||
@@ -304,7 +304,8 @@ pub fn parse_catalog(bytes: &[u8]) -> std::result::Result<(Catalog, Vec<String>)
|
||||
fn runs_here(r: &Release) -> bool {
|
||||
// Without a pre-release suffix (`0.18.0-dev` is 0.18.0).
|
||||
let app = env!("CARGO_PKG_VERSION").split(['-', '+']).next().unwrap_or_default();
|
||||
r.api == crate::effects::plugin::WASM_API_VERSION && r.min_app.as_deref().is_none_or(|m| !newer(m, app))
|
||||
(crate::effects::plugin::MIN_WASM_API_VERSION..=crate::effects::plugin::WASM_API_VERSION).contains(&r.api)
|
||||
&& r.min_app.as_deref().is_none_or(|m| !newer(m, app))
|
||||
}
|
||||
|
||||
/// The newest release this build can run (plug-in API and minimum app version).
|
||||
@@ -806,12 +807,13 @@ fn pick(s: &Session, c: &str, p: &Value) -> Result<(String, Package, Release)> {
|
||||
return Err(bad(
|
||||
c,
|
||||
format!(
|
||||
"{} {} needs plug-in API {} and EffectCraft {} or later; this is EffectCraft {} with WebAssembly plug-in API {}",
|
||||
"{} {} needs WebAssembly ABI {} and EffectCraft {} or later; this is EffectCraft {} with WebAssembly ABI {} to {}",
|
||||
pkg.name,
|
||||
rel.version,
|
||||
rel.api,
|
||||
rel.min_app.as_deref().unwrap_or("(any)"),
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
crate::effects::plugin::MIN_WASM_API_VERSION,
|
||||
crate::effects::plugin::WASM_API_VERSION
|
||||
),
|
||||
));
|
||||
@@ -1142,8 +1144,9 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
assert_eq!(latest_compatible(&p).map(|r| r.version.as_str()), Some("1.2.0"));
|
||||
assert!(runs_here(&rel("1.0.0", 1, None)), "API 1 modules still run on a build with a newer plug-in API");
|
||||
assert!(!runs_here(&rel("1.0.0", 2, None)), "WebAssembly modules are API 1: an API 2 release can't load");
|
||||
assert!(runs_here(&rel("1.0.0", 1, None)), "ABI 1 modules still run on a build with a newer ABI");
|
||||
assert!(runs_here(&rel("1.0.0", 2, None)), "ABI 2 modules run on this build");
|
||||
assert!(!runs_here(&rel("1.0.0", 3, None)), "a WebAssembly ABI this build doesn't implement can't load");
|
||||
let j = package_json(&p, None, &Status::default());
|
||||
assert_eq!(j["needsNewerApp"], true);
|
||||
assert!(latest_compatible(&Package::default()).is_none());
|
||||
|
||||
@@ -125,7 +125,13 @@ fn utc_parts(secs: u64, nanos: u32) -> (i64, chrono::NaiveDateTime) {
|
||||
|
||||
/// One log line (without the newline): `<timestamp> <LEVEL> [<thread>] <target>: <message>`.
|
||||
fn format_line(ts: &str, level: log::Level, thread: &str, target: &str, message: &str) -> String {
|
||||
let message = message.strip_suffix('\n').unwrap_or(message);
|
||||
// CRLF becomes LF first, so a message ending in "\r\n" loses its final break like one ending
|
||||
// in "\n" (and its carriage return is not taken for a lone one).
|
||||
let message = message.replace("\r\n", "\n");
|
||||
let message = message.strip_suffix('\n').unwrap_or(&message);
|
||||
// A message's own line breaks (a path or name from a project or a script) continue it on
|
||||
// indented lines, so they can't pass for records of their own.
|
||||
let message = message.replace('\r', "\\r").replace('\n', "\n\t");
|
||||
format!("{ts} {level:<5} [{thread}] {target}: {message}")
|
||||
}
|
||||
|
||||
@@ -214,16 +220,31 @@ fn rotate(dir: &Path) -> std::io::Result<(PathBuf, File)> {
|
||||
}
|
||||
}
|
||||
let path = numbered(dir, 0);
|
||||
let file = File::create(&path)?;
|
||||
let file = create_session_file(&path)?;
|
||||
Ok((path, file))
|
||||
}
|
||||
|
||||
// Another process may create the current log after our rotation. Never truncate its
|
||||
// file: its writer and crash reporter may already hold the same inode open.
|
||||
fn create_session_file(path: &Path) -> std::io::Result<File> {
|
||||
std::fs::OpenOptions::new().write(true).create_new(true).open(path)
|
||||
}
|
||||
|
||||
/// Rotate the session logs in `dir` (the `Logs` folder, see [`log_dir`]) and write records to
|
||||
/// a fresh `effectcraft.log` there, first the ones logged so far; returns its path.
|
||||
pub fn attach_session_log(dir: &Path) -> Result<PathBuf, String> {
|
||||
attach_session_log_with_header(dir, "")
|
||||
}
|
||||
|
||||
/// Open a session log with a bounded, unfiltered identity header before buffered records.
|
||||
/// A crash reporter can then recognize this run even with logging disabled or verbose startup.
|
||||
pub fn attach_session_log_with_header(dir: &Path, header: &str) -> Result<PathBuf, String> {
|
||||
if header.len() > 1024 {
|
||||
return Err("session log header exceeds 1024 bytes".into());
|
||||
}
|
||||
let (path, file) = rotate(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
|
||||
let mut sink = SESSION.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
sink.attach(file);
|
||||
sink.attach_with_header(file, header);
|
||||
sink.path = Some(path.clone());
|
||||
Ok(path)
|
||||
}
|
||||
@@ -264,8 +285,14 @@ impl Sink {
|
||||
}
|
||||
|
||||
/// Start writing to `file`, first the lines kept so far.
|
||||
#[cfg(test)]
|
||||
fn attach(&mut self, file: File) {
|
||||
self.attach_with_header(file, "");
|
||||
}
|
||||
|
||||
fn attach_with_header(&mut self, file: File, header: &str) {
|
||||
self.file = Some(file);
|
||||
self.append(header);
|
||||
for line in std::mem::take(&mut self.pending) {
|
||||
self.append(&line);
|
||||
}
|
||||
@@ -333,6 +360,22 @@ mod tests {
|
||||
use super::*;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
|
||||
#[test]
|
||||
fn identity_header_precedes_buffered_records_independent_of_log_filter() {
|
||||
let dir = temp_dir("identity-header");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("session");
|
||||
let mut sink = Sink::new(MAX_FILE_BYTES);
|
||||
sink.write(&"earlier startup record".repeat(1000));
|
||||
sink.attach_with_header(File::create(&path).unwrap(), "crash-report session 1-2\n");
|
||||
let text = std::fs::read_to_string(&path).unwrap();
|
||||
assert!(text.starts_with("crash-report session 1-2\n"));
|
||||
assert!(text.contains("earlier startup record"));
|
||||
assert!(attach_session_log_with_header(&dir, &"x".repeat(1025)).is_err());
|
||||
drop(sink);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// `RUST_LOG` picks what start-up prints to stderr (#234).
|
||||
#[test]
|
||||
fn rust_log_filters_by_level_and_target() {
|
||||
@@ -397,6 +440,20 @@ mod tests {
|
||||
assert_eq!(timestamp(u64::MAX), "584556019-04-03T14:25:51.615Z");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn concurrent_session_creation_does_not_truncate_the_winner() {
|
||||
let dir = temp_dir("concurrent-create");
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join(SESSION_LOG);
|
||||
let mut winner = create_session_file(&path).unwrap();
|
||||
winner.write_all(b"crash-report session 1-2\nwinner content\n").unwrap();
|
||||
assert_eq!(create_session_file(&path).unwrap_err().kind(), std::io::ErrorKind::AlreadyExists);
|
||||
winner.write_all(b"still the same session\n").unwrap();
|
||||
assert_eq!(read(&path), "crash-report session 1-2\nwinner content\nstill the same session\n");
|
||||
drop(winner);
|
||||
std::fs::remove_dir_all(dir).unwrap();
|
||||
}
|
||||
|
||||
/// Each launch keeps the previous runs' logs, so the log of a run that crashed survives.
|
||||
#[test]
|
||||
fn rotation_keeps_the_previous_logs_and_starts_an_empty_file() {
|
||||
@@ -472,4 +529,22 @@ mod tests {
|
||||
"2026-10-08T07:59:17.728Z WARN [main] effectcraft_engine::autosave: disk full"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_breaks_in_a_message_continue_it_on_indented_lines() {
|
||||
let forged = "cache at /tmp/x\n2026-10-08T07:59:18.000Z ERROR [main] effectcraft_engine: forged\rend";
|
||||
let line = format_line("2026-10-08T07:59:17.728Z", log::Level::Info, "main", "t", forged);
|
||||
assert_eq!(line.lines().count(), 2);
|
||||
assert!(line.lines().nth(1).unwrap().starts_with('\t'), "{line}");
|
||||
assert!(!line.contains('\r'), "{line}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_trailing_crlf_is_one_line_break_not_a_lone_carriage_return() {
|
||||
let line = |m| format_line("t", log::Level::Info, "main", "t", m);
|
||||
assert_eq!(line("cache\r\n"), line("cache"));
|
||||
assert_eq!(line("a\r\nb\r\n"), line("a\nb"));
|
||||
// A lone one, even at the end, is still escaped.
|
||||
assert!(line("cache\r").ends_with("cache\\r"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user