diff --git a/.changeset/two-garlics-try.md b/.changeset/two-garlics-try.md new file mode 100644 index 000000000..1b9ced394 --- /dev/null +++ b/.changeset/two-garlics-try.md @@ -0,0 +1,5 @@ +--- +'@halfdomelabs/sync': patch +--- + +Ensure files are not regenerated if they have been deleted and are unmodified diff --git a/packages/sync/src/output/prepare-generator-files/prepare-generator-file.ts b/packages/sync/src/output/prepare-generator-files/prepare-generator-file.ts index 7ba0bdf55..8a2ffeafa 100644 --- a/packages/sync/src/output/prepare-generator-files/prepare-generator-file.ts +++ b/packages/sync/src/output/prepare-generator-files/prepare-generator-file.ts @@ -210,12 +210,12 @@ async function mergeStringContents({ } /** - * Find the relative path of the file in the previous working codebase + * Find the relative path of the file in the previous generated codebase * * @param data - File data * @param relativePath - Relative path of the file * @param context - Generator output file writer context - * @returns The relative path of the file in the previous working codebase or undefined if it does not exist + * @returns The relative path of the file in the previous generated codebase or undefined if it does not exist */ async function findPreviousRelativePath( data: FileData, @@ -316,9 +316,10 @@ export async function prepareGeneratorFile({ // If we haven't modified the generated version of the file, // we use the previous working file version - const previousGeneratedBuffer = previousRelativePath - ? await previousGeneratedPayload?.fileReader.readFile(previousRelativePath) - : undefined; + const previousGeneratedBuffer = + await previousGeneratedPayload?.fileReader.readFile( + previousRelativePath ?? relativePath, + ); if ( previousGeneratedBuffer && diff --git a/packages/sync/src/output/prepare-generator-files/prepare-generator-file.unit.test.ts b/packages/sync/src/output/prepare-generator-files/prepare-generator-file.unit.test.ts index a44e58404..148fa4007 100644 --- a/packages/sync/src/output/prepare-generator-files/prepare-generator-file.unit.test.ts +++ b/packages/sync/src/output/prepare-generator-files/prepare-generator-file.unit.test.ts @@ -181,6 +181,38 @@ describe('prepareGeneratorFile', () => { }); }); + it('should not re-write a deleted file when content is unchanged', async () => { + const content = 'unchanged content'; + const workingFiles = new Map(); + + const previousGeneratedFiles = new Map([ + ['file.txt', Buffer.from(content)], + ]); + + const mockPreviousPayload: PreviousGeneratedPayload = { + fileReader: createCodebaseReaderFromMemory(previousGeneratedFiles), + fileIdToRelativePathMap: new Map([[DEFAULT_FILE_ID, 'file.txt']]), + }; + + const result = await prepareGeneratorFile({ + relativePath: 'file.txt', + data: createMockFileData({ + contents: content, + }), + context: createMockContext({ + previousWorkingCodebase: createCodebaseReaderFromMemory(workingFiles), + previousGeneratedPayload: mockPreviousPayload, + }), + }); + + expect(result).toEqual({ + relativePath: 'file.txt', + previousRelativePath: undefined, + mergedContents: undefined, + generatedContents: Buffer.from(content), + }); + }); + it('should handle JSON files with special merge algorithm', async () => { const workingFiles = new Map([ ['config.json', Buffer.from('{"key": "original","other": "other"}')],