nix-community / vscode-nix-ide

Nix language support for VSCode editor [maintainer: @jnoortheen]
https://marketplace.visualstudio.com/items?itemName=jnoortheen.nix-ide
MIT License
306 stars 23 forks source link

Formatting with nix fmt deletes content of file #413

Closed lriesebos closed 2 months ago

lriesebos commented 3 months ago

First, thanks for making this extension. Very helpful for all my Nix work!

Recently I switched my nix.formatterPath to use nix fmt, but that seems to cause issues sometimes. To start, the readme suggests to use "nix.formatterPath": ["nix", "fmt", "--", "-"], but that seems odd to me. The last "-" does not seem to be valid and also is odd as part of the command. "nix.formatterPath": ["nix", "fmt", "--"] makes more sense to me, and that seems to work. Now the weird thing is that if I make a formatting mistake on purpose, formatting fixes it correctly. When I then do formatting again on the correct file, the contents of the file are deleted. I do not see any error messages appear in vscode, but maybe there is some hidden spot I am not looking at.

Oh almost forgot to mention, for this project I am using formatter.x86_64-linux = pkgs.nixpkgs-fmt; and for another pkgs.nixfmt-rfc-style. Both seem to show the same behavior.

Any ideas?

lriesebos commented 3 months ago

Actually, this behavior might not be caused by the file to be correctly formatted. Sometimes I can also reproduce it by applying it on a file with a formatting mistake.

unlsycn commented 2 months ago

Nix IDE seems to read the output from the stdout hence you should make it print the content of the file. Use this command as your formatter to resolve it:

[
     "nix",
     "fmt",
      "--",
      "--output-format",
       "rnix"
]
lriesebos commented 2 months ago

Nix IDE seems to read the output from the stdout since you should make it print the content of the file. Use this command as your formattor to resolve it:

[
     "nix",
     "fmt",
      "--",
      "--output-format",
       "rnix"
]

@unlsycn thanks for the response. I tried it, but I still see the same behavior. also I am wondering of those arguments are valid for other formatters too.

unlsycn commented 2 months ago

Nix IDE seems to read the output from the stdout since you should make it print the content of the file. Use this command as your formatter to resolve it:

[
     "nix",
     "fmt",
      "--",
      "--output-format",
       "rnix"
]

@unlsycn thanks for the response. I tried it, but I still see the same behavior. also I am wondering of those arguments are valid for other formatters too.

I just use it for nixpkgs-fmt, and other formatters may have their own argument to output the content. BTW I use nixd to call the formatter.

lriesebos commented 2 months ago

ok, I don't use nixd with the plugin (server path is set to nil, but I don't think I have a nix language server installed at all). I would expect it would work fine without (which it also sometimes does as you can read above)?

I have been playing around with nixpkgs-fmt, nixfmt, and nix fmt from the command line using stdin to read content, and that all seems to work fine. the first two directly read from stdin if no file path is given, and the latter does it when using nix fmt -- - (as you describe) or nix fmt -- -- (as suggested here). so that all seems to be working fine. repeated calls always produce the desired output. the only thing that makes the commands different is that nix fmt first evaluates the flake before running the selected formatter. that causes a [0/1 built] text to show up on the terminal for a second before stdin is accepted and the output is generated. I am wondering if that somehow causes the nix-ide plugin to get confused and not always pick up the output of the command correctly?

lriesebos commented 2 months ago

ok I am pretty confused at the moment, but I have nix fmt -- -- configured right now and that seems to work without issues for nixpkgs-fmt and nixfmt-rfc-style. while nix fmt -- - does not work with nixpkgs-fmt for some reason. earlier I might also have been confused by the fact that vscode needs to be restarted for these formatter changes to apply. anyway, some docs updates might be worth considering. I will make a PR.