fix(desktop): handling of electron path not correct for portable installations

This commit is contained in:
Elian Doran
2026-04-18 12:01:58 +03:00
parent 8446e98b21
commit 2a7309477c
5 changed files with 13 additions and 20 deletions

View File

@@ -6,7 +6,7 @@ WHERE powershell.exe > NUL 2>&1
IF %ERRORLEVEL% NEQ 0 GOTO BATCH ELSE GOTO POWERSHELL
:POWERSHELL
powershell -ExecutionPolicy Bypass -NonInteractive -NoLogo -Command "Set-Item -Path Env:TRILIUM_DATA_DIR -Value './trilium-data'; Set-Item -Path Env:ELECTRON_NO_ATTACH_CONSOLE -Value '1'; ./trilium.exe"
powershell -ExecutionPolicy Bypass -NonInteractive -NoLogo -Command "Set-Item -Path Env:TRILIUM_DATA_DIR -Value './trilium-data'; Set-Item -Path Env:TRILIUM_ELECTRON_DATA_DIR -Value './trilium-electron-data'; Set-Item -Path Env:ELECTRON_NO_ATTACH_CONSOLE -Value '1'; ./trilium.exe"
GOTO END
:BATCH
@@ -17,6 +17,7 @@ chcp 65001
SET DIR=%~dp0
SET DIR=%DIR:~0,-1%
SET TRILIUM_DATA_DIR=%DIR%\trilium-data
SET TRILIUM_ELECTRON_DATA_DIR=%DIR%\trilium-electron-data
SET ELECTRON_NO_ATTACH_CONSOLE=1
cd "%DIR%"
start trilium.exe

View File

@@ -2,6 +2,7 @@
DIR=`dirname "$0"`
export TRILIUM_DATA_DIR="$DIR/trilium-data"
export TRILIUM_ELECTRON_DATA_DIR="$DIR/trilium-electron-data"
exec "$DIR/trilium"

View File

@@ -11,16 +11,16 @@
"url": "https://triliumnotes.org"
},
"scripts": {
"dev": "cross-env TRILIUM_PORT=37742 TRILIUM_DATA_DIR=data tsx ../../scripts/electron-start.mts src/main.ts",
"dev": "cross-env TRILIUM_PORT=37742 TRILIUM_DATA_DIR=data TRILIUM_ELECTRON_DATA_DIR=data-electron tsx ../../scripts/electron-start.mts src/main.ts",
"start-no-dir": "cross-env TRILIUM_PORT=37743 tsx ../../scripts/electron-start.mts src/main.ts",
"build": "tsx scripts/build.ts",
"start-prod": "pnpm build && cross-env TRILIUM_DATA_DIR=data TRILIUM_PORT=37841 ELECTRON_IS_DEV=0 electron dist",
"start-prod": "pnpm build && cross-env TRILIUM_DATA_DIR=data TRILIUM_ELECTRON_DATA_DIR=data-electron TRILIUM_PORT=37841 ELECTRON_IS_DEV=0 electron dist",
"start-prod-no-dir": "pnpm build && cross-env TRILIUM_PORT=37841 ELECTRON_IS_DEV=0 electron dist",
"electron-forge:make": "pnpm build && electron-forge make dist",
"electron-forge:make-flatpak": "pnpm build && DEBUG=* electron-forge make dist --targets=@electron-forge/maker-flatpak",
"electron-forge:package": "pnpm build && electron-forge package dist",
"electron-forge:start": "pnpm build && electron-forge start dist",
"e2e": "pnpm build && cross-env TRILIUM_INTEGRATION_TEST=memory-no-store TRILIUM_PORT=8082 TRILIUM_DATA_DIR=data-e2e ELECTRON_IS_DEV=0 playwright test"
"e2e": "pnpm build && cross-env TRILIUM_INTEGRATION_TEST=memory-no-store TRILIUM_PORT=8082 TRILIUM_DATA_DIR=data-e2e TRILIUM_ELECTRON_DATA_DIR=data-e2e-electron ELECTRON_IS_DEV=0 playwright test"
},
"dependencies": {
"@electron/remote": "2.1.3",

View File

@@ -10,7 +10,7 @@ import electronDebug from "electron-debug";
import electronDl from "electron-dl";
import { PRODUCT_NAME } from "./app-info";
import port from "@triliumnext/server/src/services/port.js";
import { join } from "path";
import { join, resolve } from "path";
import { deferred, LOCALES } from "../../../packages/commons/src";
async function main() {
@@ -102,22 +102,15 @@ async function main() {
/**
* Returns a unique user data directory for Electron so that single instance locks between legitimately different instances such as different port or data directory can still act independently, but we are focusing the main window otherwise.
*
* When running in portable mode (TRILIUM_DATA_DIR is set), the user data is stored inside the data directory
* so that no files are written to the system's roaming profile (e.g. %APPDATA% on Windows).
* This can be overridden by setting TRILIUM_ELECTRON_DATA_DIR to a custom path.
* When running in portable mode, set TRILIUM_ELECTRON_DATA_DIR (e.g. via the trilium-portable script)
* so that no Electron files are written to the system's roaming profile (e.g. %APPDATA% on Windows).
*/
function getUserData() {
if (process.env.TRILIUM_ELECTRON_DATA_DIR) {
return process.env.TRILIUM_ELECTRON_DATA_DIR;
return resolve(process.env.TRILIUM_ELECTRON_DATA_DIR, `electron-data-${port}`);
}
const name = `${app.getName()}-${port}`;
if (process.env.TRILIUM_DATA_DIR) {
return join(process.env.TRILIUM_DATA_DIR, "electron-user-data", name);
}
return join(app.getPath("appData"), name);
return join(app.getPath("appData"), `${app.getName()}-${port}`);
}
async function onReady() {

View File

@@ -81,9 +81,7 @@ You can then save the above command as a shell script on your path for convenien
When running the desktop application, Electron stores internal data (caches, spell-check dictionaries, session storage, etc.) separately from the Trilium data directory. By default this goes to the system's application data folder (e.g. `%APPDATA%` on Windows), which may be undesirable in corporate environments with roaming profiles or when running in portable mode.
When `TRILIUM_DATA_DIR` is set (e.g. via the `trilium-portable` script), the Electron user data is automatically redirected into `${TRILIUM_DATA_DIR}/electron-user-data/`, so no files are written to the system's roaming profile.
If you need to store the Electron data in a different location than the Trilium data directory, you can set the `TRILIUM_ELECTRON_DATA_DIR` environment variable to an explicit path.
To keep Electron data out of the system's roaming profile, set the `TRILIUM_ELECTRON_DATA_DIR` environment variable to an explicit path. The `trilium-portable` script does this automatically, pointing it to `trilium-electron-data/` next to the application. A subdirectory named `electron-data-[port]` is created inside it to allow multiple instances to coexist.
## Fine-grained directory/path location
@@ -97,4 +95,4 @@ Apart from the data directory, some of the subdirectories of it can be moved els
| `TRILIUM_TMP_DIR` | `${TRILIUM_DATA_DIR}/tmp` | Directory where temporary files are stored (for example when opening in an external app). |
| `TRILIUM_ANONYMIZED_DB_DIR` | `${TRILIUM_DATA_DIR}/anonymized-db` | Directory where a <a class="reference-link" href="../Troubleshooting/Anonymized%20Database.md">Anonymized Database</a> is stored. |
| `TRILIUM_CONFIG_INI_PATH` | `${TRILIUM_DATA_DIR}/config.ini` | Path to <a class="reference-link" href="../Advanced%20Usage/Configuration%20(config.ini%20or%20e.md">Configuration (config.ini or environment variables)</a> file. |
| `TRILIUM_ELECTRON_DATA_DIR` | `${TRILIUM_DATA_DIR}/electron-user-data` (portable) or system appData (default) | Directory where Electron stores internal data such as caches and spell-check dictionaries (desktop only). |
| `TRILIUM_ELECTRON_DATA_DIR` | System appData | Base directory for Electron internal data (caches, spell-check dictionaries, etc.). A `electron-data-[port]` subdirectory is created inside it. Set this in portable mode to avoid writing to the system profile (desktop only). |