Skip to content

10s delay under lefthook/pre-commit hooks — related to #524 #697

@mAbduqayum

Description

@mAbduqayum

Related to #524, but a different trigger.

#524 fixed the case where TERM=dumb caused a 10s hang. My TERM is normal (xterm-ghostty), but I still hit a 10s hang when treefmt runs from a git hook via lefthook.

What happens

Direct call: 23 ms. Same files via lefthook: 10 s. The 10s is reported by treefmt itself before any formatter runs.

$ time treefmt some.nix
formatted 0 files (0 changed) in 12ms        → 0.023s

$ time lefthook run pre-commit
formatted 0 files (0 changed) in 10.024s     → 10.044s

Why

Treefmt sends terminal color queries (OSC 10 / 11) on startup. Lefthook gives the child process a pty, so isTTY() returns true and the queries get sent — but there's no real terminal on the other end, so nothing answers. Treefmt waits the full OSC timeout (~10s).

#524's fix only short-circuits when TERM=dumb. With a normal TERM, we still run the probe and still hit the timeout.

Repro

lefthook.yml:

pre-commit:
  commands:
    format:
      glob: '*.nix'
      run: treefmt {staged_files}

treefmt.toml:

[formatter.nix]
command = 'nixfmt'
includes = ['*.nix']

Stage a .nix file, run lefthook run pre-commit.

Workarounds

  • TERM=dumb in the hook env → 20 ms. Works.
  • CI=1 → also works, but changes behaviour in other tools.
  • NO_COLOR=1 → does not help. The probe still runs.

Versions

  • treefmt v2.5.0
  • lefthook v2.1.5
  • NixOS, Linux 6.18

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions