@@ -151,8 +151,8 @@ export function initWatcher(filesDir: string): Watcher {
151151 }
152152
153153 /**
154- * Resolves the sanitized relative path for a given absolute path .
155- * On "add", renames files on disk if they don't match sanitization rules .
154+ * Resolves the relative path identity for a watcher event .
155+ * Only "add" may rewrite that identity by successfully sanitizing on disk .
156156 */
157157 const resolveRelativePath = async (
158158 kind : "add" | "change" | "delete" ,
@@ -163,28 +163,32 @@ export function initWatcher(filesDir: string): Watcher {
163163 }
164164
165165 const rawRelativePath = normalizePath ( getRelativePath ( filesDir , absolutePath ) )
166- const sanitized = sanitizeFilePath ( rawRelativePath , false )
167- const relativePath = sanitized . path
166+ let relativePath = rawRelativePath
168167
169168 let effectiveAbsolutePath = absolutePath
170- if ( relativePath !== rawRelativePath && kind === "add" ) {
171- const newAbsolutePath = path . join ( filesDir , relativePath )
172- try {
173- await fs . mkdir ( path . dirname ( newAbsolutePath ) , { recursive : true } )
174- await fs . rename ( absolutePath , newAbsolutePath )
175- debug ( `Renamed ${ rawRelativePath } -> ${ relativePath } ` )
176- effectiveAbsolutePath = newAbsolutePath
177-
178- // Suppress the echo events chokidar will fire for this rename
179- recentSanitizations . add ( rawRelativePath ) // upcoming unlink echo
180- recentSanitizations . add ( relativePath ) // upcoming add echo
181- setTimeout ( ( ) => {
182- recentSanitizations . delete ( rawRelativePath )
183- recentSanitizations . delete ( relativePath )
184- } , RENAME_BUFFER_MS * 3 )
185- } catch ( err ) {
186- warn ( `Failed to rename ${ rawRelativePath } ` , err )
187- return { relativePath : rawRelativePath , effectiveAbsolutePath : absolutePath }
169+ if ( kind === "add" ) {
170+ const sanitized = sanitizeFilePath ( rawRelativePath , false )
171+ if ( sanitized . path !== rawRelativePath ) {
172+ const nextRelativePath = sanitized . path
173+ const newAbsolutePath = path . join ( filesDir , nextRelativePath )
174+ try {
175+ await fs . mkdir ( path . dirname ( newAbsolutePath ) , { recursive : true } )
176+ await fs . rename ( absolutePath , newAbsolutePath )
177+ debug ( `Renamed ${ rawRelativePath } -> ${ nextRelativePath } ` )
178+ relativePath = nextRelativePath
179+ effectiveAbsolutePath = newAbsolutePath
180+
181+ // Suppress the echo events chokidar will fire for this rename
182+ recentSanitizations . add ( rawRelativePath ) // upcoming unlink echo
183+ recentSanitizations . add ( nextRelativePath ) // upcoming add echo
184+ setTimeout ( ( ) => {
185+ recentSanitizations . delete ( rawRelativePath )
186+ recentSanitizations . delete ( nextRelativePath )
187+ } , RENAME_BUFFER_MS * 3 )
188+ } catch ( err ) {
189+ warn ( `Failed to rename ${ rawRelativePath } ` , err )
190+ return { relativePath : rawRelativePath , effectiveAbsolutePath : absolutePath }
191+ }
188192 }
189193 }
190194
0 commit comments