Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(run): Preserve none zero exit code #6926

Merged
merged 12 commits into from Mar 14, 2019
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

- 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)
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
9 changes: 9 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,14 @@ export async function main({
reporter.info(command.getDocsInfo);
}

if (err instanceof ProcessTermError) {
return exit(err.EXIT_CODE || 1);
}

zhigang1992 marked this conversation as resolved.
Show resolved Hide resolved
if (err instanceof MessageError) {
return exit((err.code && Number(err.code)) || 1);
}
zhigang1992 marked this conversation as resolved.
Show resolved Hide resolved

return exit(1);
});
}
Expand Down
1 change: 1 addition & 0 deletions src/util/execute-lifecycle-script.js
Expand Up @@ -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),
zhigang1992 marked this conversation as resolved.
Show resolved Hide resolved
);
} else {
throw err;
Expand Down