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

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

Closed
codergautam opened this issue Nov 19, 2023 · 19 comments
Closed

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

codergautam opened this issue Nov 19, 2023 · 19 comments

Comments

@codergautam
Copy link

codergautam commented Nov 19, 2023

  • Electron-Builder Version: 24.6.4
  • Node Version: 20.5.1
  • Electron Version: 26.2.1
  • Electron Type (current, beta, nightly): current
  • Target: MacOS

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),
    });
  });
  1. Run it using npm start, see the trimmed list of packages correctly
  2. 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
Copy link
Collaborator

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
Copy link
Author

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

@betterbrand
Copy link

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
Copy link
Author

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

@mmaietta
Copy link
Collaborator

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.

protected doInstall(options: InstallOptions): boolean {
const upgradePath = options.installerPath
const sudo = this.wrapSudo()
// pkexec doesn't want the command to be wrapped in " quotes
const wrapper = /pkexec/i.test(sudo) ? "" : `"`
const packageManager = this.spawnSyncLog("which zypper")
let cmd: string[]
if (!packageManager) {
const packageManager = this.spawnSyncLog("which dnf || which yum")
cmd = [packageManager, "-y", "remove", `'${this.app.name}'`, ";", packageManager, "-y", "install", upgradePath]
} else {
cmd = [
packageManager,
"remove",
"-y",
`'${this.app.name}'`,
";",
packageManager,
"clean",
"--all",
";",
packageManager,
"--no-refresh",
"install",
"--allow-unsigned-rpm",
"-y",
"-f",
upgradePath,
]
}
this.spawnSyncLog(sudo, [`${wrapper}/bin/bash`, "-c", `'${cmd.join(" ")}'${wrapper}`])
if (options.isForceRunAfter) {
this.app.relaunch()
}
return true

@betterbrand
Copy link

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.

protected doInstall(options: InstallOptions): boolean {
const upgradePath = options.installerPath
const sudo = this.wrapSudo()
// pkexec doesn't want the command to be wrapped in " quotes
const wrapper = /pkexec/i.test(sudo) ? "" : `"`
const packageManager = this.spawnSyncLog("which zypper")
let cmd: string[]
if (!packageManager) {
const packageManager = this.spawnSyncLog("which dnf || which yum")
cmd = [packageManager, "-y", "remove", `'${this.app.name}'`, ";", packageManager, "-y", "install", upgradePath]
} else {
cmd = [
packageManager,
"remove",
"-y",
`'${this.app.name}'`,
";",
packageManager,
"clean",
"--all",
";",
packageManager,
"--no-refresh",
"install",
"--allow-unsigned-rpm",
"-y",
"-f",
upgradePath,
]
}
this.spawnSyncLog(sudo, [`${wrapper}/bin/bash`, "-c", `'${cmd.join(" ")}'${wrapper}`])
if (options.isForceRunAfter) {
this.app.relaunch()
}
return true

Thank you! Trying this now

@betterbrand
Copy link

@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
Copy link
Collaborator

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
Copy link

@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
Copy link

betterbrand commented Nov 22, 2023

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
Copy link

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
Copy link
Collaborator

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
Copy link
Author

Is it possible to load these from code?

@mmaietta
Copy link
Collaborator

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.

@mmaietta mmaietta closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2024
@codergautam
Copy link
Author

codergautam commented Jan 9, 2024

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
Copy link

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

@codergautam
Copy link
Author

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

@elianbraja
Copy link

elianbraja commented Jan 15, 2024

@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
Copy link
Author

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.

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

No branches or pull requests

4 participants