elm-format icon indicating copy to clipboard operation
elm-format copied to clipboard

Allow files to be excluded

Open yonigibbs opened this issue 5 years ago • 7 comments

Would it be possible for elm-format to allow files to be excluded, e.g. based on some glob or something? The use-case is for when you want to run elm-format --validate on the folder containing all your source, but there are some generated files in there that you want to exclude. Examples of generated files are files generated by elm-graphql or postcss-elm-tailwind. The reason for wanting to exclude them is because they are generated (so you have no control over whether they do/don't conform to the elm-format rules), and/or because they are very large and can slow down the validation process.

yonigibbs avatar Mar 03 '20 06:03 yonigibbs

I think you can currently do this with something like

find src -name '*.elm' | grep -v /TW.elm | xargs elm-format --validate

or without grep:

find src -name '*.elm' ! -name TW.elm | xargs elm-format --validate

avh4 avatar Mar 03 '20 08:03 avh4

Yup, I'm doing something similar already (but not as neatly as you suggest up there). So do you reckon this means there's no point adding an exclude flag or something directly to elm-format? (I'm happy to keep using the approach above.)

yonigibbs avatar Mar 03 '20 08:03 yonigibbs

You could also elm-format the generated files when you generate them :smile:

basile-henry avatar Mar 03 '20 08:03 basile-henry

I'd be interested to hear about cases where existing tools like find, xargs, etc aren't helpful enough. Implementing something in elm-format itself would start to get beyond its intended responsibilities, and would be a somewhat big project since it would involve deciding on an appropriate a globbing syntax, and it also then leads to the question of whether there should also be an "include" option and then how "exclude" and "include" should interact.

If there's big benefit to be had, I'd be open to considering. But I'd like to avoid recreating features for building up a list of files if it's possible to avoid it.

avh4 avatar Mar 03 '20 08:03 avh4

I agree. I think your suggested approach above meets the use cases I can think of, so this can probably be closed as far as I'm concerned. Thanks for the suggestion above!

yonigibbs avatar Mar 03 '20 08:03 yonigibbs

Looking at the formatter ecosystem rustfmt, yaph (for python), prettier, and possibly others support excluding files/directories with either a CLI option or a configuration file.

On the other hand gofmt and clang-format both seem not to provide this option.


I have come to this issue because I am writing an integration test the elm-in-elm compiler's handling of an invalid source file. Because the file is invalid the elm-format . --validate fails. Whilst I could play tricks with find and grep they would not be cross-platform and the scripts quickly get messy. On the other hand I could pass a list of directories I do wan't to be formatted to elm-format but as soon as I do this I introduce the risk that when new elm files (in new directories) are added to the project in the future the new directories will not be added to the list passed to elm-format and the new files will not be formatted.

Just my two cents!

harrysarson avatar Mar 15 '20 14:03 harrysarson

Funnily enough, I just hit this issue again because I'd used the suggested workaround from above (using xargs) and then came a cropper when trying to get the codebase working for a colleague on Windows. Obviously there are always ways around this, but it would certainly simplify things slightly if the tool natively provided the ability to exclude certain files/folders. Not a deal breaker by any stretch of the imagination, but a nice-to-have.

yonigibbs avatar Mar 25 '20 15:03 yonigibbs