Repository navigation
fix(browser): suggest custom profiles for missing port files - #3496
AmarGhugare wants to merge 1 commit into
Conversation
|
@nattallius could you please help with review of this fix? |
|
Sure, i will |
nattallius
left a comment
There was a problem hiding this comment.
Thanks for the PR. Please note that #3487, which is also open, changes the same error handling in BrowserManager. It adds devToolsActivePortPermissionError() to src/utils/errorHandling.ts to detect permission errors. Please follow the same approach here: move the ENOENT check into a helper in that file and add a unit test for it. Both PRs change the same catch block, so whichever one merges second will need to rebase and combine both changes.
Also, please change Fixes #889 to Refs #889 in the description. This PR improves the error message, but the docs for using --autoConnect with --userDataDir are still missing, so the issue should stay open.
| ' If Chrome uses a custom profile directory, ' + | ||
| 'pass --user-data-dir with that directory.'; |
There was a problem hiding this comment.
--user-data-dir is also a Chrome switch, and docs/advanced-usage.md tells users to start Chrome with it. A user can read this hint as "restart Chrome with --user-data-dir", which doesn't fix the connection. Please name the MCP option, for example:
' If the browser uses a non-default profile directory (for example, ' +
'Chromium), pass --userDataDir=<profile dir> to chrome-devtools-mcp.'| 'by going to chrome://inspect/#remote-debugging.' | ||
| : 'Check if Chrome is running.'; | ||
| if ( | ||
| connectOptions.channel && |
There was a problem hiding this comment.
connectOptions.channel isn't needed: Puppeteer only throws Could not find DevToolsActivePort from its channel path, and the userDataDir path throws its own error before puppeteer.connect is called.
| connectOptions.channel && | ||
| err instanceof Error && | ||
| err.message.startsWith('Could not find DevToolsActivePort for ') && | ||
| typeof err.cause === 'object' && |
There was a problem hiding this comment.
The typeof / null / 'code' in checks only exist to narrow err.cause for TypeScript. Please replace them with one type guard in src/utils/errorHandling.ts, so this check and isErrnoPermissionError() from #3487 can share it. For example:
function hasErrnoCode(error: unknown, ...codes: string[]): boolean {
return (
error instanceof Error &&
'code' in error &&
typeof error.code === 'string' &&
codes.includes(error.code)
);
}Then this check becomes hasErrnoCode(err.cause, 'ENOENT').
|
|
||
| for (const {name, message, cause} of [ | ||
| { | ||
| name: 'a permission error (EPERM)', |
There was a problem hiding this comment.
Please remove the EPERM and EACCES cases. #3487 covers this path. The malformed-file and ECONNREFUSED cases are the useful ones: they have the same outer message as ENOENT, which shows why the code check is needed.
|
|
||
| await assert.rejects(manager.ensureBrowser(), error => { | ||
| assert.ok(error instanceof Error); | ||
| assert.doesNotMatch(error.message, /--user-data-dir/); |
There was a problem hiding this comment.
Please compare the full message with assert.strictEqual instead. If the hint wording changes, this regex no longer matches, and these tests keep passing even if the hint appears in the wrong case.
assert.strictEqual(
error.message,
'Could not connect to Chrome. Check if Chrome is running and remote ' +
'debugging is enabled by going to chrome://inspect/#remote-debugging.',
);
Fixes #889.
When channel-based auto-connect cannot find the default profile's DevToolsActivePort file, suggest
--user-data-dirfor a custom profile. Keep the existing running-browser/remote-debugging guidance and the original error cause.Only add the hint for a missing-file (ENOENT) cause. Puppeteer uses the same outer message for malformed port files and refused connections, where changing the profile would be misleading. Permission failures also keep their existing behavior; those are covered separately by #3487. The README guidance for #889 was already updated in #3386.
Validation on Windows, Node 24.19.0, Chrome 157.0.8103.78:
npm run test -- tests/browser.test.ts: 41 passed.npm run format: passed.npm run test: 1,985 passed, 5 skipped, zero failures.