diff --git a/crates/engine/src/cmd/scripts.rs b/crates/engine/src/cmd/scripts.rs index 71a99a25b2..8cec1db4f6 100644 --- a/crates/engine/src/cmd/scripts.rs +++ b/crates/engine/src/cmd/scripts.rs @@ -95,7 +95,7 @@ pub fn specs() -> Vec { "Run Startup Scripts", [], None, - "{reason: launch|menu (launch: the app starting; menu: before a script chosen from File › Scripts), then?: script.run's parameters (the script to run once the startup scripts have run), maxFuel?: whole number from 1 to 100000000000 (each startup script's instructions; default 1000000000)} run the .jsx files of the Startup Scripts folder next to the settings, one at a time, as script.run runs a file, each with the same limits as any script and its own undo step; nothing runs, and the reply says why, when the runStartupScripts preference is off (the default), in Safe Mode, or in a session without the folder (the web, and a run with VECTORCRAFT_NO_PREFS); only the folder's own .jsx files run, sorted by name without regard to case, then by name; links, hidden files, names starting with a dot, subfolders and other files are left out, and so is a script the Safe Mode choice disabled; a missing folder runs nothing; a script that cannot be read, is larger than 8 MiB, fails or is stopped is named in failed and in the Script Console, and the next one runs, then the script of then; while a startup script runs, the launch marker names it, so a launch that stops there offers to disable it; script.* commands are refused inside a script, so a startup script does not start another startup run. VectorCraft's own rules: the folder, the order, the limits of a listing (2048 folder entries, 512 scripts, 250 ms) and what a failure does → {reason, skipped: true, why: off|safeMode|noFolder, then?: script.run's reply} | {reason, skipped: false, ran: [names], failed: [{name, error}], disabled: [names], truncated, unreadable, then?: script.run's reply}, or, in the app, {reason, skipped: false, running, queued: the runs queued, then's included, failed: [{name, error}] (the runs that could not start before the first one did), disabled, truncated, unreadable, then?: script.run's reply when it ran to its end at once}, each run then reporting as script.run's do, and a later run that cannot start is named in the status bar", + "{reason: launch|menu (launch: the app starting; menu: before a script chosen from File › Scripts), then?: script.run's parameters (the script to run once the startup scripts have run), maxFuel?: whole number from 1 to 100000000000 (each startup script's instructions; default 1000000000)} run the .jsx files of the Startup Scripts folder next to the settings, one at a time, as script.run runs a file, each with the same limits as any script and its own undo step; nothing runs, and the reply says why, when the runStartupScripts preference is off (the default), in Safe Mode, or in a session without the folder (the web, and a run with VECTORCRAFT_NO_PREFS); only the folder's own .jsx files run, sorted by name without regard to case, then by name; links, hidden files, names starting with a dot, subfolders and other files are left out, and so is a script the Safe Mode choice disabled; a missing folder runs nothing; a script that cannot be read, is larger than 8 MiB, fails or is stopped is named in failed and in the Script Console, and the next one runs, then the script of then; while a startup script runs, the launch marker names it, so a launch that stops there offers to disable it; script.* commands are refused inside a script, so a startup script does not start another startup run. VectorCraft's own rules: the folder, the order, the limits of a listing (2048 folder entries, 512 scripts, 250 ms) and what a failure does → {reason, skipped: true, why: off|safeMode|noFolder, then?: script.run's reply} | {reason, skipped: false, ran: [names], failed: [{name, error}], disabled: [names], truncated, unreadable, then?: script.run's reply}, or, in the app, {reason, skipped: false, running, queued: the runs queued, then's included, failed: [{name, error}] (the runs that could not start before the first one did), disabled, truncated, unreadable, then?: script.run's reply when it ran to its end at once}, each run then reporting as script.run's do, and a later run that cannot start is named in the status bar, where the message of a startup script that fails or is stopped also stays", always, startup_run ), @@ -394,6 +394,12 @@ impl Session { } } + /// The running script is a startup script (`script.startup.run`), not the script queued after + /// them. + pub fn startup_script_running(&self) -> bool { + self.startup_loading.is_some() + } + /// Begins a run of the startup script at `path`, read now and checked as `script.run` checks a /// file, with the launch marker naming it while it runs. → the run's name, or the error that kept /// it from starting, which the Script Console also shows. @@ -534,8 +540,10 @@ fn startup_run(s: &mut Session, p: &Value) -> Result { if s.script_running() { return Err(EngineError::Busy); } - // A chosen script that cannot start ends the request before any startup script runs. + // A chosen script that cannot start ends the request before any startup script runs, and its + // parameters are checked as `script.run` checks them. if let Some(then) = &then { + super::params::check_params("script.run", then)?; prepare(s, then)?; } let root = match (s.startup_skip(), s.startup_scripts_dir()) { diff --git a/crates/engine/tests/script_startup.rs b/crates/engine/tests/script_startup.rs index 99e29ecf14..f6ca150ded 100644 --- a/crates/engine/tests/script_startup.rs +++ b/crates/engine/tests/script_startup.rs @@ -217,6 +217,13 @@ fn bad_parameters_are_refused() { ] { assert!(s.execute("script.startup.run", &p).is_err(), "{p}"); } + // `then` is checked as `script.run` checks its parameters, before any startup script runs. + write(&dir, "a.jsx", "vc.log(1);"); + let then = json!({"source": "vc.log(2);", "bogus": true}); + let alone = s.execute("script.run", &then).unwrap_err().to_string(); + let e = s.execute("script.startup.run", &json!({"reason": "menu", "then": then})).unwrap_err().to_string(); + assert_eq!(e, alone); + assert!(started(&s).is_empty(), "no startup script ran"); } #[test] diff --git a/crates/ui-egui/src/dialogs/script_run.rs b/crates/ui-egui/src/dialogs/script_run.rs index 89164b63f2..f7f005d9fa 100644 --- a/crates/ui-egui/src/dialogs/script_run.rs +++ b/crates/ui-egui/src/dialogs/script_run.rs @@ -578,12 +578,13 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) { } None => {} } + let startup = app.session.startup_script_running(); if let Some(DialogResult::Ok(answer)) = close && answer.choice == PromptChoice::Proceed && app.session.script_abandon() { let error = app.session.script_failure_text().unwrap_or_default(); - crate::script_runs::failed(app, error); + crate::script_runs::ended_with_error(app, startup, error); forget::(app); forget::(app); app.sync_views(); diff --git a/crates/ui-egui/src/script_runs.rs b/crates/ui-egui/src/script_runs.rs index c2d4d78d46..5b5ffc099b 100644 --- a/crates/ui-egui/src/script_runs.rs +++ b/crates/ui-egui/src/script_runs.rs @@ -19,6 +19,7 @@ pub fn poll(app: &mut VectorcraftApp) { return; } let name = app.session.script_name().unwrap_or_default(); + let startup = app.session.startup_script_running(); let Some(result) = app.session.script_pump() else { // A pump that started the next queued run shows the runs before it that could not start // in this frame: with none left running, the app may draw no next one. @@ -36,7 +37,7 @@ pub fn poll(app: &mut VectorcraftApp) { app.status(format!("Ran {name}")); } } - Err(e) => failed(app, e.to_string()), + Err(e) => ended_with_error(app, startup, e.to_string()), } startup_failures(app); app.sync_views(); @@ -174,6 +175,16 @@ pub fn run_at_launch(app: &mut VectorcraftApp) { } } +/// Shows why a run ended without its changes ([`failed`]). When the run was a startup script +/// (`startup`), the message also stays in the round's notes, so the message of the run that ends +/// the round does not hide it ([`end_round`]). +pub(crate) fn ended_with_error(app: &mut VectorcraftApp, startup: bool, error: String) { + failed(app, error); + if startup { + app.ui.startup_notes.push(app.ui.status.clone()); + } +} + /// Shows why a run ended without its changes. A stopped run that wrote no file says only that it /// was stopped; any other failure shows its error, which names the files left written. pub(crate) fn failed(app: &mut VectorcraftApp, error: String) { diff --git a/crates/ui-egui/src/tests/script_run.rs b/crates/ui-egui/src/tests/script_run.rs index 4022cb7c65..f7e9a33f7b 100644 --- a/crates/ui-egui/src/tests/script_run.rs +++ b/crates/ui-egui/src/tests/script_run.rs @@ -257,6 +257,27 @@ fn quitting_asks_to_stop_and_stop_undoes_the_run() { assert_eq!(app.ui.status, "The script was stopped and its changes were undone"); } +#[test] +fn a_startup_script_stopped_by_quitting_stays_in_the_status_bar() { + let mut app = app(); + let dir = folder("startup-quit"); + std::fs::write(dir.join("a.jsx"), "for (;;) {}").unwrap(); + app.session.attach_startup_scripts_dir(dir.clone()); + app.session.prefs.show_external_script_warning = false; + app.run("prefs.set", json!({"key": "runStartupScripts", "value": true})).unwrap(); + menus::run_ui_command(&mut app, "ui.runScript", &json!({ "source": DRAW, "name": "chosen" })).unwrap().unwrap(); + script_runs::poll(&mut app); + assert!(app.session.startup_script_running()); + script_run::ask_to_close(&mut app); + assert!(confirm(&mut app, CLOSE_KIND)); + finish(&mut app); + script_runs::poll(&mut app); + let stopped = "The script was stopped and its changes were undone"; + assert_eq!(app.ui.status, stopped); + assert_eq!(app.ui.status_notes.as_ref().and_then(|(_, notes)| notes.first()).map(String::as_str), Some(stopped)); + let _ = std::fs::remove_dir_all(&dir); +} + #[test] fn a_stop_names_the_files_the_run_left_written() { let mut app = app(); diff --git a/crates/ui-egui/src/tests/scripts_menu.rs b/crates/ui-egui/src/tests/scripts_menu.rs index f6cc325f05..ce1dfdfae8 100644 --- a/crates/ui-egui/src/tests/scripts_menu.rs +++ b/crates/ui-egui/src/tests/scripts_menu.rs @@ -545,10 +545,13 @@ fn a_stopped_startup_script_says_it_was_stopped_and_the_chosen_script_still_runs } script_runs::poll(&mut app); } - assert_eq!(app.ui.status, "The script was stopped and its changes were undone"); + let stopped = "The script was stopped and its changes were undone".to_string(); + assert_eq!(app.ui.status, stopped); finish(&mut app); assert_eq!(started(&app), ["a.jsx", "chosen"]); - assert_eq!(app.ui.status, "Ran chosen"); + // The stopped startup script stays in sight when the chosen script ends, with its message in the hover. + assert_eq!(app.ui.status, stopped); + assert_eq!(app.ui.status_notes, Some((stopped.clone(), vec![stopped, "Ran chosen".to_string()]))); let _ = std::fs::remove_dir_all(dir); } diff --git a/docs/mcp.md b/docs/mcp.md index 8f6a64c800..3cac78f102 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3278,8 +3278,9 @@ The rest of this section is VectorCraft's own rules: entry it cannot read, the status bar says that the folder was not read in full. A folder that is missing runs nothing. At launch and before a chosen script, when the folder is there and cannot be read, or a file is where the folder should be, the status bar says that the folder cannot be read. -- These two notes, and the names of startup scripts that could not start, stay in the status bar after the last run - ends: the status bar shows the first of them, and its hover lists them all, then the last run's message. +- These two notes, the names of startup scripts that could not start, and the message of each startup script that + failed or was stopped stay in the status bar after the last run ends: the status bar shows the first of them, and + its hover lists them all, then the last run's message. - The scripts run one at a time, sorted by name without regard to case, then by name. Each runs as `script.run` runs a file, with the same limits, and each that changes a document adds its own Run Script undo step. The chosen script runs after the last of them.