Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): stop printing "undefined" on second-instance launch, closes #10721

Elian Doran5829314ae7d2
diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.ts
index 5cab3134e0..ccc2d3606f 100644
--- a/apps/desktop/src/main.spec.ts
+++ b/apps/desktop/src/main.spec.ts
@@ -453,6 +453,38 @@ describe("main() bootstrap", () => {
await expect(main()).rejects.toThrow("__exit__");
expect(exitSpy).toHaveBeenCalledWith(0);
});
+
+ // The single-instance guard runs *before* initializeCore() wires up
+ // translations, so it must log a plain, translation-independent string. It
+ // once passed the message through i18next's t(), which returns undefined
+ // when uninitialized — so a second launch printed a bare "undefined".
+ //
+ // This models that uninitialized behaviour (t() -> undefined) to guard
+ // against anyone reintroducing a `t()` call on this path — the file-level
+ // `vi.mock("i18next")` echoes the key back and the test-suite's shared,
+ // already-initialized i18next singleton would both otherwise mask it.
+ it("logs a non-empty message (never undefined) even though translations aren't initialized yet", async () => {
+ vi.doMock("i18next", () => ({ t: () => undefined }));
+ h.isPrimaryInstance = false;
+ const infoSpy = vi.spyOn(console, "info").mockImplementation(() => {});
+ try {
+ const { main } = await importMain();
+ await expect(main()).rejects.toThrow("__exit__");
+
+ // Regression guard: the second instance exits before initializeCore()
+ // runs, so translations are unavailable — the message must not fall
+ // through to a bare `undefined`.
+ const { initializeTranslations } = await import("@triliumnext/server/src/services/i18n.js");
+ expect(initializeTranslations).not.toHaveBeenCalled();
+ expect(infoSpy).toHaveBeenCalledTimes(1);
+ const logged = infoSpy.mock.calls[0]?.[0];
+ expect(typeof logged).toBe("string");
+ expect(logged).not.toBe("");
+ } finally {
+ infoSpy.mockRestore();
+ vi.doUnmock("i18next");
+ }
+ });
});
describe("stream EPIPE error handler", () => {
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index d39d83bc47..e6f2fc99b7 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -157,7 +157,14 @@ export async function main() {
// emit `ready`, while still letting us open the database and read options from it first.
const isPrimaryInstance = app.requestSingleInstanceLock();
if (!isPrimaryInstance) {
- console.info(t("desktop.instance_already_running"));
+ // Deliberately not localized: this guard runs before the database is
+ // opened and initializeCore() wires up i18next, so t() would always
+ // return undefined here (which is why a second launch used to print a
+ // bare "undefined"). Opening the DB just to translate a diagnostic line
+ // that a second, immediately-exiting instance emits would defeat the
+ // point of bailing out early. The already-running window is focused via
+ // the `second-instance` handler registered above.
+ console.info("There's already an instance running, focusing that instance instead.");
process.exit(0);
}
diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.json
index cc66cc254a..4c168ddde6 100644
--- a/apps/server/src/assets/translations/en/server.json
+++ b/apps/server/src/assets/translations/en/server.json
@@ -458,9 +458,6 @@
"db_not_initialized_desktop": "DB not initialized, please follow on-screen instructions.",
"db_not_initialized_server": "DB not initialized, please visit setup page - http://[your-server-host]:{{port}} to see instructions on how to initialize Trilium."
},
- "desktop": {
- "instance_already_running": "There's already an instance running, focusing that instance instead."
- },
"script": {
"wrong-environment": "Cannot execute note \"{{- noteTitle}}\" ({{- noteId}}). This is a {{- actualEnv}} script, but execution was attempted in the {{- expectedEnv}}."
},