Skip to content

Don't write a formatted file if the formatting didn't change the file #890

@wucke13

Description

@wucke13

Describe the bug
When I format a file which already is in proper format, it gets rewritten and the mtime changes. This is sub-optimal in multiple cases.

Example 1: I format all SQL files in a project automatically. They where already correctly formatted. I keep editing them in my editor, but as I want to save my changes, the editor now complains: "The file has been changed since the last write by us, do you want to overwrite or reload (discarding local changes)?". Its reasonable for the editor to ask this, but its a nuisance in the workflow.

Example 2: I use treefmt to format my projects, its a meta-formatter (it in term runs sql-formatter, taplo, prettier, ...). However, treefmt uses the combination of mtime and file size to quickly detect if a file was changed. For the CI mode of treefmt (just check if all files are properly formatted), the check fails if there is a changed file. As sql-formatter changes the mtime, the CI check always fails. The upstream issue for this one is numtide/treefmt-nix#400 , and there I got asked to open an issue here; I do however agree with them that changing mtime when the file was in fact not modified is a bug.

Expected behavior
mtime of formatted files only changes when they are truly changed, e.g. the diff between before/after formatting is not empty.

Actual behavior
mtime changes whenever sql-formatter is executed, regardless of whether modifications where made.

Usage

  • Using treefmt, configured with treefmt-nix, I format my *.sql files
  • I use this for sqlite only
  • SQL Formatter 15.6.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions