From 909c169afe0eadba5610d4e4e155e1bea793f6e4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 08:06:13 -0800 Subject: [PATCH] Scripts: queued startup runs keep the file access scope of the request that queued them In the app, script.startup.run starts only the first queued run inside the request; the later startup scripts and the chosen script start from the frame loop. Each of them now starts, and its file is read, in the scope captured when the queue was built, so a confined request's automation roots apply to every run it queued. --- crates/engine/src/cmd/scripts.rs | 11 +++++++++-- crates/engine/src/lib.rs | 3 +++ crates/engine/src/script/mod.rs | 1 + crates/engine/src/session/documents.rs | 1 + crates/engine/tests/script_startup.rs | 27 ++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/scripts.rs b/crates/engine/src/cmd/scripts.rs index 0484e170e0..c363fc4281 100644 --- a/crates/engine/src/cmd/scripts.rs +++ b/crates/engine/src/cmd/scripts.rs @@ -449,17 +449,20 @@ impl Session { pub(crate) fn start_queued(&mut self) -> Option { // Held aside while a run starts, so that the queue does not count as a running script then. let mut queue = std::mem::take(&mut self.script_queue); + // Each run starts in the file access scope of the request that queued it, which the run + // keeps, and not in the scope of the frame that starts it. + let scope = self.script_queue_scope.clone().unwrap_or_else(crate::file_access::ScopeHandle::capture); let mut reply = None; while self.script_key.is_none() { let Some(next) = queue.pop_front() else { break }; match next { script::startup::Queued::Startup { path, fuel } => { - if let Err(e) = self.start_startup(&path, fuel) { + if let Err(e) = scope.clone().run(|| self.start_startup(&path, fuel)) { self.startup_failures.push((file_name_of(&path), e)); } } script::startup::Queued::Then(params) => { - match run(self, ¶ms) { + match scope.clone().run(|| run(self, ¶ms)) { Ok(_) if self.script_key.is_some() => {} Ok(v) => reply = Some(v), Err(e) => self.startup_failures.push((then_name(¶ms), e.to_string())), @@ -468,6 +471,9 @@ impl Session { } } } + if queue.is_empty() { + self.script_queue_scope = None; + } self.script_queue = queue; reply } @@ -553,6 +559,7 @@ fn startup_run(s: &mut Session, p: &Value) -> Result { if s.script_frames { s.script_queue = scripts.into_iter().map(|path| script::startup::Queued::Startup { path, fuel }).chain(then.map(script::startup::Queued::Then)).collect(); + s.script_queue_scope = Some(crate::file_access::ScopeHandle::capture()); let queued = s.script_queue.len(); s.startup_failures.clear(); let then = s.start_queued(); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 97bf9f1a48..6163d1abe0 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -314,6 +314,9 @@ pub struct Session { /// The runs `script.startup.run` queued in the app, each started when the run before it ends /// ([`Session::script_pump`]). pub(crate) script_queue: std::collections::VecDeque, + /// The file access scope of the request that queued [`Self::script_queue`], in which each queued + /// run starts and is read. + pub(crate) script_queue_scope: Option, /// While a startup script runs: the launch marker's entry naming it /// ([`vectorcraft_plugins::safe_mode::loading`]). pub(crate) startup_loading: Option, diff --git a/crates/engine/src/script/mod.rs b/crates/engine/src/script/mod.rs index d006f0500d..f877e867eb 100644 --- a/crates/engine/src/script/mod.rs +++ b/crates/engine/src/script/mod.rs @@ -1884,6 +1884,7 @@ impl Session { pub fn script_abandon(&mut self) -> bool { // The runs queued after it are not started. self.script_queue.clear(); + self.script_queue_scope = None; self.startup_ended(); let Some(key) = self.script_key.take() else { return false }; let Some(job) = JOBS.with(|jobs| jobs.borrow_mut().remove(&key.id)) else { diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs index 7b58bba1fd..61df37240f 100644 --- a/crates/engine/src/session/documents.rs +++ b/crates/engine/src/session/documents.rs @@ -170,6 +170,7 @@ impl Session { script_runs: 0, script_frames: false, script_queue: Default::default(), + script_queue_scope: None, startup_loading: None, startup_failures: vec![], script_high_water: Default::default(), diff --git a/crates/engine/tests/script_startup.rs b/crates/engine/tests/script_startup.rs index f0135636d4..fca883cfc0 100644 --- a/crates/engine/tests/script_startup.rs +++ b/crates/engine/tests/script_startup.rs @@ -353,3 +353,30 @@ fn in_the_app_startup_scripts_that_cannot_start_are_in_the_reply() { assert_eq!(r["failed"], json!([{"name": "big.jsx", "error": vectorcraft_engine::cmd::scripts::TOO_LARGE}])); assert!(s.take_startup_failures().is_empty(), "the reply names them, so the status bar does not again"); } + +#[test] +fn in_the_app_every_queued_run_keeps_the_automation_roots_of_the_request_that_queued_it() { + let _f = fresh(); + let dir = tmp_dir("app-roots"); + // Writes a file in the run's temporary folder, which is outside the write roots below. + let source = "var ok; try { var t = new File(Folder.temp.fsName + '/t.txt'); ok = t.open('w'); t.close(); } catch (e) { ok = false; }\n\ + vc.log(ok ? 'wrote' : 'refused');"; + write(&dir, "a.jsx", source); + write(&dir, "b.jsx", source); + let roots = vectorcraft_engine::file_access::AutomationRoots::new(&[dir.as_path()], &[]).unwrap().unwrap(); + for confined in [false, true] { + let mut s = session(&dir); + s.script_frames = true; + let params = json!({"reason": "menu", "then": {"source": source, "name": "chosen"}}); + let mut request = || s.execute("script.startup.run", ¶ms).unwrap(); + let r = if confined { vectorcraft_engine::file_access::confine(Some(&roots), request) } else { request() }; + assert_eq!(r["queued"], json!(3)); + // The frames that start the later runs are outside the request. + while s.script_running() { + s.script_pump(); + } + assert_eq!(started(&s), ["a.jsx", "b.jsx", "chosen"]); + let want = if confined { "refused" } else { "wrote" }; + assert_eq!(console(&s, "log"), [want, want, want], "confined {confined}"); + } +}