From b25a981c931194263bdd44036be13d21009e8f32 Mon Sep 17 00:00:00 2001 From: Tanner Reits <47483144+tanner-reits@users.noreply.github.com> Date: Thu, 5 Sep 2024 10:15:21 -0400 Subject: [PATCH] fix(compiler): handle file rename in watch mode (#5968) Fixes: #3443 STENCIL-1104 --- src/compiler/build/watch-build.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/compiler/build/watch-build.ts b/src/compiler/build/watch-build.ts index 90aac617ccf..fd1435cbe3a 100644 --- a/src/compiler/build/watch-build.ts +++ b/src/compiler/build/watch-build.ts @@ -84,6 +84,26 @@ export const createWatchBuild = async ( ); } + // Make sure all files in the module map are still in the fs + // Otherwise, we can run into build errors because the compiler can think + // there are two component files with the same tag name + Array.from(compilerCtx.moduleMap.keys()).forEach((key) => { + if (filesUpdated.has(key) || filesDeleted.has(key)) { + // Check if the file exists in the fs + const fileExists = compilerCtx.fs.accessSync(key); + if (!fileExists) { + compilerCtx.moduleMap.delete(key); + } + } + }); + + // Make sure all added/updated files are watched + // We need to check both added/updates since the TS watch program behaves kinda weird + // and doesn't always handle file renames the same way + new Set([...filesUpdated, ...filesAdded]).forEach((filePath) => { + compilerCtx.addWatchFile(filePath); + }); + dirsAdded.clear(); dirsDeleted.clear(); filesAdded.clear();