Skip to content

Build your personal knowledge base with Trilium Notes

fix(hidden-subtree): stop re-saving value-less enforced attributes every run

Elian Doranf91065482f10
diff --git a/packages/trilium-core/src/services/hidden_subtree.spec.ts b/packages/trilium-core/src/services/hidden_subtree.spec.ts
index 92d082cce0..faca394a6f 100644
--- a/packages/trilium-core/src/services/hidden_subtree.spec.ts
+++ b/packages/trilium-core/src/services/hidden_subtree.spec.ts
@@ -1,4 +1,4 @@
-import { beforeAll, describe, expect, it } from "vitest";
+import { beforeAll, describe, expect, it, vi } from "vitest";
import becca from "../becca/becca.js";
import type BNote from "../becca/entities/bnote.js";
@@ -131,6 +131,24 @@ describe("hidden_subtree (real DB)", () => {
expect(hidden.getOwnedLabelValue("docName")).toBe("hidden");
});
+ it("does not re-save a value-less enforced attribute on every run", () => {
+ // `_template_text_snippet` declares `#textSnippet`/`#template` with no value, so they
+ // are stored as "". The enforcement compare used the raw (undefined) definition value,
+ // so `"" !== undefined` re-saved them on every run — and save() always emits a sync
+ // entity change, which churned `entitiesReloaded` and tore down every open text editor.
+ // The compare now normalizes undefined → "" to match what is actually written.
+ const snippet = becca.notes["_template_text_snippet"];
+ expect(snippet).toBeDefined();
+ const textSnippetAttr = snippet.getOwnedAttributes("label", "textSnippet")[0];
+ expect(textSnippetAttr).toBeDefined();
+ expect(textSnippetAttr.value).toBe("");
+
+ const saveSpy = vi.spyOn(textSnippetAttr, "save");
+ checkHiddenSubtree();
+ expect(saveSpy).not.toHaveBeenCalled();
+ saveSpy.mockRestore();
+ });
+
it("repairs a modified value on an enforced attribute", () => {
const hidden = becca.notes["_hidden"];
const docNameAttr = hidden.getOwnedAttributes("label", "docName")[0];
diff --git a/packages/trilium-core/src/services/hidden_subtree.ts b/packages/trilium-core/src/services/hidden_subtree.ts
index 00137dfda0..90fc215d9e 100644
--- a/packages/trilium-core/src/services/hidden_subtree.ts
+++ b/packages/trilium-core/src/services/hidden_subtree.ts
@@ -547,8 +547,12 @@ function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtree
continue;
}
- // Ensure value is consistent.
- if (attribute.value !== attrDef.value || attribute.type !== attrDef.type) {
+ // Ensure value is consistent. Normalize the expected value the same way it is written
+ // below (`attrDef.value ?? ""`): many definitions omit `value` (undefined) while the
+ // stored attribute holds "". Comparing the raw `attrDef.value` made `"" !== undefined`
+ // always true, so every value-less attribute was re-saved on each run — and save()
+ // unconditionally emits a sync entity change, churning all open editors.
+ if (attribute.value !== (attrDef.value ?? "") || attribute.type !== attrDef.type) {
attribute.type = attrDef.type;
attribute.value = attrDef.value ?? "";
attribute.save();