Skip to content

Commit 8d29d6b

Browse files
committed
fix(cli): exit with non-zero code when the tool result is an error
Fixes: #42028
1 parent 0e057b2 commit 8d29d6b

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

packages/playwright-core/src/tools/cli-client/program.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ async function runInSession(entry: SessionFile, clientInfo: ClientInfo, args: Mi
295295
delete args[globalOption];
296296
const session = new Session(entry);
297297
const result = await session.run(clientInfo, args, { raw, json: output.json });
298+
if (result.isError)
299+
process.exitCode = 1;
298300
return result.text;
299301
}
300302

packages/playwright-core/src/tools/cli-client/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Session {
4242
return compareSemver(clientInfo.version, this.config.version) >= 0;
4343
}
4444

45-
async run(clientInfo: ClientInfo, args: MinimistArgs, options?: { raw?: boolean, json?: boolean }): Promise<{ text: string }> {
45+
async run(clientInfo: ClientInfo, args: MinimistArgs, options?: { raw?: boolean, json?: boolean }): Promise<{ text: string, isError?: boolean }> {
4646
if (!this.isCompatible(clientInfo))
4747
throw new Error(`Client is v${clientInfo.version}, session '${this.name}' is v${this.config.version}. Run\n\n playwright-cli${this.name !== 'default' ? ` -s=${this.name}` : ''} open\n\nto restart the browser session.`);
4848

tests/mcp/cli-core.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,18 @@ test('--raw on command without output', async ({ cli, server }) => {
369369
expect(output).not.toContain('Page URL');
370370
});
371371

372+
test('tool error exits with non-zero code', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42028' } }, async ({ cli, server }) => {
373+
await cli('open', server.HELLO_WORLD);
374+
375+
const { output, exitCode } = await cli('click', 'e999');
376+
expect(output).toContain('Ref e999 not found in the current page snapshot.');
377+
expect(exitCode).toBe(1);
378+
379+
const { output: jsonOutput, exitCode: jsonExitCode } = await cli('--json', 'click', 'e999');
380+
expect(JSON.parse(jsonOutput).isError).toBe(true);
381+
expect(jsonExitCode).toBe(1);
382+
});
383+
372384
test('codegen escapes single quotes in user input', async ({ cli, server }) => {
373385
server.setContent('/', `<input type=text>`, 'text/html');
374386
await cli('open', server.PREFIX);

0 commit comments

Comments
 (0)