Skip to content

Chrome DevTools for coding agents

fix: better config support for chrome-devtools CLI (#2819)

Nikolay Vitkov9624e64aada5
diff --git a/src/bin/chrome-devtools.ts b/src/bin/chrome-devtools.ts
index 15b4cc43..88a072cf 100644
--- a/src/bin/chrome-devtools.ts
+++ b/src/bin/chrome-devtools.ts
@@ -35,7 +35,7 @@ import {commands} from '../config/cli-options.js';
import {
mcpOptions,
parseArguments,
- getMcpOptionsForViaCli,
+ getCliOptions,
} from '../config/mcp-options.js';
await checkForUpdates(
@@ -44,25 +44,18 @@ await checkForUpdates(
const DEFAULT_CLI_ARGS = ['--viaCli'];
-async function start(args: string[], sessionId: string) {
+async function start(args: string[], sessionId: string, stopExisting = false) {
const combinedArgs = [...DEFAULT_CLI_ARGS, ...args];
+ const parsedArgs = parseArguments(VERSION, [
+ process.execPath,
+ process.argv[1],
+ ...combinedArgs,
+ ]);
+ if (stopExisting && isDaemonRunning(sessionId)) {
+ await stopDaemon(sessionId);
+ }
await startDaemon(combinedArgs, sessionId);
- logDisclaimers(parseArguments(VERSION, combinedArgs));
-}
-
-function getCliOptions() {
- const options: Partial<typeof mcpOptions> = {
- ...getMcpOptionsForViaCli(),
- };
-
- // Missing CLI serialization.
- delete options.viewport;
-
- // Change the defaults for the CLI.
- delete options.experimentalStructuredContent;
- delete options.experimentalInteropTools;
-
- return options;
+ logDisclaimers(parsedArgs);
}
const y = yargs(hideBin(process.argv))
@@ -135,29 +128,8 @@ y.command(
)
.strict(),
async argv => {
- if (isDaemonRunning(argv.sessionId)) {
- await stopDaemon(argv.sessionId);
- }
- // Defaults but we do not want to affect the yargs conflict resolution.
- if (
- argv.isolated === undefined &&
- argv.userDataDir === undefined &&
- !argv.autoConnect &&
- !argv.browserUrl &&
- !argv.wsEndpoint
- ) {
- argv.isolated = true;
- }
- if (
- argv.headless === undefined &&
- !argv.autoConnect &&
- !argv.browserUrl &&
- !argv.wsEndpoint
- ) {
- argv.headless = true;
- }
const args = serializeArgs(getCliOptions(), argv);
- await start(args, argv.sessionId);
+ await start(args, argv.sessionId, /* stopExisting= */ true);
process.exit(0);
},
).strict(); // Re-enable strict validation for other commands; this is applied to the yargs instance itself
diff --git a/src/config/mcp-options.ts b/src/config/mcp-options.ts
index 0f7cfb53..5935c4fa 100644
--- a/src/config/mcp-options.ts
+++ b/src/config/mcp-options.ts
@@ -8,6 +8,7 @@ import type {YargsOptions} from '../third_party/index.js';
import {yargs, hideBin} from '../third_party/index.js';
import os from 'node:os';
import {readFileSync} from 'node:fs';
+import path from 'node:path';
export const DEFAULT_FILESYSTEM_ROOT = [os.tmpdir()];
@@ -273,6 +274,12 @@ export const mcpOptions = {
config: {
type: 'string',
describe: 'Path to JSON configuration file.',
+ coerce: (configPath: string | undefined) => {
+ if (!configPath) {
+ return;
+ }
+ return path.resolve(configPath);
+ },
},
} satisfies Record<string, YargsOptions>;
@@ -317,6 +324,36 @@ export function getMcpOptionsForViaCli(): typeof mcpOptions {
};
}
+export function getCliOptions(): Partial<
+ Record<keyof typeof mcpOptions, YargsOptions>
+> {
+ const options: Partial<Record<keyof typeof mcpOptions, YargsOptions>> = {
+ ...getMcpOptionsForViaCli(),
+ };
+
+ // Missing CLI serialization.
+ delete options.viewport;
+
+ // Change the defaults for the CLI.
+ delete options.experimentalStructuredContent;
+ delete options.experimentalInteropTools;
+
+ const recordOptions: Record<string, YargsOptions | undefined> = options;
+ for (const [key, option] of Object.entries(recordOptions)) {
+ if (option?.default !== undefined) {
+ const copy: YargsOptions = {
+ ...option,
+ defaultDescription:
+ option.defaultDescription ?? JSON.stringify(option.default),
+ };
+ delete copy.default;
+ recordOptions[key] = copy;
+ }
+ }
+
+ return options;
+}
+
/**
* Exported only for testing to not trigger process exit.
*/
@@ -337,13 +374,25 @@ export function parser(
.options(options)
.showHelpOnFail(false, 'Specify --help for available options')
.middleware(args => {
- if (isViaCli && args.filesystemRoot === DEFAULT_FILESYSTEM_ROOT) {
- const cliFilesystemArgs: {
- allowUnrestrictedPaths?: boolean;
- filesystemRoot?: unknown;
- } = args;
- cliFilesystemArgs.allowUnrestrictedPaths = true;
- cliFilesystemArgs.filesystemRoot = undefined;
+ if (isViaCli) {
+ if (args.filesystemRoot === DEFAULT_FILESYSTEM_ROOT) {
+ const cliFilesystemArgs: {
+ allowUnrestrictedPaths?: boolean;
+ filesystemRoot?: unknown;
+ } = args;
+ cliFilesystemArgs.allowUnrestrictedPaths = true;
+ cliFilesystemArgs.filesystemRoot = undefined;
+ }
+ // Defaults that cannot be set in options without affecting yargs conflict resolution.
+ if (
+ args.isolated === undefined &&
+ args.userDataDir === undefined &&
+ !args.autoConnect &&
+ !args.browserUrl &&
+ !args.wsEndpoint
+ ) {
+ args.isolated = true;
+ }
}
// We can't set default in the options else
// Yargs will complain
diff --git a/tests/cli.test.ts b/tests/cli.test.ts
index 55bad6f7..98f20a7a 100644
--- a/tests/cli.test.ts
+++ b/tests/cli.test.ts
@@ -5,12 +5,14 @@
*/
import assert from 'node:assert';
+import path from 'node:path';
import {describe, it} from 'node:test';
import {buildCommand} from '../src/config/cli-commands.js';
import {commands} from '../src/config/cli-options.js';
import {
DEFAULT_FILESYSTEM_ROOT,
+ getCliOptions,
mcpOptions,
parser,
} from '../src/config/mcp-options.js';
@@ -61,6 +63,7 @@ describe('cli args parsing', () => {
const args = parseArguments(['--viaCli']);
assert.strictEqual(args.allowUnrestrictedPaths, true);
assert.strictEqual(args.headless, true);
+ assert.strictEqual(args.isolated, true);
assert.strictEqual(args.memoryDebugging, true);
assert.strictEqual(args.categoryExtensions, true);
assert.strictEqual(args.experimentalStructuredContent, true);
@@ -501,6 +504,33 @@ describe('cli args parsing', () => {
assert.deepStrictEqual(args.viewport, {width: 800, height: 600});
});
+ it('resolves relative config path and respects config with viaCli', async () => {
+ using testConfig = createTempFile(
+ JSON.stringify({
+ userDataDir: '/tmp/custom-profile',
+ headless: false,
+ }),
+ 'cd4a.test.config.viacli.json',
+ );
+ const relativePath = path.relative(process.cwd(), testConfig.path);
+ const args = parseArguments(['--viaCli', '--config', relativePath]);
+ assert.strictEqual(args.config, testConfig.path);
+ assert.strictEqual(args.userDataDir, '/tmp/custom-profile');
+ assert.strictEqual(args.isolated, undefined);
+ assert.strictEqual(args.headless, false);
+ });
+
+ it('respects isolated=false in config with viaCli', async () => {
+ using testConfig = createTempFile(
+ JSON.stringify({
+ isolated: false,
+ }),
+ 'cd4a.test.config.viacli-isolated.json',
+ );
+ const args = parseArguments(['--viaCli', '--config', testConfig.path]);
+ assert.strictEqual(args.isolated, false);
+ });
+
it('parses config should not allow no prefix', async () => {
using testConfig = createTempFile(
JSON.stringify({
@@ -533,6 +563,29 @@ describe('cli args parsing', () => {
const args = parseArguments(['--devtoolsComments']);
assert.strictEqual(args.devtoolsComments, true);
});
+
+ it('clears default values and populates defaultDescription in getCliOptions', () => {
+ const cliOptions = getCliOptions();
+
+ assert.strictEqual(cliOptions.viewport, undefined);
+ assert.strictEqual(cliOptions.experimentalStructuredContent, undefined);
+ assert.strictEqual(cliOptions.experimentalInteropTools, undefined);
+
+ for (const [key, option] of Object.entries(cliOptions)) {
+ assert.strictEqual(
+ option && 'default' in option,
+ false,
+ `Expected 'default' property for ${key} to be omitted`,
+ );
+ }
+
+ assert.strictEqual(cliOptions.headless?.defaultDescription, 'true');
+ assert.strictEqual(cliOptions.memoryDebugging?.defaultDescription, 'true');
+ assert.strictEqual(
+ cliOptions.filesystemRoot?.defaultDescription,
+ 'OS temp directory',
+ );
+ });
});
describe('cli command strings', () => {
diff --git a/tests/e2e/chrome-devtools-start-stop.test.ts b/tests/e2e/chrome-devtools-start-stop.test.ts
index 755c6b5e..2d4c1cb0 100644
--- a/tests/e2e/chrome-devtools-start-stop.test.ts
+++ b/tests/e2e/chrome-devtools-start-stop.test.ts
@@ -13,6 +13,7 @@ import {
assertDaemonIsNotRunning,
assertDaemonIsRunning,
createTempDir,
+ createTempFile,
runCli,
} from '../utils.js';
@@ -111,4 +112,60 @@ describe('chrome-devtools', () => {
`workspace was not forwarded: ${statusResult.stdout}`,
);
});
+
+ it('can start the daemon with a config file', async () => {
+ using userDataDir = createTempDir('chrome-devtools-config-profile-');
+ using configFile = createTempFile(
+ JSON.stringify({
+ userDataDir: userDataDir.path,
+ headless: true,
+ }),
+ 'chrome-devtools-config.json',
+ );
+
+ const relativeConfigPath = path.relative(process.cwd(), configFile.path);
+ const startResult = await runCli(
+ ['start', '--config', relativeConfigPath],
+ sessionId,
+ );
+ assert.strictEqual(
+ startResult.status,
+ 0,
+ `start command failed: ${startResult.stderr}`,
+ );
+
+ const statusResult = await runCli(['status'], sessionId);
+ assert.strictEqual(statusResult.status, 0);
+ assert.ok(
+ statusResult.stdout.includes(
+ JSON.stringify(`--config=${configFile.path}`),
+ ),
+ `resolved config path was not forwarded: ${statusResult.stdout}`,
+ );
+ assert.ok(
+ !statusResult.stdout.includes('--headless'),
+ `default --headless should not be forwarded when not specified on CLI: ${statusResult.stdout}`,
+ );
+ assert.ok(
+ !statusResult.stdout.includes('--filesystem-root'),
+ `default --filesystem-root should not be forwarded when not specified on CLI: ${statusResult.stdout}`,
+ );
+
+ const overrideResult = await runCli(
+ ['start', '--config', relativeConfigPath, '--headless'],
+ sessionId,
+ );
+ assert.strictEqual(
+ overrideResult.status,
+ 0,
+ `start command with --headless override failed: ${overrideResult.stderr}`,
+ );
+
+ const overrideStatusResult = await runCli(['status'], sessionId);
+ assert.strictEqual(overrideStatusResult.status, 0);
+ assert.ok(
+ overrideStatusResult.stdout.includes('"--headless"'),
+ `explicit --headless CLI flag was not forwarded: ${overrideStatusResult.stdout}`,
+ );
+ });
});