Skip to content
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

Wrong $0 in bundled electron app #685

Closed
acromarco opened this issue Oct 20, 2016 · 15 comments · Fixed by #1554
Closed

Wrong $0 in bundled electron app #685

acromarco opened this issue Oct 20, 2016 · 15 comments · Fixed by #1554

Comments

@acromarco
Copy link

acromarco commented Oct 20, 2016

We have an election app that is bundled to an .exe file.

A command line like
bundled.exe -f filename
will result in:
{ _: ['filename'], '$0': 'bundled.exe -f' }

The following workaround:
const argv = yargs.parse(process.argv.slice(1));
will result in
{ _: [], f: 'filename', '$0': 'bundled.exe -f' }

Please notice that the $0 ist still wrong because it contains the "-f".

If this an expected behavior? Is there a way to work around it?

@zinwalin
Copy link

same issue here.

@bcoe
Copy link
Member

bcoe commented Nov 10, 2019

@zinwalin @acromarco does this continue to be an issue? I wonder if we could detect we're in an electron environment and potentially handle our slicing a bit differently.

@bcoe bcoe added the triaged label Nov 10, 2019
@shadowspawn
Copy link
Member

I was looking into this in weekend (in context of Commander).

References:

Still a WIP, detection:

    if (process.versions && process.versions.electron) {
      parseOptions.from = 'electron';
    }

and decision about where to slice:

    case 'electron':
      if (process.defaultApp) {
        userArgs = argv.slice(2);
      } else {
        userArgs = argv.slice(1);
      }
      break;

@mleguen
Copy link
Member

mleguen commented Jan 29, 2020

@shadowspawn This would indeed solve the first half of this issue (slice(1) needed instead of slice(2)).

However, @acromarco also reported that it first flag (-f) was merged with the exe name into req.argv[0].

Is is something you can reproduce too?

@shadowspawn
Copy link
Member

-f merged: no, I didn't investigate that aspect.

@mleguen
Copy link
Member

mleguen commented Feb 5, 2020

@shadowspawn The master branch should now be OK for your use case (ie slice(2)replaced by slice(1) if process.defaultApp is false). Would you please mind testing?

@zinwalin @acromarco Could one of you please give us some feedback about the 2nd point ("notice that the $0 ist still wrong because it contains the "-f") reported in the initial issue?

@shadowspawn
Copy link
Member

(Disclaimer: I do not currently use yargs or electron for real, just an interested third party!)

  1. the slice issue does not appear to be fixed on master

From experimentation process.defaultApp is undefined or true, but not false. So the test for strict equality against false is not working as intended currently:

if (process.defaultApp === false) return 0

  1. I am not seeing the -f problem. Testing on macOS.
console.log(process.defaultApp);
console.log(args);
const argv = require('yargs').argv;
console.log(argv);
# Calling electron directly
% ./node_modules/.bin/electron . -f filename                                                     master
true
[
  '/Users/john/Documents/Sandpits/commander/issues/electron-quick-start/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron',
  '.',
  '-f',
  'filename'
]
{
  _: [],
  f: 'filename',
  '$0': 'node_modules/electron/dist/Electron.app/Contents/MacOS/Electron'
}

# Running bundled app
% ./foo-darwin-x64/foo.app/Contents/MacOS/foo -f filename                                        master
undefined
[
  '/Users/john/Documents/Sandpits/commander/issues/electron-quick-start/foo-darwin-x64/foo.app/Contents/MacOS/foo',
  '-f',
  'filename'
]
{
  _: [ 'filename' ],
  '$0': 'foo-darwin-x64/foo.app/Contents/MacOS/foo'
}

@bcoe
Copy link
Member

bcoe commented Feb 5, 2020

@shadowspawn @mleguen reading the issue linked, it sounds like they were suggesting only slicing at 0 when we know the electron app is bundled, via, process.defaultApp === false? might be worth asking for clarification from the electron folks?

@shadowspawn
Copy link
Member

I tracked down the supporting docs:

process.defaultApp Readonly

A Boolean. When app is started by being passed as parameter to the default app, this property is true in the main process, otherwise it is undefined.

@bcoe
Copy link
Member

bcoe commented Feb 5, 2020

@zcbenz @MarshallOfSound, recommendation on how we should best detect a argv.slice(0) vs., argv.slice(1), such that we get the bin correct?

should we be checking for process.defaultApp === true?

@zcbenz
Copy link

zcbenz commented Feb 10, 2020

@bcoe Checking process.defaultApp is currently the only way to handle this.

@mleguen
Copy link
Member

mleguen commented Feb 12, 2020

@shadowspawn Thanks for the tests, and pointing to the doc.

So I am going to replace my (process.defaultApp === false) by (!!process.version.electron && !process.defaultApp), which will lead to:

  • slice(1) only if process.version.electron is defined (this is an electron app) and process.defaultApp is undefined (the app was not launched by passing a parameter to electron)
  • slice(2) if process.version.electron is undefined (this is not an electron app) or process.defaultApp is true (the app was launched by passing a parameter to electron)

EDIT: checking !process.defaultApp alone is of course not enough, so we have to combine it with process.versions.electron. See the doc for process.versions.electron

2nd EDIT: nothing wrong in electron's doc as I initialy stated: process.defaultApp is indeed a boolean. Maybe time to take a break?

@mleguen
Copy link
Member

mleguen commented Feb 20, 2020

@shadowspawn 2nd fixed merged into master: ready for testing, if you don't mind.

@shadowspawn
Copy link
Member

Working fine now in my tests, args getting sliced at correct point.

@bcoe
Copy link
Member

bcoe commented Mar 8, 2020

@shadowspawn thanks for helping to sort this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants