Skip to content
This repository has been archived by the owner on Sep 23, 2021. It is now read-only.

Behavior is not consistent across macOS/linux #89

Closed
samuela opened this issue Feb 26, 2019 · 0 comments
Closed

Behavior is not consistent across macOS/linux #89

samuela opened this issue Feb 26, 2019 · 0 comments

Comments

@samuela
Copy link

samuela commented Feb 26, 2019

I have a bit of code:

class SudoSpawnError extends Error {
  public err: any;
  public stdout?: string;
  public stderr?: string;

  constructor(err: any, stdout?: string, stderr?: string) {
    super(
      `Error occurred in spawn.\n` +
        `  err: ${err}\n` +
        `  stdout: ${stdout}\n` +
        `  stderr: ${stderr}\n`
    );

    this.err = err;
    this.stdout = stdout;
    this.stderr = stderr;
  }
}

async function sudoExec(commands: string[]) {
  return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
    sudo.exec(
      `sh ./my-script.sh`,
      some_options,

      // There's no @types/sudo-prompt typings yet, so these are best-effort.
      (err: any, stdout?: string, stderr?: string) => {
        if (
          err === undefined &&
          stdout !== undefined &&
          stderr !== undefined &&
          stdout.includes("success!")
        ) {
          resolve({ stdout, stderr });
        } else {
          reject(new SudoSpawnError(err, stdout, stderr));
        }
      }
    );
  });
}

that tries to run a script that should eventually print "success!".

The issue is that the error handling is quite different between linux and macOS. If the script returns with a non-zero exit code, the linux version includes a useful (not undefined) stderr:

{ Error: Error occurred in spawn.
  err: Error: User did not grant permission.
  stdout: 
  stderr: umount: /home/darthvader/nu/skainswo: target is busy.


    at sudo.exec (/home/darthvader/nu/skainswo/nuvemfs/packages/cli/src/util.ts:108:18)
    at /opt/nu-links/cli-node_modules/node_modules/sudo-prompt/index.js:114:11
    at ChildProcess.exithandler (child_process.js:282:5)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
  err: Error: User did not grant permission.
    at /opt/nu-links/cli-node_modules/node_modules/sudo-prompt/index.js:111:23
    at ChildProcess.exithandler (child_process.js:282:5)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5),
  stdout: '',
  stderr: 'umount: /home/darthvader/nu/skainswo: target is busy.\n' }

but on macOS:

{ Error: Error occurred in spawn.
  err: Error: Command failed: bash /var/folders/dk/6wr_v2z91hdfz1y2q405wf740000gn/T/tmp-53671eyCwS3TKWlXD.tmp
umount(/Users/skainswo/nu/skainswo): Resource busy -- try 'diskutil unmount'

  stdout: undefined
  stderr: undefined

    at sudo.exec (/Users/skainswo/nu/skainswo/nuvemfs/packages/cli/src/util.ts:110:18)
    at /opt/nu-links/cli-node_modules/node_modules/sudo-prompt/index.js:163:31
    at ChildProcess.exithandler (child_process.js:288:7)
    at ChildProcess.emit (events.js:197:13)
    at maybeClose (internal/child_process.js:978:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:265:5)
  err:
   { Error: Command failed: bash /var/folders/dk/6wr_v2z91hdfz1y2q405wf740000gn/T/tmp-53671eyCwS3TKWlXD.tmp
   umount(/Users/skainswo/nu/skainswo): Resource busy -- try 'diskutil unmount'
   
       at /opt/nu-links/cli-node_modules/node_modules/sudo-prompt/index.js:306:27
       at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:54:3) code: 1 },
  stdout: undefined,
  stderr: undefined }

Why is stderr given as undefined? It seems like the API should be consistent across platforms.

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

No branches or pull requests

1 participant