rannn505 / child-shell

Node.js bindings 🔗 for shell
http://rannn505.github.io/child-shell/
MIT License
301 stars 71 forks source link

ProcessError: Shell process (somePID) exited. originalError: undefined, exitCode: null, signalCode: 'SIGABRT' #157

Open msdiniz opened 1 year ago

msdiniz commented 1 year ago

I tried to implement some basic stuff, like get MachineName and Private and Public IP address through Node using node-powershell, and it is running without issues in Windows with Powershell 7.3.1.

Part of the working code is:

const Shell = require('node-powershell');
const os = require('os');

async function getMachineName () {
  let name = null;
  let error = null;
  let cmd;
  if (os.platform() === 'linux') {
    cmd = 'hostname';
  } else {
    cmd = '$Env:Computername';
  }
  await Shell.PowerShell.$`${cmd}`
    .then(response => {
      console.log(response.raw);
      name = response.raw.replaceAll('[?1h[?1l', '').trim();
    })
    .catch(err => {
      console.log(err);
      error = err;
    });
  return { name, error };
}
module.exports = { getMachineName };

I tried the same in Ubuntu 22.04 - same powershell version - and the very same function(s) are all always ending in:

ProcessError: Shell process 118990 exited.
undefined
    at /home/marcelo/repos/child-shell-test/packages/child-shell/lib/Shell.ts:232:15
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  originalError: undefined,
  exitCode: null,
  signalCode: 'SIGABRT'
}

only changing the PID, of course.

So, I forked and installed child-shell playground, installed it in both Windows and Ubuntu machines and to my surprise, both ended in error at same spots:

> npm start
> cd playground && node -r ts-node/register index.ts

========== playground ==========
  BASH:118974: # shell process started: "bash --noprofile -s" +0ms
  BASH:118977: # shell process started: "bash -s" +0ms
  BASH:118974: # starting command invocation +18ms
  BASH:118974: $ pwd +0ms
  BASH:118974: > tXgOmeQ0oQ7bB0gb
  BASH:118974: > /home/marcelo/repos/child-shell-test/playground
  BASH:118974: > tXgOmeQ0oQ7bB0gb +0ms
  BASH:118974: > tXgOmeQ0oQ7bB0gb
  BASH:118974: > tXgOmeQ0oQ7bB0gb +2ms
  BASH:118974: # command invocation succeeded +7ms
  BASH:118977: # starting command invocation +9ms
  BASH:118977: $ sleep 2s +0ms
  BASH:118977: > xhAiunabkuTl8pQP +0ms
  BASH:118977: > xhAiunabkuTl8pQP +1ms
  BASH:118977: # command invocation timed out +1s
  BASH:118974: # starting command invocation +1s
  BASH:118974: $ echo "hey from node-bash <3" +1s
  BASH:118974: > 2vkgs43isRRUMFik +1s
  BASH:118974: > 2vkgs43isRRUMFik +0ms
  BASH:118974: > 2vkgs43isRRUMFik
  BASH:118974: > hey from node-bash <3
  BASH:118974: > 2vkgs43isRRUMFik +1ms
  BASH:118974: # command invocation succeeded +2ms
  BASH:118974: # starting command invocation +0ms
  BASH:118974: $ echo "hey","from","node-bash","<3" +2ms
  BASH:118974: > xNJqQaHSUaPgaX42 +1ms
  BASH:118974: > xNJqQaHSUaPgaX42
  BASH:118974: > hey,from,node-bash,<3
  BASH:118974: > xNJqQaHSUaPgaX42 +0ms
  BASH:118974: > xNJqQaHSUaPgaX42 +0ms
  BASH:118974: # command invocation succeeded +2ms
  BASH:118974: # starting command invocation +0ms
  BASH:118974: $ cat ./script.sh +2ms
  BASH:118974: > 14yiHRNLL24AwCOD +1ms
  BASH:118974: > 14yiHRNLL24AwCOD +0ms
  BASH:118974: > #!/bin/bash
  BASH:118974: > 
  BASH:118974: > ARGUMENT_LIST=(
  BASH:118974: >   "message",
  BASH:118974: > )
  BASH:118974: > # default values
  BASH:118974: > OPTS=$(getopt \
  BASH:118974: >     --longoptions "$(printf "+1ms:," "${ARGUMENT_LIST[@]}")" \
  BASH:118974: >     --name "$(basename "$0")" \
  BASH:118974: >     --options "" \
  BASH:118974: >     -- "$@"
  BASH:118974: > )
  BASH:118974: > # eval set -- "$OPTS"
  BASH:118974: > message="child-shell"
  BASH:118974: > while [[ $# -gt 0 ]]; do
  BASH:118974: >   case "$1" in
  BASH:118974: >       --message ) message="$2" ;;
  BASH:118974: >   esac
  BASH:118974: >   shift 2
  BASH:118974: > done
  BASH:118974: > 
  BASH:118974: > echo $message
  BASH:118974: > 14yiHRNLL24AwCOD +1ms
  BASH:118974: > 14yiHRNLL24AwCOD +0ms
  BASH:118974: # command invocation succeeded +2ms
  BASH:118974: # starting command invocation +0ms
  BASH:118974: $ . ./script.sh +2ms
  BASH:118974: > 9dR3dVTK9OrKz7nq +1ms
  BASH:118974: > 9dR3dVTK9OrKz7nq +0ms
  BASH:118974: > getopt: opção longa vazia após argumento -l ou --long
  BASH:118974: > Tente "getopt --help" para mais informações. +4ms
  BASH:118974: > child-shell +0ms
  BASH:118974: > 9dR3dVTK9OrKz7nq +0ms
  BASH:118974: > 9dR3dVTK9OrKz7nq +0ms
  BASH:118974: # command invocation failed +6ms
InvocationError: getopt: opção longa vazia após argumento -l ou --long
Tente "getopt --help" para mais informações.
    at Bash.<anonymous> (/home/marcelo/repos/child-shell-test/packages/child-shell/lib/Shell.ts:359:13)
    at Generator.next (<anonymous>)
    at fulfilled (/home/marcelo/repos/child-shell-test/packages/child-shell/dist/Shell.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
  BASH:118974: # shell process exited +15ms
  PWSH:118990: # shell process started: "pwsh -NoLogo -ExecutionPolicy Bypass -NoProfile -NoExit -Command -" +0ms
  PWSH:118990: # starting command invocation +1ms
  PWSH:118990: $ Write-Host "hey from node-powershell <3" -ForegroundColor red -BackgroundColor white +0ms
  PWSH:118990: # shell process exited +544ms
  PWSH:118990: # command invocation stopped +0ms
ProcessError: Shell process 118990 exited.
undefined
    at /home/marcelo/repos/child-shell-test/packages/child-shell/lib/Shell.ts:232:15
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  originalError: undefined,
  exitCode: null,
  signalCode: 'SIGABRT'
}
ProcessError: Shell process 118990 exited.
undefined

There are few differences though: 1) Windows log is much less verbose 2) Windows error msg says that signalCode: null, instead of 'SIGABRT'

I have the last nvm in both machines and tested all these node versions: v14.21.2 v16.14.2 v16.15.0 -> v18.12.1 v19.3.0

Any clues how to address this?