Add options to mix format to allow easy excluding of files #14702
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently when configuring the formatter there is only the ability to
include
and notexclude
files from the match. This leads to some interesting situations depending on project setup.The only way to exclude files is to actually pattern match the files yourself inside the
formatter.exs
file so that you can then exclude specific files you care about, returning the modified list for the mix formatter code to work with.Things start getting more complicated when you start talking Umbrella apps, or just subdirectories (as defined in formatter.exs) in general. If you have a subdirectory, running
mix format
from the root does NOT change the CWD you are in. So thatformatter.exs
file sitting in anapps/awesome_umbrella_sub_app_1
that tries to find its files usingPath.wildcard
might find surprising behavior depending on wheremix format
was run, whether it was from the umbrella root or the application subfolder.In short, if you want to exclude a file from within an umbrella sub app you have to do something like this:
That will check if you're running the formatter from the umbrella root or the application subdirectory, find the files based off of the correct root directory, allow you to define relative paths for files you want to exclude, and then return a final list of cared about files minus the excludes.
This...is a lot of work just to exclude a single file. And you have to be aware that the formatter file might get run from an entirely different working directory than the one it is defined in which is definitely not intuitive.
I think there is a far, far easier way. Just allow us to define an
excludes
option for every formatter. Any files that match from theexcludes
option are removed from the results of theinputs
config. By using the exact same matching code for bothinputs
andexcludes
the above formatter becomes dead simple:I tried actually running the test I wrote but I ran into the following error inside the
mix
folder and don't have the bandwidth to figure it out: