Skip to content

exitOverride doesn't override call to process.exit #1444

@nicholasbailey

Description

@nicholasbailey

I'm running into an issue where exitOverride doesn't work the way I would expect it to based on documentation, though the behavior may be intended.

My assumption is that exitOverride would completely override the exit behavior of the Command, and that the provided callback would be called instead of calling process.exit. Instead, the callback is called and then, if the callback does not throw process.exit is called as well.

Here's a minimal example that will demonstrate this

import {Command} from 'commander'

let happy = false
const program = new Command()
    .exitOverride((err) => {
        happy = true
    })

program.parse([], {from: 'user'})

// This line will never be hit, and the process will exit with exit code 1
expect(happy).toBe(true)

The issue seems to be this snippet

  _exit(exitCode, code, message) {
    if (this._exitCallback) {
      this._exitCallback(new CommanderError(exitCode, code, message));
      // Expecting this line is not reached.
    }
    process.exit(exitCode);
  };

This looks like it should be an if-else.

If this is expected behavior, please close this (though it might be nice to update the documentation to cover this, since it confused the heck out of me).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions