shelljs icon indicating copy to clipboard operation
shelljs copied to clipboard

Shelljs exec does not capture output from snap programs

Open FRodrigues-BrandenPT opened this issue 5 years ago • 7 comments

Node version (or tell us if you're using electron or some other framework):

v12.16.2

ShellJS version (the most recent version/Github branch you see the bug on):

0.8.4

Operating system:

Ubuntu 20.04 LTS

Description of the bug:

Shelljs exec does not capture output from snap programs

Example ShellJS command to reproduce the error:

Installed kotlin using snap.

$ snap list kotlin
Name    Version  Rev  Tracking       Publisher   Notes
kotlin  1.3.72   43   latest/stable  jetbrains✓  classic

$ kotlin -version
Kotlin version 1.3.72-release-468 (JRE 11.0.7+10-post-Ubuntu-3ubuntu1)

In node console:

> sh.exec('kotlin -version')
[String: ''] {
  stdout: '',
  stderr: '',
  code: 0,
  cat: [Function: bound ],
  exec: [Function: bound ],
  grep: [Function: bound ],
  head: [Function: bound ],
  sed: [Function: bound ],
  sort: [Function: bound ],
  tail: [Function: bound ],
  to: [Function: bound ],
  toEnd: [Function: bound ],
  uniq: [Function: bound ]
}
> sh.exec('kotlin -version | cat').code
Kotlin version 1.3.72-release-468 (JRE 11.0.7+10-post-Ubuntu-3ubuntu1)
0

FRodrigues-BrandenPT avatar Apr 26 '20 15:04 FRodrigues-BrandenPT

Can you confirm this really is snap-specific, and not a quirk of the kotlin command itself? It may be possible it's changing its behavior based on whether it's attached to a TTY. It should be enough to install a different snap to verify its behavior, or install kotlin some other way (not via snap).

nfischer avatar Apr 27 '20 17:04 nfischer

Yeah for example helm:

$ snap list helm    
Name  Version  Rev  Tracking       Publisher     Notes
helm  3.2.0    220  latest/stable  snapcrafters  classic
> sh.exec('helm version')
[String: ''] {
  stdout: '',
  stderr: '',
  code: 0,
  cat: [Function: bound ],
  exec: [Function: bound ],
  grep: [Function: bound ],
  head: [Function: bound ],
  sed: [Function: bound ],
  sort: [Function: bound ],
  tail: [Function: bound ],
  to: [Function: bound ],
  toEnd: [Function: bound ],
  uniq: [Function: bound ]
}
> sh.exec('helm version | cat')
version.BuildInfo{Version:"v3.2.0", GitCommit:"e11b7ce3b12db2941e90399e874513fbd24bcb71", GitTreeState:"clean", GoVersion:"go1.13.10"}
[String: 'version.BuildInfo{Version:"v3.2.0", GitCommit:"e11b7ce3b12db2941e90399e874513fbd24bcb71", GitTreeState:"clean", GoVersion:"go1.13.10"}\n'] {
  stdout: 'version.BuildInfo{Version:"v3.2.0", GitCommit:"e11b7ce3b12db2941e90399e874513fbd24bcb71", GitTreeState:"clean", GoVersion:"go1.13.10"}\n',
  stderr: '',
  code: 0,
  cat: [Function: bound ],
  exec: [Function: bound ],
  grep: [Function: bound ],
  head: [Function: bound ],
  sed: [Function: bound ],
  sort: [Function: bound ],
  tail: [Function: bound ],
  to: [Function: bound ],
  toEnd: [Function: bound ],
  uniq: [Function: bound ]
}

FRodrigues-BrandenPT avatar Apr 28 '20 11:04 FRodrigues-BrandenPT

What happens if you use child_process.execSync() instead of shell.exec()? Same results?

nfischer avatar Apr 29 '20 03:04 nfischer

Yeah, happens the same.

> execSync('kotlin -version')
<Buffer >
> execSync('kotlin -version | cat')
<Buffer 4b 6f 74 6c 69 6e 20 76 65 72 73 69 6f 6e 20 31 2e 33 2e 37 32 2d 72 65 6c 65 61 73 65 2d 34 36 38 20 28 4a 52 45 20 31 31 2e 30 2e 37 2b 31 30 2d 70 ... 21 more bytes>
> execSync('kotlin -version | cat').toString()
'Kotlin version 1.3.72-release-468 (JRE 11.0.7+10-post-Ubuntu-3ubuntu1)\n'

FRodrigues-BrandenPT avatar May 05 '20 22:05 FRodrigues-BrandenPT

I can also confirm that ShellJS exec running Node 14 installed with snap on Ubuntu 20.04 does not capture stdout.

File x.js prints a message to stdout.

console.log('hello node')

File shelljs-node.js runs "node x.js" asynchronously and captures stdout.

const sh = require('shelljs')

let cmd = 'node x.js'
let execOpts = { async: true }
let outputFn = x => console.log(`output: ${x}`)
let child = sh.exec(cmd, execOpts)
child.stdout.on('data', outputFn)

Running shelljs-node.js results in no output.

$ node x.js hello node $ node shelljs-node.js $

Replacing the snap Node with Node installed from the nodesource PPA fixes the problem.

$ node x.js hello node $ node shelljs-node.js hello node output: hello node

ToddFincannonEI avatar Nov 20 '20 00:11 ToddFincannonEI

This is quite odd and rather debilitating in some cases. Anyone have insights?

egnor avatar Apr 30 '21 05:04 egnor

This helped me https://github.com/shelljs/shelljs/issues/990#issuecomment-613728095

mblenton avatar Mar 10 '23 10:03 mblenton