Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(hosts): eol-consistent replace path in upsertSection
Shrish Dwivedi1d98bc66c709
diff --git a/src/hosts/sections.ts b/src/hosts/sections.tsindex 6fac9094..fef0e21a 100644--- a/src/hosts/sections.ts+++ b/src/hosts/sections.ts@@ -13,7 +13,10 @@ export type UpsertAction = 'created' | 'appended' | 'replaced' | 'unchanged';type LineEnding = '\n' | '\r\n';export function fencedBlock(body: string, eol: LineEnding = '\n'): string {- const block = `${START}\n${body.replace(/\s+$/, '')}\n${END}`;+ // Normalize any pre-existing '\r' out of the body first so callers passing+ // a CRLF (or stray-CR) body never get doubled '\r' when eol is '\r\n'.+ const normalizedBody = body.replace(/\r/g, '');+ const block = `${START}\n${normalizedBody.replace(/\s+$/, '')}\n${END}`;return eol === '\n' ? block : block.replace(/\n/g, '\r\n');}@@ -39,17 +42,24 @@ export function upsertSection(filePath: string, body: string): { action: UpsertA}const text = readFileSync(filePath, 'utf8');const eol = detectEol(text);- const block = fencedBlock(body, eol);- const lines = text.split('\n');+ // Split on either eol so lines never carry an embedded '\r' — that keeps+ // the marker/content comparison clean and lets us rejoin deliberately with+ // the detected eol instead of relying on '\r' characters riding along+ // inside array elements (which broke down whenever the block-with-no-+ // trailing-'\r' element sat next to a '\n' join, e.g. right after END, or+ // when the block was the entire file).+ const lines = text.split(/\r\n|\n/);const s = markerLineIndex(lines, START);const e = s === -1 ? -1 : markerLineIndex(lines, END, s + 1);if (s !== -1 && e !== -1) {const current = lines.slice(s, e + 1).join('\n');- if (current.replace(/\r/g, '') === block.replace(/\r/g, '')) return { action: 'unchanged' };- const next = [...lines.slice(0, s), ...block.split('\n'), ...lines.slice(e + 1)];- writeFileSync(filePath, next.join('\n'));+ if (current === fencedBlock(body)) return { action: 'unchanged' };+ const block = fencedBlock(body, eol);+ const next = [...lines.slice(0, s), ...block.split(eol), ...lines.slice(e + 1)];+ writeFileSync(filePath, next.join(eol));return { action: 'replaced' };}+ const block = fencedBlock(body, eol);const doubleEol = eol + eol;const sep = text.endsWith(doubleEol) ? '' : text.endsWith(eol) ? eol : doubleEol;writeFileSync(filePath, `${text}${sep}${block}${eol}`);diff --git a/test/hosts-sections.test.ts b/test/hosts-sections.test.tsindex 76383ffe..72fd280b 100644--- a/test/hosts-sections.test.ts+++ b/test/hosts-sections.test.ts@@ -77,3 +77,35 @@ test('CRLF file: appending produces a block using CRLF line endings throughout',// No bare '\n' without a preceding '\r' anywhere in the result.assert.equal(/(?<!\r)\n/.test(text), false, 'result must not mix LF into a CRLF file');});++test('CRLF file: replacing the block with surrounding content keeps CRLF throughout', () => {+ const f = join(fresh(), 'AGENTS.md');+ const block = fencedBlock('old body', '\r\n');+ writeFileSync(f, `above\r\n\r\n${block}\r\nbelow\r\n`);+ const r = upsertSection(f, 'new body');+ assert.equal(r.action, 'replaced');+ const text = readFileSync(f, 'utf8');+ assert.ok(text.includes('above'));+ assert.ok(text.includes('below'));+ assert.ok(text.includes('new body'));+ assert.ok(!text.includes('old body'));+ assert.equal(/(?<!\r)\n/.test(text), false, 'result must not mix LF into a CRLF file');+});++test('CRLF file: replacing when the block IS the entire file keeps CRLF throughout', () => {+ const f = join(fresh(), 'AGENTS.md');+ const block = fencedBlock('old body', '\r\n');+ writeFileSync(f, `${block}\r\n`);+ const r = upsertSection(f, 'new body');+ assert.equal(r.action, 'replaced');+ const text = readFileSync(f, 'utf8');+ assert.ok(text.includes('new body'));+ assert.ok(!text.includes('old body'));+ assert.equal(/(?<!\r)\n/.test(text), false, 'result must not mix LF into a CRLF file');+});++test('fencedBlock does not double "\\r" when the body already has CRLF line endings', () => {+ const block = fencedBlock('line one\r\nline two', '\r\n');+ assert.equal(/\r\r/.test(block), false, 'must not contain doubled carriage returns');+ assert.equal(/(?<!\r)\n/.test(block), false, 'every \\n must be preceded by \\r');+});