Skip to content

Commit 5bf0971

Browse files
committed
fix: electron main files always open when starting app in editor due to cli args parsing
1 parent 47d82fc commit 5bf0971

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src-electron/main-app-ipc.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,33 @@
99
const { app, ipcMain } = require('electron');
1010
const { spawn } = require('child_process');
1111
const readline = require('readline');
12+
const path = require('path');
1213
const { productName } = require('./package.json');
1314

1415
let processInstanceId = 0;
16+
17+
// Path to main.js - used to filter it out from CLI args in dev mode
18+
const mainScriptPath = path.resolve(__dirname, 'main.js');
19+
20+
/**
21+
* Filter CLI args to remove internal Electron arguments.
22+
* In dev mode, process.argv includes: [electron, main.js, ...userArgs]
23+
* In production, it includes: [app, ...userArgs]
24+
* This function filters out the main.js entry point in dev mode.
25+
*/
26+
function filterCliArgs(args) {
27+
if (!args || args.length === 0) {
28+
return args;
29+
}
30+
31+
const normalizedMainScript = mainScriptPath.toLowerCase();
32+
33+
return args.filter(arg => {
34+
// Resolve to handle both absolute and relative paths
35+
const resolvedArg = path.resolve(arg).toLowerCase();
36+
return resolvedArg !== normalizedMainScript;
37+
});
38+
}
1539
// Map of instanceId -> { process, terminated }
1640
const spawnedProcesses = new Map();
1741

@@ -115,8 +139,9 @@ function registerAppIpcHandlers() {
115139
});
116140

117141
// CLI args (mirrors Tauri's cli.getMatches for --quit-when-done / -q)
142+
// Filter out internal Electron args (main.js in dev mode)
118143
ipcMain.handle('get-cli-args', () => {
119-
return process.argv;
144+
return filterCliArgs(process.argv);
120145
});
121146

122147
// App path (repo root when running from source)
@@ -132,5 +157,6 @@ function registerAppIpcHandlers() {
132157

133158
module.exports = {
134159
registerAppIpcHandlers,
135-
terminateAllProcesses
160+
terminateAllProcesses,
161+
filterCliArgs
136162
};

src-electron/main.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { app, BrowserWindow, protocol, Menu, ipcMain } = require('electron');
22
const path = require('path');
33
const fs = require('fs');
44

5-
const { registerAppIpcHandlers, terminateAllProcesses } = require('./main-app-ipc');
5+
const { registerAppIpcHandlers, terminateAllProcesses, filterCliArgs } = require('./main-app-ipc');
66
const { registerFsIpcHandlers, getAppDataDir } = require('./main-fs-ipc');
77
const { registerCredIpcHandlers } = require('./main-cred-ipc');
88
const { registerWindowIpcHandlers, registerWindow } = require('./main-window-ipc');
@@ -105,10 +105,12 @@ app.on('quit-requested', (exitCode) => {
105105
app.on('second-instance', (event, commandLine, workingDirectory) => {
106106
// Forward to all windows via IPC
107107
// Window focusing is handled by the renderer's singleInstanceHandler
108+
// Filter out internal electron args (executable and main.js script)
109+
const filteredArgs = filterCliArgs(commandLine);
108110
BrowserWindow.getAllWindows().forEach(win => {
109111
if (!win.isDestroyed()) {
110112
win.webContents.send('single-instance', {
111-
args: commandLine,
113+
args: filteredArgs,
112114
cwd: workingDirectory
113115
});
114116
}

0 commit comments

Comments
 (0)