Commit 870b4c5
committed
BUG/MINOR: prevent panic and avoid unnecessary file operations
The `WriteOnDiskIfChanged` method eagerly created files and deferred `f.Close()` before confirming if there were changes to write. If the file already existed, `f` remained `nil`, leading to a panic when the function returned without changes. It also redundantly recreated the file later in the flow.
This commit moves directory and file creation after the `hasDiff` check, ensuring file operations only occur when necessary. It also correctly scopes `defer f.Close()` immediately after the actual file creation to prevent nil pointer panics and optimize performance.1 parent 900b5bc commit 870b4c5
1 file changed
Lines changed: 13 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
446 | 446 | | |
447 | 447 | | |
448 | 448 | | |
449 | | - | |
450 | | - | |
451 | 449 | | |
452 | | - | |
453 | | - | |
454 | | - | |
| 450 | + | |
| 451 | + | |
455 | 452 | | |
456 | 453 | | |
457 | 454 | | |
458 | | - | |
459 | | - | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
460 | 461 | | |
461 | 462 | | |
462 | 463 | | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
463 | 467 | | |
464 | | - | |
465 | 468 | | |
466 | 469 | | |
467 | 470 | | |
| |||
482 | 485 | | |
483 | 486 | | |
484 | 487 | | |
485 | | - | |
| 488 | + | |
486 | 489 | | |
487 | 490 | | |
488 | 491 | | |
| 492 | + | |
489 | 493 | | |
490 | 494 | | |
491 | 495 | | |
| |||
0 commit comments