nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 640 forks source link

exclude option from config file is ignored when run for single file with githook #374

Closed NKorchagin closed 3 years ago

NKorchagin commented 5 years ago

That's the issue we faced recently.

We have configuration file with exclude option:

--exclude **/Generated

If you run swiftformat for root folder like that:

swiftformat . --config swiftformat.config

files in Generated folder will be ignored correctly.

But, if you run it for specific file, like we do in githook for all staged files:

swiftformat --config swiftformat.config "SomeFolder/Generated/SomeFile.swift"

This file will be formatted.

After investigation we found that issue was introduced in version 0.36.0. Version 0.35.10 will say error: no eligible files found at .../SomeFolder/Generated/SomeFile.swift but will not format this file.

@nicklockwood please, review that change, probably it's the reason https://github.com/nicklockwood/SwiftFormat/commit/10cafc4af83b596dbf7ee932d421d68b15006e5e

nicklockwood commented 5 years ago

@NKorchagin thanks for reporting this - I'll get it fixed asap.

nicklockwood commented 5 years ago

@NKorchagin fixed in 0.39.4

palle-k commented 4 years ago

Possible regression in 0.46.3: When I run swiftformat --config .swiftformat ./**/*.swift instead of swiftformat --config .swiftformat ./, my exclude list will be ignored.

nicklockwood commented 4 years ago

@palle-k using wildcards for input paths is not currently supported. I guess this works because the shell itself expands the path, but it's not something I ever tested. Are you sure it worked prior to 0.46.3?

palle-k commented 4 years ago

I haven't tested it with prior versions. But I have noticed that this error also occurs in the pre-commit hook (the one described in this repo with git-format-staged) and with single files.

nicklockwood commented 4 years ago

@palle-k ah OK, thanks. I'll take a look.

palle-k commented 4 years ago

Thanks. I was able to work around it for now by using exclude patterns directly with git-format-staged (e.g. "!Pods/*") but a solution directly in SwiftFormat would be ideal.

nicklockwood commented 4 years ago

@palle-k with git-format-staged are you using the --stdinpath argument? I recently updated the docs to recommend this, as it is required for exclusions to work correctly:

git-format-staged --formatter "swiftformat stdin --stdinpath '{}'" "*.swift"

(the reason for this is that git-format-staged passes files via stdin instead of passing the file path itself, so without the --stdinpath argument, swiftformat cannot know what the path of the input was, and whether to exclude it)

palle-k commented 4 years ago

Yes I am. My git hook currently looks like this (minus the git-format-staged exclude workaround):

#!/bin/bash
export PATH=/usr/local/bin:$PATH
git-format-staged --formatter "swiftformat stdin --stdinpath '{}' --config path/to/.swiftformat" "*.swift" 

My .swiftformat file contains the following:

--exclude Pods
# and some more commands

and the Pods directory is in the same directory as the .swiftformat file. And everything works as expected if I simply run swiftformat --config .swiftformat ./

nicklockwood commented 4 years ago

@nicklockwood can you try it without the --config path/to/.swiftformat? It should pick that up anyway if it's in the same directory.

palle-k commented 4 years ago

The Pods directory is getting formatted even without the --config argument. And according to the output of swiftformat, the .swiftformat file is picked up.

oonoo commented 3 years ago

This is not working with the current version 0.48.1. Is it possible to open this issue again? Or another way to make it visible this could need a fix? @nicklockwood

keithcml commented 3 years ago

@oonoo which version is workable do you know?

@nicklockwood any updates for this issues?

nicklockwood commented 3 years ago

@keithcml sorry, I forgot about this. AFAICT there's no way to bump reopened issues to the front page of the github issues, so I've opened a new issue for this. Please post any further comments there.

nicklockwood commented 3 years ago

@keithcml @oonoo @palle-k fixed in 0.48.3, sorry for the delay