9
votes

I have copied the following question from the Elm Slack channel for posterity.

I am having trouble getting the Elm VSCode plugin to format on save. I have set the absolute path to my global install of elm-format in settings.json. I have also set format on save for elm:

"[elm]": {
    "editor.formatOnSave": true,
  },

I am on an Ubuntu machine, and format does not happen on save or with Ctrl-Shift-I. Does anyone see something that I might be missing?

5

5 Answers

6
votes
  1. Install elm-format

  2. Install the extension Elm Language Support for VSCode, which includes syntax and error highlighting

  3. Configure VS code settings.json:

"[elm]": {
    "editor.formatOnSave": true
},
2
votes

The following are the responses and resolution.

Response from @stoft:

Try installing elm-format locally or globally with npm, that helped me on MacOS.

Response from @razzee:

please check two things:

  1. are you on the latest elm-format version
  2. increase the formatting timeout of vscode, unfortunately elm-format is sometimes too slow "editor.formatOnSaveTimeout" is what the setting is called"

A global install of the latest version of elm-format was enough for format on save to work.

One case where format on save does not work by following these suggestions is in the elm-0.19-workshop. Format on save will not trigger on Elm files in intro/part1/ or any of the other "part" directories.

2
votes

The format on save timeout should not be an issue with VSCode 1.42 (Q1 2020)
See "Better handling of slow save operations":

VS Code allows extensions to change a file's contents when saving files to disk.
Features like Format on Save and Fix on Save are examples.

However, saving is a critical operation and extensions doing processing during a save must finish quickly so that the actual save operation can proceed.

"Quickly" used to be enforced by VS Code with timeouts that would make VS Code cancel extension save participation.
That enforcement guaranteed speedy saving but could be frustrating because sometimes expected processing would not happen.

With this release, we have given up the timeout-based approach and instead show a progress notification that allows you to cancel extensions participating in save ("Save Participants").
The notification lets you know that save operations are running and you can decide if you want to wait for a save participant to finish or not.

https://media.githubusercontent.com/media/microsoft/vscode-docs/vnext/release-notes/images/1_42/save-participant.png

1
votes

Not setting the paths to elm-format and elm-tests explicitely worked for me.

I ran npm install --global elm-test elm-format and let the extension figure out where it is. As soon as I specify that /home/a-teammate/npm/lib/node_modules/elm-test/bin/elm-format is the elm-format I want to use, it stops working. Although pointing to the same elm-format.

Tested with elmtooling.elm-ls-vscode in version 0.9.4, elm 0.19.1 and elm-format 0.8.3

0
votes

I had this issue with Prettier formatter being installed as default. I resolved it by overriding formatter for Elm specifically:

  "[elm]": {
    "editor.defaultFormatter": "elmTooling.elm-ls-vscode"
  },