@@ -196,7 +196,9 @@ func (r CollectorResult) ReplaceResult(bundlePath string, relativePath string, r
196196 if runtime .GOOS == "windows" {
197197 // Windows-only: Use destination directory to avoid antivirus issues
198198 destDir := filepath .Dir (filepath .Join (bundlePath , relativePath ))
199- os .MkdirAll (destDir , 0755 )
199+ if err := os .MkdirAll (destDir , 0755 ); err != nil {
200+ return errors .Wrap (err , "failed to create destination directory" )
201+ }
200202 tmpFile , err = os .CreateTemp (destDir , "replace-" )
201203 } else {
202204 // Linux/macOS: EXACT original behavior - system temp
@@ -223,14 +225,14 @@ func (r CollectorResult) ReplaceResult(bundlePath string, relativePath string, r
223225 // Delete target file first (Windows requirement)
224226 os .Remove (finalPath )
225227
226- // Windows: Use copy+delete instead of rename (more reliable)
228+ // Use copy+delete instead of rename (more reliable on Windows )
227229 err = copyFileWindows (tmpFile .Name (), finalPath )
228230 } else {
229231 // Linux/macOS: EXACT original behavior - DO NOT CHANGE
230232 err = os .Rename (tmpFile .Name (), filepath .Join (bundlePath , relativePath ))
231233 }
232234 if err != nil {
233- return errors .Wrap (err , "failed to rename tmp file" )
235+ return errors .Wrap (err , "failed to replace file" )
234236 }
235237
236238 return nil
@@ -442,27 +444,30 @@ func TarSupportBundleDir(bundlePath string, input CollectorResult, outputFilenam
442444 return input .ArchiveBundle (bundlePath , outputFilename )
443445}
444446
445- // copyFileWindows performs copy+delete for Windows file operations
447+ // copyFileWindows performs simple copy+delete for Windows (no rename operations)
446448func copyFileWindows (src , dst string ) error {
447449 srcFile , err := os .Open (src )
448450 if err != nil {
449451 return err
450452 }
451- defer srcFile .Close ()
452453
453454 dstFile , err := os .Create (dst )
454455 if err != nil {
456+ srcFile .Close ()
455457 return err
456458 }
457- defer dstFile .Close ()
458459
460+ // Copy data
459461 _ , err = io .Copy (dstFile , srcFile )
462+
463+ // Explicitly close files before removing source
464+ srcFile .Close ()
465+ dstFile .Close ()
466+
460467 if err != nil {
461468 return err
462469 }
463470
464- dstFile .Close ()
465- srcFile .Close ()
466-
471+ // Now safe to remove source file (handles are closed)
467472 return os .Remove (src )
468473}
0 commit comments