electron-userland / electron-builder

A complete solution to package and build a ready for distribution Electron app with “auto update” support out of the box
https://www.electron.build
MIT License
13.58k stars 1.73k forks source link

/bin/sh command not found after building MacOS #7887

Closed codergautam closed 8 months ago

codergautam commented 10 months ago

Seems to be related to #6726 in some way.

I'm trying to run some pip commands from my electron app using exec

(but same/similar issue with spawn and execa module as well)

Steps to reproduce:

  1. create project using template https://github.com/electron-react-boilerplate/electron-react-boilerplate
  2. Add this code in the main process (make sure to import exec and dialog correctly). You also need python installed for this (which I have installed in /Users/codergautam/.pyenv/shims/pip)
    // Run pip list command and show output in dialog
    exec('pip list', (err, stdout, stderr) => {
    console.log(stdout);
    console.log(stderr);
    if (err) {
      dialog.showErrorBox('Pip List Error', inspect(err));
      return;
    }
    dialog.showMessageBoxSync({
      title: 'Pip List',
      message: stdout.substring(0, 200),
    });
    });
  3. Run it using npm start, see the trimmed list of packages correctly
  4. Build it using npm run package then run the built app, you will get error:
Error: Command failed: pip list
/bin/sh: pip: command not found

at ChildProcess.exithandler (node:child_process:430:12)
at ChildProcess.emit (node:events:513:28)
at maybeClose (node:internal/child_process:1091:16)
at Socket.<anonymous> (node:internal/child_process:449:11)
at Socket.emit (node:events:513:28)
at Pipe.<anonymous> (node:net:322:12) {
code: 127,
killed: false,
signal: null,
cmd: 'pip list'
}

Image:

image

I think for some reason it's trying to run the python 2.7 stuff which was removed. This is a major blocker, any way to fix?

mmaietta commented 10 months ago

Aren't node system commands supposed to be called from the main process? This seems unrelated to electron-builder, which is simply a packager (not a compiler or bundler).

codergautam commented 10 months ago

Sorry, typo on my end. I did mean main process (src/main.ts file)

betterbrand commented 9 months ago

I am having this exact same problem. npm start works just fine, but npm build produces a "command not found" error. I am on a mac trying to reach Ollama via Cl

codergautam commented 9 months ago

Yeah trying to find a workaround for this and will post here, let me know if you find anything @betterbrand

mmaietta commented 9 months ago

Try logging what process.env is and check that your executable is accessible via PATH var or any other env vars you may need. You may need to provide a direct path to the executable, similar to the /bin/bash logic that exists for RPM and Deb updaters, albeit it is using spawn. https://github.com/electron-userland/electron-builder/blob/6b84010b67de0ddc07404d53436bbfbfa2761f88/packages/electron-updater/src/RpmUpdater.ts#L30-L64

betterbrand commented 9 months ago

Try logging what process.env is and check that your executable is accessible via PATH var or any other env vars you may need. You may need to provide a direct path to the executable, similar to the /bin/bash logic that exists for RPM and Deb updaters, albeit it is using spawn.

https://github.com/electron-userland/electron-builder/blob/6b84010b67de0ddc07404d53436bbfbfa2761f88/packages/electron-updater/src/RpmUpdater.ts#L30-L64

Thank you! Trying this now

betterbrand commented 9 months ago

@mmaietta @codergautam before building I get two different responses to the path.

npm start returns:

/Users/myname/Documents/GitHub/Morpheus/node_modules/.bin:/Users/myname/Documents/GitHub/node_modules/.bin:/Users/myname/Documents/node_modules/.bin:/Users/myname/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/opt/homebrew/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/node-gyp-bin:/Users/myname/google-cloud-sdk/y/google-cloud-sdk/bin:/Users/myname/anaconda3/bin:/Users/myname/anaconda3/condabin:/Users/myname/Library/Application Support/cloud-code/installer/google-cloud-sdk/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/myname/Library/Application Support/cloud-code/installer/google-cloud-sdk/bin:/Applications/Conveyor.app/Contents/MacOS://Users/myname/Desktop/flutter/bin://Users/myname/StudioProjects/flutter/bin

yarn start returns

/var/folders/9h/6xxs5cm509n1k0l80kln6q8h0000gn/T/yarn--1700602882940-0.31378766444904027:/Users/myname/Documents/GitHub/Morpheus/node_modules/.bin:/Users/myname/.config/yarn/link/node_modules/.bin:/Users/myname/.yarn/bin:/opt/homebrew/Cellar/node/21.1.0/libexec/lib/node_modules/npm/bin/node-gyp-bin:/opt/homebrew/Cellar/node/21.1.0/lib/node_modules/npm/bin/node-gyp-bin:/opt/homebrew/Cellar/node/21.1.0/bin/node_modules/npm/bin/node-gyp-bin:/Users/myname/google-cloud-sdk/y/google-cloud-sdk/bin:/Users/myname/anaconda3/bin:/Users/myname/anaconda3/condabin:/Users/myname/Library/Application Support/cloud-code/installer/google-cloud-sdk/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/myname/Library/Application Support/cloud-code/installer/google-cloud-sdk/bin:/Applications/Conveyor.app/Contents/MacOS://Users/myname/Desktop/flutter/bin://Users/myname/StudioProjects/flutter/bin

Note: yarn start shows /var/folders/9h/6xxs5cm509n1k0l80kln6q8h0000gn/T/yarn--1700602882940-0.31378766444904027: before the list of User directories

mmaietta commented 9 months ago

I meant to check the difference in process.env between runtime npm start and the bundled electron app. Doesn't matter what the diff is between yarn and npm, only matters what the bundled electron app is able to access

betterbrand commented 9 months ago

@mmaietta @codergautam @paulrosania These are the two different log files. I'm new to this – I really appreciate your patience and guidance.

process_env_runtime.log

process_env_packaged.log

betterbrand commented 9 months ago

FWIW and Just to be complete, I have tried this with forge as well.

process_env_forge_packaged.log process_env_forge_runtime.log

Checking for differences now Thanks again

betterbrand commented 9 months ago

PATH in runtime (npm run start) PATH: '/Users/myname/Documents/GitHub/Morpheus/node_modules/.bin:/Users/myname/Documents/GitHub/node_modules/.bin:/Users/myname/Documents/node_modules/.bin:/Users/myname/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/opt/homebrew/lib/node_modules/npm/node_modules/@npmcli/run-script/lib/node-gyp-bin:/Users/myname/google-cloud-sdk/y/google-cloud-sdk/bin:/Users/myname/anaconda3/bin:/Users/myname/anaconda3/condabin:/Users/myname/Library/Application Support/cloud-code/installer/google-cloud-sdk/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/myname/Library/Application Support/cloud-code/installer/google-cloud-sdk/bin:/Applications/Conveyor.app/Contents/MacOS://Users/myname/Desktop/flutter/bin://Users/myname/StudioProjects/flutter/bin',

And PATH when packaged (npm run build) PATH: '/usr/bin:/bin:/usr/sbin:/sbin',

mmaietta commented 9 months ago

When running npm run start, it is executing in your current terminal environment, which has all the settings of .profile/.bash_profile/.zshrc/etc. loaded.

A packaged application doesn't have access to your user-profile

codergautam commented 9 months ago

Is it possible to load these from code?

mmaietta commented 9 months ago

Anything that is loaded from the user's machine may or may not have their user profiles set up in the same way, so I would suggest against trying to load those files from code.

There's a package that might be useful in this context: https://www.npmjs.com/package/python-shell

That all being said, I don't know of a reliable way to load pip from within the app without it already being in the PATH.

codergautam commented 8 months ago

No it is in Path, just doesnt work when opening the app normally. When right clicking the app, pressing view package contents, navigating to the Terminal iconed app and opening that works perfectly fine. (It shows a log of the app while its running) @mmaietta

Anyways will be investigating this, will post an update if I find anything.

elianbraja commented 8 months ago

@codergautam i am having the same problem. Have you found any solution to make the command recognizable in the packed app?

codergautam commented 8 months ago

@elianbraja No not atm, will be investigating more this week and will post if I find any fix

elianbraja commented 8 months ago

@codergautam I ended up generating a binary file and calling that file instead. You can create a Python executable file with “pyinstaller”. This way you can directly execute the script just by referencing it to exec.

codergautam commented 8 months ago

Interesting will try it out, my issue is I need to call pip to check some current packages. Maybe I can try rewriting that in Python and calling that executed program.