Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(core): regression in handling of bulk actions
Elian Dorandc79b2e54517
diff --git a/packages/trilium-core/src/services/bulk_actions.spec.ts b/packages/trilium-core/src/services/bulk_actions.spec.tsindex f6d0824541..0a5d11122c 100644--- a/packages/trilium-core/src/services/bulk_actions.spec.ts+++ b/packages/trilium-core/src/services/bulk_actions.spec.ts@@ -9,6 +9,7 @@ import cloningService from "./cloning.js";import config from "./config.js";import { getContext } from "./context.js";import noteService from "./notes.js";+import { getSql } from "./sql/index.js";/*** Wraps a callback in a CLS context. Entity mutations (createNewNote,@@ -209,6 +210,24 @@ describe("bulk_actions service (real DB)", () => {expect(note.note.getOwnedLabelValue("scripted")).toBe("yes");});+ it("persists mutations even when the script returns early", () => {+ // A top-level `return` (used to exit early) must not skip the implicit+ // note.save(). A title change only reaches the DB via note.save(), so we+ // assert against the persisted row (the in-memory becca entity reflects the+ // mutation regardless of whether save() ran).+ const note = createNote("root");++ withContext(() =>+ bulkActionService.executeActions(+ [{ name: "executeScript", script: "note.title = 'renamed by script';\nreturn;" }],+ [note.note.noteId]+ )+ );++ const persistedTitle = getSql().getValue<string>("SELECT title FROM notes WHERE noteId = ?", [note.note.noteId]);+ expect(persistedTitle).toBe("renamed by script");+ });+it("is a no-op for an empty / whitespace-only script", () => {const note = createNote("root");const labelsBefore = note.note.getOwnedAttributes().length;diff --git a/packages/trilium-core/src/services/bulk_actions.ts b/packages/trilium-core/src/services/bulk_actions.tsindex b289737cfa..b06e2549b0 100644--- a/packages/trilium-core/src/services/bulk_actions.ts+++ b/packages/trilium-core/src/services/bulk_actions.ts@@ -117,10 +117,13 @@ const ACTION_HANDLERS: ActionHandlerMap = {// Route through the script service's executeBundle instead of raw// new Function() to get proper CLS context, logging, and error handling.// The preamble provides access to `note` and `api` as the UI documents.+ // The user script runs inside an IIFE so that a top-level `return` (used to+ // exit early) only exits the user code; note.save() still runs afterwards,+ // matching the previous behavior where save() was called unconditionally.const noteId = note.noteId.replace(/[^a-zA-Z0-9_]/g, "");const preamble = `const api = apiContext.apis["${noteId}"] || {};\n` +`const note = apiContext.notes["${noteId}"];\n`;- const scriptBody = `${preamble}${action.script}\nnote.save();`;+ const scriptBody = `${preamble}(function() {\n${action.script}\n})();\nnote.save();`;executeBundle({note: note,