-
Notifications
You must be signed in to change notification settings - Fork 15k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
second-instance
event process arguments rip apart "name-value" arguments
#20322
Comments
This does seem surprising, but I think it's intended behavior from Chromium's command-line parsing logic. Chromium treats "switches" (things like I'm not sure it's worth messing with the way Chromium handles command-line parsing in order to change this behavior. Would it be possible for you to use the |
Thanks for the reply and clarification (and especially the chromium link, I couldn't find where that happened)
Definitely not worth it! It'll be just a brittle hack and will break all the time, causing issues regularly.
Not without seriously upsetting our customers 😥 Is there some other way to send kind of an argument to the second instance instead? Technically, I just need one or two flags passed from the second instance to the "main" one... To me this looks like a bug in chromium, I'll take a closer look tomorrow |
@siebertm Just shimming in, but would it be worth it to wrap the CLI to your electron-app with something that would do the conversion of arguments? Anything like |
If you wrote a wrapper script, you could also just pass I'm closing this for now since there's no action for us to take here, but feel free to continue discussion in this issue. |
A workaround (hack) is to exploit const { app } = require("electron");
let argv = process.argv;
app.commandLine.appendSwitch("second-instance" , JSON.stringify(process.argv));
if (!app.requestSingleInstanceLock()) {
return;
} else {
app.on("second-instance", function(event, argvChromium, cwd) {
// argv = JSON.parse(event.sender.commandLine.getSwitchValue("second-instance")); // does not work ...
// parse from argvChromium
const argStart = "--second-instance=";
let argv = argvChromium.filter(a => a.startsWith(argStart))[0];
if (argv) {
argv = argv.substr(argStart.length);
argv = JSON.parse(argv);
}
// and here we go with argv ...
} |
Another workaround, using additionalData: //Get your args and pass them when you call app.requestSingleInstanceLock
var argv = require("minimist")(process.argv.slice(2));
var isFirstInstance = app.requestSingleInstanceLock({
argv: argv,
}); Then consume argv in your first instance if (!isFirstInstance) {
app.quit(); //Application already running ?
} else {
//First instance detects second instance
app.on("second-instance", (event, commandLine, workingDirectory, additionalData) => {
console.log("additionalData.argv: ", additionalData.argv);
// ...
}
// ...
} Also, thanks xmedeko for your snippet! |
@alexNecroJack This issue (and my workaround) was about problems in Electron 15 and lower when |
Add note about argv getting modified See #20322
See #20322 Co-authored-by: Mikael Finstad <finstaden@gmail.com>
Add note about argv getting modified See #20322 Co-authored-by: Mikael Finstad <finstaden@gmail.com> Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Mikael Finstad <finstaden@gmail.com>
Add note about argv getting modified See electron#20322
Add note about argv getting modified See electron#20322
Preflight Checklist
Issue Details
When I run an electron app with
requestSingleInstanceLock()
and some command line arguments, in electron 6 (and maybe even 5), it seems that the--allow-file-access-from-files
and--original-process-start-time=13213718723637733
arguments are inserted anywhere in the argv array. The original arguments are preserved but when we have applications which expect key-value type arguments, these arguments will be split, resulting in invalid values.A simple test case is actually contained in the electron repository: https://github.com/electron/electron/blob/6-0-x/spec/fixtures/api/singleton/main.js
When we now start the second instance with 2 arguments:
["--param", "value"]
, thesecond-instance
event will fire with the following arguments:The expected behaviour would be to keep the arguments together, like:
In the spec spec-main/api-app-spec.ts:222, the test successfully validates exactly that behaviour, but that may be wrong. the arguments could be appended or prepended, but should not be inserted somewhere in the middle...
Electron Version:
v6.0.9
Operating System:
Windows 10 (insiders, Version 10.0.18975.1000)
Last Known Working Electron version:
4.x
The text was updated successfully, but these errors were encountered: