From bb69888ff21cbff94e9d8583df025ea21e32a3f4 Mon Sep 17 00:00:00 2001 From: Zhigang Fang Date: Thu, 14 Mar 2019 20:12:30 +0800 Subject: [PATCH] fix(run): Preserve none zero exit code (#6926) * fix: preserve EXIT_CODE on close * test: - add test fixture * test: Move test to index folder * fix: Check for ProcessTermError * fix: Passing EXIT_CODE along when converting ProcessTermError * test: Use integration to to custom exit code * fix: flow type check * doc: Add Changelog * refactor: Throw new ProcessTermError instead of using code differently * Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ __tests__/integration.js | 24 ++++++++++++++++++++++++ src/cli/index.js | 5 +++++ src/util/execute-lifecycle-script.js | 5 ++++- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec1b8ce593..047580f990 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa ## Master +- Fixes a bug where non-zero exit codes were converted to a generic 1 when running `yarn run` + + [#6926](https://github.com/yarnpkg/yarn/pull/6926) - [**Kyle Fang**](https://github.com/zhigang1992) + - Fixes production / development reporting when running `yarn audit` [#6970](https://github.com/yarnpkg/yarn/pull/6970) - [**Adam Richardson**](https://github.com/as3richa) diff --git a/__tests__/integration.js b/__tests__/integration.js index 2703d371ad..4a57493ba1 100644 --- a/__tests__/integration.js +++ b/__tests__/integration.js @@ -450,6 +450,30 @@ test('yarn run ', async () => { expect(stderr).toEqual('error Command failed with exit code 1.'); }); +test('yarn run ', async () => { + const cwd = await makeTemp(); + + await fs.writeFile( + path.join(cwd, 'package.json'), + JSON.stringify({ + license: 'MIT', + scripts: {false: 'exit 78'}, + }), + ); + + let stderr = null; + let err = null; + try { + await runYarn(['run', 'false'], {cwd}); + } catch (e) { + stderr = e.stderr.trim(); + err = e.code; + } + + expect(err).toEqual(78); + expect(stderr).toEqual('error Command failed with exit code 78.'); +}); + test('yarn run in path need escaping', async () => { const cwd = await makeTemp('special (chars)'); diff --git a/src/cli/index.js b/src/cli/index.js index d0c58f1c6b..60cd2c2597 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -24,6 +24,7 @@ import {spawnp, forkp} from '../util/child.js'; import {version} from '../util/yarn-version.js'; import handleSignals from '../util/signal-handler.js'; import {boolify, boolifyWithDefault} from '../util/conversion.js'; +import {ProcessTermError} from '../errors'; function findProjectRoot(base: string): string { let prev = null; @@ -583,6 +584,10 @@ export async function main({ reporter.info(command.getDocsInfo); } + if (err instanceof ProcessTermError) { + return exit(err.EXIT_CODE || 1); + } + return exit(1); }); } diff --git a/src/util/execute-lifecycle-script.js b/src/util/execute-lifecycle-script.js index 02dc6b29c0..dcbbc008d2 100644 --- a/src/util/execute-lifecycle-script.js +++ b/src/util/execute-lifecycle-script.js @@ -351,11 +351,14 @@ export async function execCommand({ return Promise.resolve(); } catch (err) { if (err instanceof ProcessTermError) { - throw new MessageError( + const formattedError = new ProcessTermError( err.EXIT_SIGNAL ? reporter.lang('commandFailedWithSignal', err.EXIT_SIGNAL) : reporter.lang('commandFailedWithCode', err.EXIT_CODE), ); + formattedError.EXIT_CODE = err.EXIT_CODE; + formattedError.EXIT_SIGNAL = err.EXIT_SIGNAL; + throw formattedError; } else { throw err; }