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

execa.sync doesn't (can't?) provide all #391

Closed
amitdahan opened this issue Nov 21, 2019 · 3 comments
Closed

execa.sync doesn't (can't?) provide all #391

amitdahan opened this issue Nov 21, 2019 · 3 comments

Comments

@amitdahan
Copy link

execa.sync apparently does not provide the all property on both the resolved result or the error object thrown.
The docs mention returning all yet I don't see how it's even possible.
Is this a mere docs (and types) issue?

Example:

const execa = require('execa');

const result = execa.sync(
  `node -e "console.log('stdout'); console.error('stderr');"`, 
  { shell: true, all: true }
);

console.log(result);
/* 
  { command:
    'node -e "console.log(\'stdout\'); console.error(\'stderr\');"',
    exitCode: 0,
    stdout: 'stdout',
    stderr: 'stderr',
    failed: false,
    timedOut: false,
    isCanceled: false,
    killed: false }
*/

console.log(result.all);
// undefined

Example with an error:

const execa = require('execa');

try {
  const result = execa.sync(
    `node -e "console.log('stdout'); console.error('stderr'); process.exit(1);"`, 
    { shell: true, all: true }
  );
} catch (err) {
  console.log(err);
  /*
  { Error: Command failed with exit code 1: node -e "console.log('stdout'); console.error('stderr'); process.exit(1);"
      at makeError (/private/tmp/execa-test/node_modules/execa/lib/error.js:56:11)
      at Function.module.exports.sync (/private/tmp/execa-test/node_modules/execa/index.js:188:17)
      at repl:2:24
      at Script.runInThisContext (vm.js:122:20)
      at REPLServer.defaultEval (repl.js:332:29)
      at bound (domain.js:402:14)
      at REPLServer.runBound [as eval] (domain.js:415:12)
      at REPLServer.onLine (repl.js:642:10)
      at REPLServer.emit (events.js:203:15)
      at REPLServer.EventEmitter.emit (domain.js:448:20)
    command:
     'node -e "console.log(\'stdout\'); console.error(\'stderr\'); process.exit(1);"',
    exitCode: 1,
    signal: undefined,
    signalDescription: undefined,
    stdout: 'stdout',
    stderr: 'stderr',
    failed: true,
    timedOut: false,
    isCanceled: false,
    killed: false }
  */

  console.log(err.all);
  // undefined
}
@ehmicky
Copy link
Collaborator

ehmicky commented Nov 21, 2019

Hi @amitdahan,

Indeed all is not returned by execa.sync() (this is documented in the README).

The reason is because all relies on streaming mechanics that are not available with execa.sync() which relies on child_process.spawnSync() under the hood. You can see more information from the original PR #171.

If you have some idea on how to bring all to execa.sync() though, a PR would be welcome :)

@ehmicky ehmicky closed this as completed Nov 21, 2019
@sindresorhus
Copy link
Owner

If you have some idea on how to bring all to execa.sync() though, a PR would be welcome :)

The only way to support it is to open an issue on https://github.com/nodejs/node/issues and convince them that childProcess.spawnSync should support such an option.

@ehmicky
Copy link
Collaborator

ehmicky commented Nov 22, 2019

Using async logic is also always a good idea.
There are some legitimate reasons why one might want to use sync logic, such as in process exit handler, or to trigger something during load / require() time.
But very often sync I/O is used for convenience (not having to use async/await) which is an anti-pattern IMHO.

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

3 participants