Do not rewrite a proto file that -fix did not change - #565
Open
Eljees wants to merge 1 commit into
Open
Conversation
BaseFixing.Finally always ended in osutil.WriteExistingFile, which opens the file with O_TRUNC, so every -fix run rewrote every file it looked at and updated its modification time even when the content was byte-for-byte identical. Tools that read a changed mtime as "the formatter touched this file", such as treefmt, then report a clean file as modified. Keep the content read at construction time and skip the write when the fixed content is equal to it. Fixes yoheimuta#452
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #452
The problem
protolint lint -fixupdates the modification time of a file even when it changesnothing in it. Tools that read a changed mtime as "the formatter touched this file"
then report a clean tree as dirty — the reporter hit this with
treefmt-nix.Reproduced on master:
The cause
BaseFixing.Finallyalways ends inosutil.WriteExistingFile, and that opens thefile with
os.O_WRONLY|os.O_TRUNC, so the file is truncated and written backunconditionally.
BaseFixableVisitor.Finallycalls it once per fixable rule, so afile with nothing to fix is rewritten many times in a single run.
The change
Keep the bytes read in
NewBaseFixingand skip the write when the fixed content isequal to them. Files that do need fixing are written exactly as before.
Tests
linter/fixerhad no test file, so this adds one: the case above plus a controlthat a real edit is still written to disk.
mtime of an unmodified file changed: before ..., after ...go test -race ./...: no new failures.internal/linter/report/reporters(
TestEnvMatcherReporterFromUnallowedTemplateFile_Report) already fails on aclean master here and is unrelated to this change.
Checked on the CLI after the change: the clean file above keeps its 2020-01-01
timestamp, while a file with a real indentation problem is still fixed and written.