Make generated files // swift-format-ignore
Description
I have generated files & swift-format. I don't want the formatter to reformat the generated code though. So it'd be awesome if it could generate a line with // swift-format-ignore which will disable formatting for that file.
Reproduction
N/a
Package version(s)
All
Expected behavior
No formatting
Environment
All
Additional information
No response
Sounds good.
Are we happy for it to be behind a config option for those that do want to include the generated files in their format?
This is probably the better solution although we could have something in the meanwhile.
We use .swiftformatignore in different projects for now, which has turned out to be fine. The formatting script in .github-workflows repo has been configured to use .swiftformatignore. swift-format itself doesn't really care about it.
I agree that swift-format would benefit from ignore support in its own config file. You’re right that the reusable GitHub workflows have support for this already, but I don’t think we can expect that all adopters use these. @weissi presumably you don’t use the reusable script since you filed the issue?
I agree that swift-format would benefit from ignore support in its own config file. You’re right that the reusable GitHub workflows have support for this already, but I don’t think we can expect that all adopters use these. @weissi presumably you don’t use the reusable script since you filed the issue?
Correct, not using those. I'm fine with a configuration option to turn on/off // swift-format-ignore comments in gen'd files.
That'd be fine with me, I'd just make sure it's not tied to swift-format.
Maybe a "top-level-comments" property, then users can specify "swift-format-ignore". + good docs.
@MahdiBM wrote:
That'd be fine with me, I'd just make sure it's not tied to swift-format.
Maybe a "top-level-comments" property, then users can specify "swift-format-ignore". + good docs.
I'm less inclined to make this super generalisable. Seems like an odd thing for folks to be inserting arbitrary things at the top of the generated file and, while I would be sympathetic to not wanting to over-index on one tool if we had an ecosystem of tens of linters, swift-format is now in the Swift toolchain so IMHO we could just add this unilaterally.
Sounds like @FranzBusch agrees (https://github.com/apple/swift-openapi-generator/issues/738#issuecomment-2694512139) and if Swift Protobuf is doing this, then I'm inclined for us to.
What do others think. I know I originally suggested we make this configurable but maybe we should just add the comment(s) unilaterally? Any objections?
@simonjbeaumont
swift-formatis now in the Swift toolchain so IMHO we could just add this unilaterally.
I wish swift-format was good enough so one can just use it and not worry about much else, but in its current form it has some pretty bad formatting behaviors so I'm not excited to get the whole ecosystem locked on it. It would have been fine by me if swift-format could actually do its job properly.
See this about possibility of support for other formatters in sourcekit-lsp.
If we're really against just hardcoding it, then I guess an additionalFileComments: could live alongside the additionalImports: config file option. Probably a list of strings, each of which becomes its own line comment.
additionalFileComments:
- "swift-format-ignore-file"
- "swiftlint:disable all"
- "mumble mumble mumble"
// swift-format-ignore-file
// swiftlint:disable all
// mumble mumble mumble
We haven't had many requests for it, so as long as there's a way to it, for those that want it, I'm pretty flexible on how.