Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore: create socket file in cwd (#38855)
Yury Semikhatskyc8e312b7f364
diff --git a/packages/playwright/src/mcp/terminal/cli.ts b/packages/playwright/src/mcp/terminal/cli.tsindex 0aa1f280ff..b59fc1ff7a 100644--- a/packages/playwright/src/mcp/terminal/cli.ts+++ b/packages/playwright/src/mcp/terminal/cli.ts@@ -227,9 +227,25 @@ class SocketSession {}function daemonSocketPath(): string {- if (os.platform() === 'win32')- return path.join('\\\\.\\pipe', 'pw-daemon.sock');- return path.join(os.homedir(), '.playwright', 'pw-daemon.sock');+ const socketPath = path.resolve('.playwright.sock');+ return normalizeSocketPath(socketPath);+}++/**+ * Normalize socket path for the current platform.+ * On Windows, converts Unix-style paths to named pipe format.+ * On Unix, returns the path as-is.+ */+function normalizeSocketPath(path: string): string {+ if (os.platform() === 'win32') {+ // Windows named pipes use \\.\pipe\name format+ if (path.startsWith('\\\\.\\pipe\\'))+ return path;+ // Convert Unix-style path to Windows named pipe+ const name = path.replace(/[^a-zA-Z0-9]/g, '-');+ return `\\\\.\\pipe\\${name}`;+ }+ return path;}async function connectToDaemon(options: { headless?: boolean }): Promise<SocketSession> {@@ -251,6 +267,7 @@ async function connectToDaemon(options: { headless?: boolean }): Promise<SocketSconst child = spawn(process.execPath, [cliPath, 'run-mcp-server', `--daemon=${socketPath}`, ...(options.headless ? ['--headless'] : [])], {detached: true,stdio: 'ignore',+ cwd: process.cwd(), // Will be used as root.});child.unref();diff --git a/packages/playwright/src/mcp/terminal/daemon.ts b/packages/playwright/src/mcp/terminal/daemon.tsindex 8213510b8e..022937c384 100644--- a/packages/playwright/src/mcp/terminal/daemon.ts+++ b/packages/playwright/src/mcp/terminal/daemon.ts@@ -18,6 +18,7 @@ import fs from 'fs/promises';import net from 'net';import os from 'os';import path from 'path';+import url from 'url';import { debug } from 'playwright-core/lib/utilsBundle';import { SocketConnection } from './socketConnection';@@ -26,23 +27,6 @@ import type { ServerBackendFactory } from '../sdk/server';const daemonDebug = debug('pw:daemon');-/**- * Normalize socket path for the current platform.- * On Windows, converts Unix-style paths to named pipe format.- * On Unix, returns the path as-is.- */-function normalizeSocketPath(path: string): string {- if (os.platform() === 'win32') {- // Windows named pipes use \\.\pipe\name format- if (path.startsWith('\\\\.\\pipe\\'))- return path;- // Convert Unix-style path to Windows named pipe- const name = path.replace(/[^a-zA-Z0-9]/g, '-');- return `\\\\.\\pipe\\${name}`;- }- return path;-}-async function socketExists(socketPath: string): Promise<boolean> {try {const stat = await fs.stat(socketPath);@@ -52,6 +36,7 @@ async function socketExists(socketPath: string): Promise<boolean> {}return false;}+/*** Start a daemon server listening on Unix domain socket (Unix) or named pipe (Windows).*/@@ -59,13 +44,11 @@ export async function startMcpDaemonServer(socketPath: string,serverBackendFactory: ServerBackendFactory): Promise<string> {- const normalizedPath = normalizeSocketPath(socketPath);-// Clean up existing socket file on Unix- if (os.platform() !== 'win32' && await socketExists(normalizedPath)) {- daemonDebug(`Socket already exists, removing: ${normalizedPath}`);+ if (os.platform() !== 'win32' && await socketExists(socketPath)) {+ daemonDebug(`Socket already exists, removing: ${socketPath}`);try {- await fs.unlink(normalizedPath);+ await fs.unlink(socketPath);} catch (error) {daemonDebug(`Failed to remove existing socket: ${error}`);throw error;@@ -73,14 +56,18 @@ export async function startMcpDaemonServer(}const backend = serverBackendFactory.create();+ const cwd = url.pathToFileURL(process.cwd()).href;await backend.initialize?.({- name: 'mcp-daemon',+ name: 'playwright-cli',version: '1.0.0',- roots: [],+ roots: [{+ uri: cwd,+ name: 'cwd'+ }],timestamp: Date.now(),});- await fs.mkdir(path.dirname(normalizedPath), { recursive: true });+ await fs.mkdir(path.dirname(socketPath), { recursive: true });const server = net.createServer(socket => {daemonDebug('new client connection');@@ -110,9 +97,9 @@ export async function startMcpDaemonServer(reject(error);});- server.listen(normalizedPath, () => {- daemonDebug(`daemon server listening on ${normalizedPath}`);- resolve(normalizedPath);+ server.listen(socketPath, () => {+ daemonDebug(`daemon server listening on ${socketPath}`);+ resolve(socketPath);});});}