SwiftFormat: exclude option from config file is ignored when run for single file with githook

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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

@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)

@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.