Skip to content

Commit e326cde

Browse files
authored
fix: exit after async handler done (#2313)
1 parent 8343c66 commit e326cde

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/yargs-factory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,8 +2162,10 @@ export class YargsInstance {
21622162
if (helpOptSet) {
21632163
if (this.#exitProcess) setBlocking(true);
21642164
skipValidation = true;
2165-
this.showHelp('log');
2166-
this.exit(0);
2165+
this.showHelp(message => {
2166+
this.#logger.log(message);
2167+
this.exit(0);
2168+
});
21672169
} else if (versionOptSet) {
21682170
if (this.#exitProcess) setBlocking(true);
21692171
skipValidation = true;

test/usage.cjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const chalk = require('chalk');
77
const yargs = require('../index.cjs');
88
const expect = require('chai').expect;
99
const {YError} = require('../build/index.cjs');
10+
const assert = require('assert');
1011

1112
const should = require('chai').should();
1213

@@ -4877,6 +4878,33 @@ describe('usage tests', () => {
48774878
help.split('\n').should.deep.equal(expected);
48784879
});
48794880
});
4881+
4882+
it('help is displayed before exit is called with async default command', async () => {
4883+
// https://github.com/yargs/yargs/issues/2312
4884+
const _exit = process.exit;
4885+
const _log = console.log;
4886+
let callCount = 0;
4887+
let logCall = 0;
4888+
let exitCall = 0;
4889+
process.exit = () => {
4890+
exitCall = ++callCount;
4891+
};
4892+
console.log = () => {
4893+
logCall = ++callCount;
4894+
};
4895+
await yargs(['--help'])
4896+
.command('$0', 'a test command', async yargs => {
4897+
await wait();
4898+
})
4899+
.parseAsync();
4900+
// The async help is dangling, so wait for it to fire!
4901+
await wait();
4902+
assert.ok(exitCall > 0, 'exit never called'); // sanity check
4903+
assert.ok(logCall > 0, 'log never called'); // sanity check
4904+
assert.ok(exitCall > logCall, 'exit called before help displayed');
4905+
console.log = _log;
4906+
process.exit = _exit;
4907+
});
48804908
});
48814909

48824910
// Refs: https://github.com/yargs/yargs/issues/1820

0 commit comments

Comments
 (0)