fix: wait for CLI error output before exiting (#4263)

Co-authored-by: 梁镇贤 <251972984+xiaoyao-666-coder@users.noreply.github.com>
This commit is contained in:
Zhenxian Liang
2026-11-20 19:20:48 -07:00
committed by GitHub
co-authored by 梁镇贤
parent 502e658115
commit 6c2a5ab48d
2 changed files with 31 additions and 6 deletions
+6 -5
View File
@@ -298,11 +298,12 @@ export async function main(nodeProcess) {
await start(nodeProcess.argv.slice(2));
nodeProcess.exit(0);
} catch(err) {
if (err.code === 'ENOENT') {
nodeProcess.stderr.write('marked: ' + err.path + ': No such file or directory');
} else {
nodeProcess.stderr.write(err.message);
}
const message = err.code === 'ENOENT'
? 'marked: ' + err.path + ': No such file or directory'
: err.message;
await new Promise(resolve => {
nodeProcess.stderr.write(message, resolve);
});
return nodeProcess.exit(1);
}
}
+25 -1
View File
@@ -32,7 +32,10 @@ function createMocks() {
}),
},
stderr: {
write: mock.fn((str) => { mocks.stderr += str; }),
write: mock.fn((str, callback) => {
mocks.stderr += str;
callback?.();
}),
},
stdin: {
setEncoding: mock.fn(),
@@ -115,6 +118,27 @@ describe('bin/marked', () => {
});
describe('output', () => {
it('writes all error output before exiting when stderr is piped', async() => {
const dir = await mkdtemp(resolve(tmpdir(), 'marked-stderr-'));
try {
const config = resolve(dir, 'config.cjs');
const message = 'config error '.repeat(131072);
await writeFile(config, `module.exports = () => { throw new Error(${JSON.stringify(message)}); };\n`);
await assert.rejects(execFileAsync(process.execPath, [
resolve(import.meta.dirname, '../../bin/marked.js'),
'--config', config, '--string', '# test',
], { maxBuffer: 4 * 1024 * 1024 }), (err) => {
assert.strictEqual(err.code, 1);
assert.strictEqual(err.stdout, '');
assert.strictEqual(err.stderr.length, message.length);
assert.strictEqual(err.stderr, message);
return true;
});
} finally {
await rm(dir, { recursive: true, force: true });
}
});
it('writes all output before exiting when stdout is piped', async() => {
const dir = await mkdtemp(resolve(tmpdir(), 'marked-stdout-'));
try {