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

- 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);
}

zhigang1992 marked this conversation as resolved.
Show resolved Hide resolved
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