Skip to content

Commit

Permalink
fix(run): Preserve none zero exit code (#6926)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
zhigang1992 authored and arcanis committed Mar 14, 2019
1 parent d4a46a5 commit bb69888
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions __tests__/integration.js
Expand Up @@ -450,6 +450,30 @@ test('yarn run <failing script>', async () => {
expect(stderr).toEqual('error Command failed with exit code 1.');
});

test('yarn run <failing script with custom exit code>', 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)');

Expand Down
5 changes: 5 additions & 0 deletions src/cli/index.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
}
Expand Down
5 changes: 4 additions & 1 deletion src/util/execute-lifecycle-script.js
Expand Up @@ -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;
}
Expand Down

0 comments on commit bb69888

Please sign in to comment.