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): preserve CRLF line endings and idempotency in upsertSection
Shrish Dwivedi8cae97aad31f
diff --git a/src/hosts/sections.ts b/src/hosts/sections.tsindex bec6ca07..6fac9094 100644--- a/src/hosts/sections.ts+++ b/src/hosts/sections.ts@@ -10,8 +10,16 @@ export const END = '<!-- graft:end -->';export type UpsertAction = 'created' | 'appended' | 'replaced' | 'unchanged';-export function fencedBlock(body: string): string {- return `${START}\n${body.replace(/\s+$/, '')}\n${END}`;+type LineEnding = '\n' | '\r\n';++export function fencedBlock(body: string, eol: LineEnding = '\n'): string {+ const block = `${START}\n${body.replace(/\s+$/, '')}\n${END}`;+ return eol === '\n' ? block : block.replace(/\n/g, '\r\n');+}++/** The file's dominant line ending: CRLF if any '\r\n' is present, else LF. */+function detectEol(text: string): LineEnding {+ return text.includes('\r\n') ? '\r\n' : '\n';}/** Index of a marker that sits alone on its own line, or -1. */@@ -23,24 +31,27 @@ function markerLineIndex(lines: string[], marker: string, from = 0): number {}export function upsertSection(filePath: string, body: string): { action: UpsertAction } {- const block = fencedBlock(body);if (!existsSync(filePath)) {+ const block = fencedBlock(body);mkdirSync(dirname(filePath), { recursive: true });writeFileSync(filePath, `${block}\n`);return { action: 'created' };}const text = readFileSync(filePath, 'utf8');+ const eol = detectEol(text);+ const block = fencedBlock(body, eol);const lines = text.split('\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 === block) return { action: 'unchanged' };+ 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'));return { action: 'replaced' };}- const sep = text.endsWith('\n\n') ? '' : text.endsWith('\n') ? '\n' : '\n\n';- writeFileSync(filePath, `${text}${sep}${block}\n`);+ const doubleEol = eol + eol;+ const sep = text.endsWith(doubleEol) ? '' : text.endsWith(eol) ? eol : doubleEol;+ writeFileSync(filePath, `${text}${sep}${block}${eol}`);return { action: 'appended' };}diff --git a/test/hosts-sections.test.ts b/test/hosts-sections.test.tsindex 7f67f58b..76383ffe 100644--- a/test/hosts-sections.test.ts+++ b/test/hosts-sections.test.ts@@ -54,3 +54,26 @@ test('ignores inline marker mentions (marker must be alone on its line)', () =>assert.equal(r.action, 'appended');assert.equal(readFileSync(f, 'utf8').match(/^<!-- graft:start -->$/gm)!.length, 1);});++test('CRLF file: no-op run reports unchanged and preserves bytes exactly', () => {+ const f = join(fresh(), 'AGENTS.md');+ const block = fencedBlock('same body', '\r\n');+ const original = `above\r\n\r\n${block}\r\nbelow\r\n`;+ writeFileSync(f, original);+ const r = upsertSection(f, 'same body');+ assert.equal(r.action, 'unchanged');+ const after = readFileSync(f, 'utf8');+ assert.equal(after, original, 'file must be byte-identical after a no-op run');+});++test('CRLF file: appending produces a block using CRLF line endings throughout', () => {+ const f = join(fresh(), 'AGENTS.md');+ writeFileSync(f, '# My rules\r\n\r\nBe nice.\r\n');+ const r = upsertSection(f, 'graft body');+ assert.equal(r.action, 'appended');+ const text = readFileSync(f, 'utf8');+ assert.ok(text.startsWith('# My rules\r\n\r\nBe nice.\r\n'));+ assert.ok(text.includes('graft body'));+ // 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');+});