From 8bad42e6fe4b9013cc66c0a8ce292d4d8246bcdc Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Tue, 15 Jan 2019 21:38:30 +0800 Subject: [PATCH 01/10] fix: preserve EXIT_CODE on close --- src/cli/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/index.js b/src/cli/index.js index a4b307d57a..7e84ceda34 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -583,7 +583,7 @@ export async function main({ reporter.info(command.getDocsInfo); } - return exit(1); + return exit(err['EXIT_CODE'] || 1); }); } From eb28bf0876eb06238b971cacbc70d81eee2a5de4 Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Tue, 15 Jan 2019 21:47:01 +0800 Subject: [PATCH 02/10] test: - add test fixture --- .../fixtures/run/preserve-exit-code/exitWithCode78.js | 1 + __tests__/fixtures/run/preserve-exit-code/package.json | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 __tests__/fixtures/run/preserve-exit-code/exitWithCode78.js create mode 100644 __tests__/fixtures/run/preserve-exit-code/package.json diff --git a/__tests__/fixtures/run/preserve-exit-code/exitWithCode78.js b/__tests__/fixtures/run/preserve-exit-code/exitWithCode78.js new file mode 100644 index 0000000000..4337f69e0e --- /dev/null +++ b/__tests__/fixtures/run/preserve-exit-code/exitWithCode78.js @@ -0,0 +1 @@ +process.exit(78); diff --git a/__tests__/fixtures/run/preserve-exit-code/package.json b/__tests__/fixtures/run/preserve-exit-code/package.json new file mode 100644 index 0000000000..2bfd329f07 --- /dev/null +++ b/__tests__/fixtures/run/preserve-exit-code/package.json @@ -0,0 +1,7 @@ +{ + "license": "MIT", + "scripts": { + "exitWithCode78": "node exitWithCode78.js" + } +} + From 65d2275f58499745e476a11ae2ed0e4a7be1d803 Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Tue, 15 Jan 2019 21:54:24 +0800 Subject: [PATCH 03/10] test: Move test to index folder --- .../fixtures/{run => index}/preserve-exit-code/exitWithCode78.js | 0 __tests__/fixtures/{run => index}/preserve-exit-code/package.json | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename __tests__/fixtures/{run => index}/preserve-exit-code/exitWithCode78.js (100%) rename __tests__/fixtures/{run => index}/preserve-exit-code/package.json (100%) diff --git a/__tests__/fixtures/run/preserve-exit-code/exitWithCode78.js b/__tests__/fixtures/index/preserve-exit-code/exitWithCode78.js similarity index 100% rename from __tests__/fixtures/run/preserve-exit-code/exitWithCode78.js rename to __tests__/fixtures/index/preserve-exit-code/exitWithCode78.js diff --git a/__tests__/fixtures/run/preserve-exit-code/package.json b/__tests__/fixtures/index/preserve-exit-code/package.json similarity index 100% rename from __tests__/fixtures/run/preserve-exit-code/package.json rename to __tests__/fixtures/index/preserve-exit-code/package.json From 977e65e1c453e4288a08f6216fcbc5ba44d2fdea Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Tue, 15 Jan 2019 22:05:52 +0800 Subject: [PATCH 04/10] fix: Check for ProcessTermError --- src/cli/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cli/index.js b/src/cli/index.js index 7e84ceda34..832a5239da 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,7 +584,11 @@ export async function main({ reporter.info(command.getDocsInfo); } - return exit(err['EXIT_CODE'] || 1); + if (err instanceof ProcessTermError) { + return exit(err.EXIT_CODE || 1); + } + + return exit(1); }); } From b58b9224cfd9ad331cfe4147cc379862bdddea8f Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Tue, 15 Jan 2019 22:31:50 +0800 Subject: [PATCH 05/10] fix: Passing EXIT_CODE along when converting ProcessTermError --- src/cli/index.js | 2 +- src/util/execute-lifecycle-script.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli/index.js b/src/cli/index.js index 832a5239da..aa36e1e7c9 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -588,7 +588,7 @@ export async function main({ return exit(err.EXIT_CODE || 1); } - return exit(1); + return exit((err.code && Number(err.code)) || 1); }); } diff --git a/src/util/execute-lifecycle-script.js b/src/util/execute-lifecycle-script.js index aae9a2d09d..6cd9222fe5 100644 --- a/src/util/execute-lifecycle-script.js +++ b/src/util/execute-lifecycle-script.js @@ -366,6 +366,7 @@ export async function execCommand({ err.EXIT_SIGNAL ? reporter.lang('commandFailedWithSignal', err.EXIT_SIGNAL) : reporter.lang('commandFailedWithCode', err.EXIT_CODE), + String(err.EXIT_CODE), ); } else { throw err; From 8cc39fe9cbf84434bec050ada857dee6772bfe59 Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Tue, 15 Jan 2019 22:33:49 +0800 Subject: [PATCH 06/10] test: Use integration to to custom exit code --- .../preserve-exit-code/exitWithCode78.js | 1 - .../index/preserve-exit-code/package.json | 7 ------ __tests__/integration.js | 24 +++++++++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) delete mode 100644 __tests__/fixtures/index/preserve-exit-code/exitWithCode78.js delete mode 100644 __tests__/fixtures/index/preserve-exit-code/package.json diff --git a/__tests__/fixtures/index/preserve-exit-code/exitWithCode78.js b/__tests__/fixtures/index/preserve-exit-code/exitWithCode78.js deleted file mode 100644 index 4337f69e0e..0000000000 --- a/__tests__/fixtures/index/preserve-exit-code/exitWithCode78.js +++ /dev/null @@ -1 +0,0 @@ -process.exit(78); diff --git a/__tests__/fixtures/index/preserve-exit-code/package.json b/__tests__/fixtures/index/preserve-exit-code/package.json deleted file mode 100644 index 2bfd329f07..0000000000 --- a/__tests__/fixtures/index/preserve-exit-code/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "license": "MIT", - "scripts": { - "exitWithCode78": "node exitWithCode78.js" - } -} - 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)'); From fae8f68a23929c41b091d9c25095d304f9bf6aca Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Tue, 15 Jan 2019 22:43:44 +0800 Subject: [PATCH 07/10] fix: flow type check --- src/cli/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cli/index.js b/src/cli/index.js index aa36e1e7c9..17b564746d 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -588,7 +588,11 @@ export async function main({ return exit(err.EXIT_CODE || 1); } - return exit((err.code && Number(err.code)) || 1); + if (err instanceof MessageError) { + return exit((err.code && Number(err.code)) || 1); + } + + return exit(1); }); } From 8992942f70a0fe093e5e0f514d173f867bb7534d Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Tue, 15 Jan 2019 22:51:12 +0800 Subject: [PATCH 08/10] doc: Add Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c941e3ef8f..9bd695bd1f 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 +- Preserve none zero exit code in `yarn run` + + [#6926](https://github.com/yarnpkg/yarn/pull/6926) - [**Kyle Fang**](https://github.com/zhigang1992) + - Improves PnP compatibility with Node 6 [#6871](https://github.com/yarnpkg/yarn/pull/6871) - [**Robert Jackson**](https://github.com/rwjblue) From b0f77daa23a19b256318f841df4c15e6e01e57f5 Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Thu, 24 Jan 2019 17:34:01 +0800 Subject: [PATCH 09/10] refactor: Throw new ProcessTermError instead of using code differently --- src/cli/index.js | 4 ---- src/util/execute-lifecycle-script.js | 6 ++++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cli/index.js b/src/cli/index.js index 17b564746d..832a5239da 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -588,10 +588,6 @@ export async function main({ return exit(err.EXIT_CODE || 1); } - if (err instanceof MessageError) { - return exit((err.code && Number(err.code)) || 1); - } - return exit(1); }); } diff --git a/src/util/execute-lifecycle-script.js b/src/util/execute-lifecycle-script.js index 6cd9222fe5..267ead1d66 100644 --- a/src/util/execute-lifecycle-script.js +++ b/src/util/execute-lifecycle-script.js @@ -362,12 +362,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), - String(err.EXIT_CODE), ); + formattedError.EXIT_CODE = err.EXIT_CODE; + formattedError.EXIT_SIGNAL = err.EXIT_SIGNAL; + throw formattedError; } else { throw err; } From 1d12fe1fe3c97c955dc5a0a63477888489754c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Thu, 14 Mar 2019 12:12:00 +0000 Subject: [PATCH 10/10] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98f4894e2b..314f78921c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa ## Master -- Preserve none zero exit code in `yarn run` +- 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)