Skip to content

fix: resolve the right formatter for each file - #880

Open
doorgan wants to merge 1 commit into
mainfrom
doorgan/formatter-resolver
Open

fix: resolve the right formatter for each file#880
doorgan wants to merge 1 commit into
mainfrom
doorgan/formatter-resolver

Conversation

@doorgan

@doorgan doorgan commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

#804 added a formatter cache, but it fails in the following scenarios:

Closing the first formatted file:

  1. Open file a.ex and format it
  2. Open file b.ex and format it
  3. Close file a.ex
  4. Try to format b.ex
  5. It fails with :no_formatter in the logs

This is because a.ex and b.ex will both point to the same entry in the cache. Removing a.ex will remove that path AND its entry, so the next time we request a formatter for b.ex, it will not find the old entry and the cache will return :no_formatter

This sharing of the formatter entry causes a second issue. Because they share the same entry and formatter function, any failure to format b.ex will point to a.ex, as that's the filename closed over by the formatter function in the entry cache:

  1. Open file a.ex and format it
  2. Open file b.ex, introduce a syntax error, and format it
  3. A formatter error will be logged(not notified to the user though), but it points to a.ex instead of b.ex

This PR fixes both issues by changing the cache to track one formatter function per path, ensuring no formatter function is shared, and that closing a document won't break formatting for other files.

Note: it is very annoying that the formatter works by creating a bespoke function for each file, and that the formatter plugins don't seem to inherit the formatter options if we override them to set the right :file for each path, so this is what we have for now.

@doorgan
doorgan force-pushed the doorgan/formatter-resolver branch from 84e0d00 to 3b8066b Compare September 3, 2026 05:33
@doorgan doorgan linked an issue Sep 3, 2026 that may be closed by this pull request
@doorgan
doorgan marked this pull request as ready for review September 3, 2026 05:43
@doorgan
doorgan requested a review from scohen September 3, 2026 05:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatter randomly stops working

1 participant