Skip to content

Build your personal knowledge base with Trilium Notes

fix(scripting): move backend-scripting toast to its own module to break an import cycle

Elian Dorane06d630d9479
diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts
index 9c2f859b8e..ea6551775f 100644
--- a/apps/client/src/components/entrypoints.ts
+++ b/apps/client/src/components/entrypoints.ts
@@ -1,9 +1,9 @@
import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons";
+import { showBackendScriptingDisabledToast } from "../services/backend_scripting.js";
import bundleService from "../services/bundle.js";
import dialog from "../services/dialog.js";
import dateNoteService from "../services/date_notes.js";
-import { showBackendScriptingDisabledToast } from "../services/frontend_script_api.js";
import froca from "../services/froca.js";
import { t } from "../services/i18n.js";
import linkService from "../services/link.js";
diff --git a/apps/client/src/services/backend_scripting.ts b/apps/client/src/services/backend_scripting.ts
new file mode 100644
index 0000000000..5f80830b2b
--- /dev/null
+++ b/apps/client/src/services/backend_scripting.ts
@@ -0,0 +1,61 @@
+import appContext from "../components/app_context.js";
+import { t } from "./i18n.js";
+import toastService from "./toast.js";
+import { openInAppHelpFromUrl } from "./utils.js";
+
+/**
+ * Thrown by `api.runOnBackend()` / `api.runAsyncOnBackendWithManualTransactionHandling()` when
+ * backend scripting is disabled on the server. The bundle executor recognizes this so it can skip
+ * its per-note error toast — the single deduplicated toast raised alongside it is enough.
+ *
+ * This lives in its own module (rather than in `frontend_script_api.ts`) so `entrypoints`/`bundle`
+ * can import it as a value without eagerly pulling in the frontend script API's widget graph, which
+ * triggers a circular-import initialization error.
+ */
+export class BackendScriptingDisabledError extends Error {
+ constructor() {
+ super("Backend script execution is disabled.");
+ this.name = "BackendScriptingDisabledError";
+ }
+}
+
+// The notes that attempted to run backend code while it was disabled, accumulated so the single
+// deduplicated toast can list them all as reference links. Cleared when the toast is removed.
+const attempts = new Set<string>();
+
+/**
+ * Shows the single deduplicated "backend scripting is disabled" toast, adding the given note to the
+ * list of scripts that tried to run backend code. Safe to call from any backend-execution entry
+ * point — a frontend `runOnBackend()` call, or executing a backend code note directly.
+ */
+export function showBackendScriptingDisabledToast(noteId: string) {
+ attempts.add(noteId);
+ toastService.showPersistent({
+ id: "backend-scripting-disabled",
+ icon: "bx bx-code-block",
+ title: t("frontend_script_api.backend_scripting_disabled_title"),
+ message: t("frontend_script_api.backend_scripting_disabled_message"),
+ notesHeading: t("frontend_script_api.backend_scripting_disabled_notes_heading"),
+ noteIds: [ ...attempts ],
+ wide: true,
+ timeout: 60_000,
+ onRemove: () => attempts.clear(),
+ buttons: [
+ {
+ text: t("frontend_script_api.backend_scripting_disabled_open_settings"),
+ onClick: ({ dismissToast }) => {
+ appContext.triggerCommand("showOptions", { section: "_optionsSecurity" });
+ dismissToast();
+ }
+ },
+ {
+ text: t("frontend_script_api.backend_scripting_disabled_more_info"),
+ onClick: ({ dismissToast }) => {
+ // openInAppHelpFromUrl takes the help note ID without the "_help_" prefix.
+ openInAppHelpFromUrl("fiHicjpHjIRJ");
+ dismissToast();
+ }
+ }
+ ]
+ });
+}
diff --git a/apps/client/src/services/bundle.spec.ts b/apps/client/src/services/bundle.spec.ts
index 5bf30c459b..544a6c41b1 100644
--- a/apps/client/src/services/bundle.spec.ts
+++ b/apps/client/src/services/bundle.spec.ts
@@ -60,7 +60,7 @@ describe("executeBundle / executeBundleWithoutErrorHandling", () => {
});
it("isBackendScriptingDisabled detects the error anywhere in the cause chain", async () => {
- const { BackendScriptingDisabledError } = await import("./frontend_script_api.js");
+ const { BackendScriptingDisabledError } = await import("./backend_scripting.js");
// The bundler nests the real error as a cause under its "Load of script note …" wrapper.
const wrapped = new Error(`Load of script note "X" (id) failed with: disabled`, { cause: new BackendScriptingDisabledError() });
expect(isBackendScriptingDisabled(wrapped)).toBe(true);
diff --git a/apps/client/src/services/bundle.ts b/apps/client/src/services/bundle.ts
index 73798eeadf..6ef3bf62d2 100644
--- a/apps/client/src/services/bundle.ts
+++ b/apps/client/src/services/bundle.ts
@@ -4,7 +4,8 @@ import { h, VNode } from "preact";
import FNote from "../entities/fnote.js";
import BasicWidget, { ReactWrappedWidget } from "../widgets/basic_widget.js";
import RightPanelWidget from "../widgets/right_panel_widget.js";
-import { BackendScriptingDisabledError, type Entity } from "./frontend_script_api.js";
+import { BackendScriptingDisabledError } from "./backend_scripting.js";
+import type { Entity } from "./frontend_script_api.js";
import { WidgetDefinitionWithType } from "./frontend_script_api_preact.js";
import { t } from "./i18n.js";
import ScriptContext from "./script_context.js";
diff --git a/apps/client/src/services/frontend_script_api.spec.ts b/apps/client/src/services/frontend_script_api.spec.ts
index 8deec2be42..1f2055684b 100644
--- a/apps/client/src/services/frontend_script_api.spec.ts
+++ b/apps/client/src/services/frontend_script_api.spec.ts
@@ -3,9 +3,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import appContext from "../components/app_context.js";
import type FNote from "../entities/fnote.js";
import { buildNote } from "../test/easy-froca.js";
+import { BackendScriptingDisabledError } from "./backend_scripting.js";
import dateNotesService from "./date_notes.js";
import dialogService from "./dialog.js";
-import FrontendScriptApi, { BackendScriptingDisabledError, type Api, type Entity } from "./frontend_script_api.js";
+import FrontendScriptApi, { type Api, type Entity } from "./frontend_script_api.js";
import { preactAPI } from "./frontend_script_api_preact.js";
import froca from "./froca.js";
import linkService from "./link.js";
diff --git a/apps/client/src/services/frontend_script_api.ts b/apps/client/src/services/frontend_script_api.ts
index ea3eb27caf..cc9f9c8d44 100644
--- a/apps/client/src/services/frontend_script_api.ts
+++ b/apps/client/src/services/frontend_script_api.ts
@@ -10,6 +10,7 @@ import type FNote from "../entities/fnote.js";
import BasicWidget, { ReactWrappedWidget } from "../widgets/basic_widget.js";
import NoteContextAwareWidget from "../widgets/note_context_aware_widget.js";
import RightPanelWidget from "../widgets/right_panel_widget.js";
+import { BackendScriptingDisabledError, showBackendScriptingDisabledToast } from "./backend_scripting.js";
import dateNotesService from "./date_notes.js";
import dialogService from "./dialog.js";
import froca from "./froca.js";
@@ -25,7 +26,7 @@ import server from "./server.js";
import shortcutService from "./shortcuts.js";
import SpacedUpdate from "./spaced_update.js";
import toastService from "./toast.js";
-import utils, { openInAppHelpFromUrl } from "./utils.js";
+import utils from "./utils.js";
import ws from "./ws.js";
/**
@@ -822,59 +823,6 @@ export default FrontendScriptApi as any as {
new(startNote: FNote, currentNote: FNote, originEntity: Entity | null, $container: JQuery<HTMLElement> | null): Api;
};
-/**
- * Thrown by {@link Api.runOnBackend} / {@link Api.runAsyncOnBackendWithManualTransactionHandling}
- * when backend scripting is disabled on the server. The bundle executor recognizes this so it can
- * skip its per-note error toast — the single deduplicated toast raised alongside it is enough.
- */
-export class BackendScriptingDisabledError extends Error {
- constructor() {
- super("Backend script execution is disabled.");
- this.name = "BackendScriptingDisabledError";
- }
-}
-
-// The notes that attempted to run backend code while it was disabled, accumulated so the single
-// deduplicated toast can list them all as reference links. Cleared when the toast is removed.
-const backendScriptingAttempts = new Set<string>();
-
-/**
- * Shows the single deduplicated "backend scripting is disabled" toast, adding the given note to the
- * list of scripts that tried to run backend code. Exported so any backend-execution entry point can
- * reuse it — a frontend `runOnBackend()` call, or executing a backend code note directly.
- */
-export function showBackendScriptingDisabledToast(noteId: string) {
- backendScriptingAttempts.add(noteId);
- toastService.showPersistent({
- id: "backend-scripting-disabled",
- icon: "bx bx-code-block",
- title: t("frontend_script_api.backend_scripting_disabled_title"),
- message: t("frontend_script_api.backend_scripting_disabled_message"),
- notesHeading: t("frontend_script_api.backend_scripting_disabled_notes_heading"),
- noteIds: [ ...backendScriptingAttempts ],
- wide: true,
- timeout: 60_000,
- onRemove: () => backendScriptingAttempts.clear(),
- buttons: [
- {
- text: t("frontend_script_api.backend_scripting_disabled_open_settings"),
- onClick: ({ dismissToast }) => {
- appContext.triggerCommand("showOptions", { section: "_optionsSecurity" });
- dismissToast();
- }
- },
- {
- text: t("frontend_script_api.backend_scripting_disabled_more_info"),
- onClick: ({ dismissToast }) => {
- // openInAppHelpFromUrl takes the help note ID without the "_help_" prefix.
- openInAppHelpFromUrl("fiHicjpHjIRJ");
- dismissToast();
- }
- }
- ]
- });
-}
-
// --- Drift guards -----------------------------------------------------------
// The public script API surface lives in @triliumnext/commons (self-contained so
// it can feed the in-editor language service and the script-deployer). These