1
1
mirror of https://github.com/tweag/ormolu.git synced 2024-09-11 13:16:13 +03:00

Avoid modifying correctly-formatted files

Currently, when running with `--mode=inplace`, Ormolu always overwrites the
files, even when nothing has changed, updating the modified timestamp of the
file. This encourages Stack to rebuild everything when only one file has
changed.

A quick check to see if the file needs rewriting solves this problem.

I would prefer the API allowed me to only read the file once, but as the
`--mode=check` logic also reads the file twice, I decided this wasn't the
biggest sin for now.
This commit is contained in:
Samir Talwar 2020-09-06 12:01:33 +02:00 committed by Mark Karpov
parent 6a053acb10
commit e95ec9b83b
2 changed files with 15 additions and 6 deletions

View File

@ -1,3 +1,8 @@
## Unreleased
* Ormolu no longer overwrites already formatted files. [PR
649](https://github.com/tweag/ormolu/pull/649).
## Ormolu 0.1.2.0
* Fixed the bug when comments in different styles got glued together after

View File

@ -61,15 +61,19 @@ formatOne mode config = \case
-- 101 is different from all the other exit codes we already use.
exitWith (ExitFailure 101)
Just inputFile -> do
r <- ormoluFile config inputFile
originalInput <- TIO.readFile inputFile
formattedInput <- ormoluFile config inputFile
case mode of
Stdout ->
TIO.putStr r
InPlace ->
TIO.writeFile inputFile r
TIO.putStr formattedInput
InPlace -> do
-- Only write when the contents have changed, in order to avoid
-- updating the modified timestamp if the file was already correctly
-- formatted.
when (formattedInput /= originalInput) $
TIO.writeFile inputFile formattedInput
Check -> do
r' <- TIO.readFile inputFile
when (r /= r') $
when (formattedInput /= originalInput) $
-- 100 is different to all the other exit code that are emitted
-- either from an 'OrmoluException' or from 'error' and
-- 'notImplemented'.