VSCode formatting works in one project, does not work in another

I have one project (published here) that formats just fine in VSCode using the formatting built into the ocamllabs.ocaml-platform extension.

I have another project (published here) that does not format at all using the same workflow. I can examine the text sent back from the language server upon executing the format command, and it is simply the same text with no formatting applied. It’s as minimal of a reproduction as I could make. It includes the simple code:

type t = This | Should | Format

let () = print_string "hey"

and an .ocamlformat file that has profile=janestreet. Running ocamlformat manually on the file produces:

type t =
  | This
  | Should
  | Format

let () = print_string "hey"

But the response that the language server gives on the formatting request is:

[Trace - 11:33:23 PM] Received response 'textDocument/formatting - (285)' in 21ms.
Result: [
    .
    .
    .
    {
        "newText": "type t = This | Should | Format\n\nlet () = print_string \"hey\""
    }
]

Which does not include the expected newlines separating the type constructors for t.

I created the second project that is not formatting by copying the directory of the first project. So I have a hunch it’s something to do with the project itself, but I am very new to OCaml so I’m having a hard time debugging this. Any help or pointers would be very much appreciated!

Unfortunately, I can’t seem to reproduce your behavior on either Windows or WSL. Both projects format fine for me.

What version of ocamlformat are you using?

Thank you for trying! I am using ocamllsp 1.4.1 and ocamlformat 0.17.0. I’ve seen that the ocamllabs extension uses the formatting provided by the language server, so I think it could be something wrong with my language server installation? I checked the commands being executed by that extension and it’s simply starting the language server and then calling it’s formatting endpoint.

That’s correct. The language server calls ocamlformat every time VSCode asks to format.

I tried again with ocamllsp 1.4.1 and ocamlformat 0.17.0 and it stills continues to work. I’m not sure what the cause of your problem could be, but I would recommend trying to reinstall ocaml-lsp-server if you haven’t already.

Edit:

Are you using the same opam switch for both projects?

Just to be sure: did your copy include hidden files? I’m thinking about “.ocamlformat”.

Regards

So strange… thanks so much for your help with this. Yes as far as I can tell I am using the same switch for both projects. I am in the default switch on both shells in both of my VSCode windows, which I’ve reloaded, and I can see that the extension runs the language server with that switch when it starts up. I tried reinstalling ocaml-lsp-server in that switch as well and am still getting the same result. I’m not sure if it matters, but I am on version 1.8.1 of the ocamllabs.ocaml-platform extension, and I’ve double checked that I have .ocamlformat files in both projects :sweat_smile:

It seems so weird to me that the textDocument/formatting response from the language server is just returning the same unformatted text… does that indicate anything? Is there some way I could see more log output from the server if I ran it manually?

Edit:

By the way, I am on macOS big sur (which has been the root of many evils).

Yes at first I did miss that, but I’ve since verified that both projects have that!

Adding some more to this issue. The project that correctly formats has multiple modules and they all load just fine and no issues are reported as long as the project compiles. The project that cannot format, however, can only have a single module without the language server incorrectly reporting “Unbound module” errors. I’ve updated the example project listed in my original post to include another module that I’m running into this with.

dune build runs just fine, but the language server thinks the Formula module opened in Sat is unbound. I’ve tried obliterating my switches and reinstalling/updating everything to latest versions and still the issue persists. I’ll update this thread if I find any other issues or a solution!

After all this time… I’m ashamed to admit that this issue appears to have been because the path to my project had a = character in it. As soon as I remove that character and change the path name, everything works great.

I know I’ve seen some warnings about spaces in path names in the documentation, but I did not know other characters would be invalid as well. I wish it wouldn’t have failed so quietly because of this…

1 Like