question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run `elm-format` when file saved even when there are syntax errors

See original GitHub issue

Problem

elm-format isn’t executed when a file is saved, and I think this is because the plugin detects syntax errors (I am not confident in that, but that matches my experiments).

Motivation

Coming from Atom and elmjutsu, I am a bit frustrated with how many more syntax errors I get now compared to with elmjutsu.

From what I understand, elm-format has looser syntax requirements than IntelliJ, but fixes them when it is run. I show a few examples where elm-format fixes the syntax errors below. They already get fixed today when I run elm-format from the CLI or from IntelliJ, so it would only be a matter of enabling it on save.

I feel that this would be a huge improvement to the user’s experience, especially for newcomers to the language. With elmjutsu doing this this way, I never felt that the language was too strict on the syntax (like indentation for instance), but without elm-format covering for me, it feels more painful to write Elm.

Examples of fixed syntax errors

(Would fix the error reported in https://github.com/klazuka/intellij-elm/issues/443 if the users enabled elm-format on save)

-- Doesn't compile
type alias A =
{}
-- After elm-format, compiles
type alias A =
    {}

(Would fix the error reported in https://github.com/klazuka/intellij-elm/issues/22 if the users enabled elm-format on save)

-- Doesn't compile
foo =
    a (\x ->
        case x of
            [] -> 0
            _ -> 1)
-- After elm-format, compiles
foo =
    a
        (\x ->
            case x of
                [] ->
                    0

                _ ->
                    1
        )
-- Doesn't compile
type alias A =
    { a = Int }
-- After elm-format, compiles
type alias A =
    { a : Int }
-- Doesn't compile
value =
    { a : 1 }
-- After elm-format, compiles
value =
    { a = 1 }
-- Doesn't compile
func a =
    let
b = 1
    in
    a
-- After elm-format, compiles
func a =
    let
        b =
            1
    in
    a

Thanks

I will use this opportunity to thank you for the hard work you have put into this project. It is really a splendid tool ❤️

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lydellcommented, May 2, 2020

I recently learned on the Elm Town podcast that elm-format can fix type alias Foo = { foo = Int } into type alias Foo = { foo : Int }. When trying this feature out, I was confused because I tried it via format-on-save in IntelliJ. Now I understand why it didn’t work! (Running format via keyboard shortcut works, fortunately.)

I’d also like it if elm-format was (attempted to be) run on save even if there are syntax errors.

1reaction
jfmengelscommented, Mar 12, 2020

Oops, I’m sorry, it was not elmjutsu but the elm-format Atom plugin.

Anyway, this is the result when you have a syntax error: A (not very helpful) notification in the top-right corner Screenshot from 2020-03-12 19-06-05

I have checked, and elm-syntax exits with status code 0 when there are syntax errors that it can (and does) fix. I have also looked at the code, and here is the section that deals with when elm-format exits with an error code. https://github.com/humio/atom-elm-format/blob/master/src/index.js#L100-L128

So basically, they strip the ANSI codes and whatever information that is not really relevant, find the line number in order to create something actionable like “Jump to Syntax Error” (which doesn’t really work for me).

The reason why I was confused about atom-elm-format/elmjutsu is that after that, I then see this (more helpful) compiler error message, that elmjutsu creates because the compiler returned with an exit code.

Screenshot from 2020-03-12 18-47-49

Summary:

  • atom-elm-format tries to run elm-format
    • If it succeeds, it fixes the file (even)
    • It it fails, it creates an error notification just to get the line number
  • elmjutsu comes in and runs the compiler, which gives me a useful error message

I think that in the case of this plugin, you can run elm-format on every save, and if it exits with status code 1, you pretty much ignore the error message, and use the current behavior to indicate there is a syntax error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A different elm-format crash · Issue #62
When I save an .elm file, formatting isn't happening, and in the console I get the following error: Sublime Text 3 elm 0.19...
Read more >
"IntelliJ Elm now runs elm-format even when ...
IntelliJ Elm now runs elm-format even when there are syntax errors. Here are syntax errors that elm-format fixes for you. Many thanks to....
Read more >
elm-format | Yarn - Package Manager
Detailed error messages for syntax errors have been removed. Use elm make to see a detailed error if elm-format fails. New features: In...
Read more >
Elm VSCode plugin does not format on save
Install elm-format. Install the extension Elm Language Support for VSCode, which includes syntax and error highlighting.
Read more >
elm - Visual Studio Marketplace
Syntax highlighting · Autocomplete (for external packages and experimental for local project) · Error highlighting · Hover info · Document Symbol ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found