diff --git a/src/application/ClientConfigurationDocument.ts b/src/application/ClientConfigurationDocument.ts index 0b96a79d3..2b8a83a22 100644 --- a/src/application/ClientConfigurationDocument.ts +++ b/src/application/ClientConfigurationDocument.ts @@ -80,12 +80,22 @@ export const clientConfigurationServersKey = ( } }; +/** + * Whether raw configuration text holds no settings to preserve. Some clients + * create a zero-byte config file before any server is added. + */ +const isEmptyClientConfigurationText = (text: string): boolean => + text.replace(/^\uFEFF/, "").trim() === ""; + const parseDocument = ( text: string, format: ClientConfigurationFormat, ): Record => { if (format === "toml" || format === "grok") return objectSchema.parse(parseToml(text)); + // An empty file, which some clients create before any server is added, + // holds no settings to preserve. + if (isEmptyClientConfigurationText(text)) return {}; const errors: ParseError[] = []; // Accept a UTF-8 BOM without shifting diagnostics or editing the original text. const jsonText = text.startsWith("\uFEFF") ? ` ${text.slice(1)}` : text; @@ -1148,7 +1158,13 @@ export const serializeClientConfiguration = ( if (format === "toml") return stringifyToml(document); if (format === "grok") return serializeGrokConfiguration(document, originalText); - if (originalText !== undefined && editedPaths !== undefined) + // An empty original holds no comments or settings to preserve. Writing a + // fresh document also keeps a lone BOM from shifting past the closing brace. + if ( + originalText !== undefined && + !isEmptyClientConfigurationText(originalText) && + editedPaths !== undefined + ) return editedPaths.reduce((text, path) => { const value = valueAt(document, path); if (value === undefined) { diff --git a/tests/boundary/filesystem/clientConfigurationBom.test.ts b/tests/boundary/filesystem/clientConfigurationBom.test.ts index 92b5d9628..7dcf9ae04 100644 --- a/tests/boundary/filesystem/clientConfigurationBom.test.ts +++ b/tests/boundary/filesystem/clientConfigurationBom.test.ts @@ -149,3 +149,33 @@ it("restores original BOM-prefixed JSONC bytes after update and removal", async }); expect(await readFile(client.configPath, "utf8")).toBe(original); }); + +it.each(["", "\n \n", "\uFEFF"])( + "configures an empty client file %j as a new document", + async (original) => { + const home = await createTestTempDirectory("rea-client-config-empty-"); + const client = supportedClients(home, "linux", {}).find( + (candidate) => candidate.name === "antigravity", + ); + if (client?.format !== "json") + throw new Error("missing Antigravity JSON config"); + await mkdir(dirname(client.configPath), { recursive: true }); + await writeFile(client.configPath, original); + const command = [join(home, "rea"), "mcp"]; + + expect(await inspectClientConfiguration(client, {}, command)).toEqual({ + status: "update", + backupPath: `${client.configPath}.rea.backup`, + }); + expect(await configureClientConfiguration(client, {}, command)).toEqual({ + status: "configured", + backupPath: `${client.configPath}.rea.backup`, + }); + expect(JSON.parse(await readFile(client.configPath, "utf8"))).toEqual({ + mcpServers: { rea: clientRegistrationEntry(client.format, command, {}) }, + }); + expect(await inspectClientConfiguration(client, {}, command)).toEqual({ + status: "already_current", + }); + }, +);