Skip to content

mtime is not preserved after rewrite #23

Description

@dolph

Summary

After rewriteFile renames the temp file over the target, the modification time becomes "now". For files whose contents didn't actually change (e.g., find string occurs but no replacement happens — handled — or replacement happens but produces identical bytes — possible with overlapping find/replace) we already skip the rename. But for files that DO change, the mtime jumps forward.

This invalidates Make-style timestamp dependency tracking, file-watcher caches, and various reproducible-build setups that rely on mtime stability. Some users explicitly run find-replace as part of code generation pipelines that depend on stable mtimes.

Impact (Reliability: Low)

  • Surprises some workflows.
  • git status is unaffected (git uses content), so most users won't notice.

Suggested Fix

After the temp file is fully written but before the rename, os.Chtimes(tmpName, info.ModTime(), info.ModTime()) (atime is harder to preserve and rarely matters; could use the original atime too).

This is consistent with what sed -i and perl -i users expect.

Files

  • file_handling.go:60-149 (rewriteFile)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions