avh4 / elm-format

elm-format formats Elm source code according to a standard set of rules based on the official Elm Style Guide
BSD 3-Clause "New" or "Revised" License
1.31k stars 145 forks source link

Detecting Elm's version fails #582

Open welblaud opened 5 years ago

welblaud commented 5 years ago

I have just installed Elm and elm-format together via NPM. The problem is elm is in ma PATH and responds correctly (version 0.19) but elm-format logs:


I couldn't figure out what version of Elm you are using!

You should either run elm-format from the directory containing
your elm.json (for Elm 0.19) or elm-package.json (for Elm 0.18),
or tell me which version of Elm with --elm-version=0.19 or --elm-version=0.18

Where could be the problem?

jwoudenberg commented 5 years ago

I ran into a similar issue running elm-format through a Vim plugin, in monorepo project.

I usually start Vim in the root of the project, which is not the root of the Elm code. Vim takes elm-format from the path and tries to run it on an Elm module when I save, but because the cwd is the root of the project elm-format gives the error message above and nothing gets formatted.

I'm currently using a wrapper script around elm-format that ensures it is always ran with the --elm-version=0.19 parameter in this project. That works fine, but it's something I'll need to set up for each project with Elm code separately.

I think the error message we get is clear, but wonder if elm-format can recognize the correct version of Elm code in a wider variety of cases. For example: would it be an option to walk the directory tree upwards from the to-be-formatted file until an elm.json or elm-package.json file is encountered?

avh4 commented 5 years ago

wonder if elm-format can recognize the correct version of Elm code in a wider variety of cases

See https://github.com/avh4/elm-format/issues/561

codefist commented 5 years ago

the easy and obvious fix here is to crawl UP the directory hierarchy until an elm.json file is found. I shouldn't have to keep an elm.json file at the root of my project just make this work. anyone opposed to that?! I'll take a swing at it unless someone else has a fix.