elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Formatter output changes on a second run when there are comments

Open mandar1045 opened this issue 1 month ago • 3 comments

Existing issue

  • [x] I have searched existing issues and could not find a duplicate.

Elixir and Erlang/OTP versions

Erlang/OTP 29 [erts-17.0.6] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit:ns]

Elixir 1.21.0-dev (d23e799) (compiled with Erlang/OTP 29)

Operating system

Arch Linux

Current behavior

On main, formatting some code with comments gives an output that changes again if you format it a second time, so mix format --check-formatted can fail right after mix format. 1.20.2 is fine.

The worst one:

foo(
  # c
  a ++ b
)

first becomes

foo(# c
a ++ b)

and then

# c
foo(a ++ b)

Same with a bitstring:

<<a::8, b::8,
  # c
  c::8>>

turns into <<a::8, b::8, # c and then moves the comment above it.

And with end of line comments, a blank line shows up on the first run and goes away on the second:

y =
  x |> f() |> g() # c

z
@spec f() ::
        {:ok, t} | :error # c

The first one becomes # c + x + empty line + |> f(), the spec one {:ok, t} + empty line + | :error.

I bisected it to #15893. I found these by adding comments to Elixir's own lib files and formatting twice: about 20 out of 20k came out different on the second run. No comments were lost, only moved.

Expected behavior

Formatting once gives the final output, like on 1.20.2.

mandar1045 avatar Oct 20 '26 16:10 mandar1045

@mandar1045 are you planning to send a PR for those too?

josevalim avatar Oct 20 '26 16:10 josevalim

@mandar1045 are you planning to send a PR for those too?

Yes I'm working on it , give me some time, I'll open the PR soon

mandar1045 avatar Oct 20 '26 16:10 mandar1045

No worries, just checking, thank you! :heart:

josevalim avatar Oct 20 '26 16:10 josevalim