-
Notifications
You must be signed in to change notification settings - Fork 652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CI] Add format ignore file #2803
Conversation
# Motivation Some repositories may want to exclude files from being format checked. # Modification This PR moves the formatting into a separate script and adds an optional `.swiftformatignore` file that can be placed at the repo root similar to the `.licenseignore` file. # Result Repos can now decide what files are ignored when running the formatter.
Format check is expected to fail since I moved the inline scripts into a file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not loving .swiftformatignore
as a name, I think .swift-format-ignore
would be more readable, but approving either way :)
I was going back and forth. But decided for the former because of |
|
||
log "Running swift format lint..." | ||
|
||
git ls-files -z '*.swift' $excluded_files | xargs -0 swift format lint --strict --parallel |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quoting error here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are intentionally without quotes since I to split on spaces. Otherwise this exclude list doesn't work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not how you do that in shell (will break with spaces in the file names)... The correct syntax is
files=( foo 'foo bar' buz )
echo "${files[@]}"
will do what you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would have made more sense to use git ls-files -z ...
and then use grep -f
to make use of the exclude file?
Or git ls-files $(cat IGNOREFILE | xargs -I% printf ":(exclude)% ")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
git ls-files $(cat IGNOREFILE | xargs -I% printf ":(exclude)% ")
This has two quoting errors, one in bash and one with xargs. You may be able to leverage printf "...%q"
which outputs it quoted. But I think the right solution here is
# -X <file>, --exclude-from=<file>
# Read exclude patterns from <file>; 1 per line.
git ls-files -X IGNOREFILE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, doesn't work as advertised :) Only applies to files in the index, which is a bit of a bummer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤯 infuriating!
|
||
excluded_files="" | ||
|
||
if [ -f .swiftformatignore ]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prefer [[
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding was that [[
isn't portable so I preferred [
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an incorrect understanding. You're writing bash
(as you should and as indicated by your shebang) not "POSIX shell".
Once you have bash
, [[
is a built-in which means absolute maximum compatibility. If you type [
you're invoking the program called /bin/[
:
$ ls -la /bin/[
-rwxr-xr-x 1 root wheel 101456 12 Jul 21:18 /bin/[
##===----------------------------------------------------------------------===## | ||
set -euo pipefail | ||
|
||
log() { printf -- "** %s\n" "$*" >&2; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we run these scripts through shellcheck
? Quoting errors etc are avoidable and can be checked by tooling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am running a shell check locally already but yes we should setup a GH action for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shell check would've spotted the above quoting error, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed
$ echo -e 'files="foo bar"\necho $files' | shellcheck --shell=bash -
In - line 2:
echo $files
^----^ SC2086 (info): Double quote to prevent globbing and word splitting.
Did you mean:
echo "$files"
For more information:
https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shell check found two quoting warnings not errors for line 29 and line 33 https://www.shellcheck.net/wiki/SC2086.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capitalised variables are bad as they have unknown, unintended side effects. The space of lower case vars is reserved (by POSIX) for app code.
Where we do want side effects (like with GIT_PAGER=....
) they're great. That's exactly how they're meant to be used:
- UPPER_CASE_STUFF -> side effects in other programs
- lower_case_stuff -> my everyday variables that I do not want to go to other programs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not arguing with you here. I have long used uppercase variables in my bash scripts, but I have heard your arguments against them and plan to incorporate them in the future.
FWIW, I don't think the side-effect argument is actually true, unless the variable is exported
, but I'm still planning on stopping this practice in my own scripts because it helps with local reasoning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, for some reason this isn't widely known. But I think it's well documented:
- stackoverflow: https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization
- posix: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
And yes, the export
ed thing is correct (unless you prefix a command/script) but these things happen by accident. And as you point out, it's just hard to reason about it. If you assign to a variable called IFS
I don't know if you're meaning to overwrite IFS
for say read
or if you want a variable called IFS
. If you name it ifs=123
on the other hand I know what you mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also use local
variables more, I try to do that everywhere except when needing to export vars.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I readily accept that I have fallen into the trap of shouty-case for all my scripting variables. I'm not alone, I know. But I always knew when they were and weren't exported so they didn't affect behaviour, and I've learned to not name my variables to clash with ones that I'll need to affect behaviour. But, like I've already said, I've started to lowercase—we can all grow.
|
||
log "Checking for modified files..." | ||
|
||
GIT_PAGER= git diff --exit-code '*.swift' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the space here is also incorrect, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I set the GIT_PAGER
to null intentionally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gotcha. Might be nice to be explicit here: GIT_PAGER='' git diff ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very idiomatic shell already.
If we're gonna make it explicit, I'd rather env -u GIT_PAGER git diff ...
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO that's obfuscation. I'd suggest to ask a few people what happens if you do
FOO= hello world
vs.
FOO='' hello world
In my mind, the first one is confusing, it might be an assignment of hello world
to FOO=
or it might be starting hello
with parameter world
and env var FOO=''
.
The second one is obvious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean the use of env
is obfuscation? If anything it's clearer and has different semantics. It unsets the variable, as opposed to setting it to the empty string.
I concede that for setting a variable to the empty string, FOO=''
is clearer than FOO=
.
I don't think it's quite as much of an alarm bell as you make out, and shellcheck
doesn't complain, but I'm happy for it to change if it irks you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was referring to env
. If this is common knowledge that env -u FOO hello world
runs hello world
, then I'm completely happy with that of course. As am I with
unset FOO
hello world
or
FOO='' hello world
because I think they convey the meaning pretty well.
But out of all the issues, this really is the most minuscule one. Feel free to leave GIT_PAGER= git diff
, it just reads a little off in my head at least.
This PR contains the following updates: | Package | Update | Change | |---|---|---| | [apple/swift-nio](https://togithub.com/apple/swift-nio) | minor | `2.68.0` -> `2.70.0` | --- ### Release Notes <details> <summary>apple/swift-nio (apple/swift-nio)</summary> ### [`v2.70.0`](https://togithub.com/apple/swift-nio/releases/tag/2.70.0): SwiftNIO 2.70.0 [Compare Source](https://togithub.com/apple/swift-nio/compare/2.69.0...2.70.0) <!-- Release notes generated using configuration in .github/release.yml at main --> #### What's Changed ##### SemVer Minor - `FileSystem.copyItem` can parallelise directory copy by [@​UncleMattHope](https://togithub.com/UncleMattHope) in [https://github.com/apple/swift-nio/pull/2806](https://togithub.com/apple/swift-nio/pull/2806) - `ChannelOption`: Allow types to be accessed with leading dot syntax by [@​ayush1794](https://togithub.com/ayush1794) in [https://github.com/apple/swift-nio/pull/2816](https://togithub.com/apple/swift-nio/pull/2816) - Make `EventLoopPromise` conform to Equatable by [@​gjcairo](https://togithub.com/gjcairo) in [https://github.com/apple/swift-nio/pull/2714](https://togithub.com/apple/swift-nio/pull/2714) - Provide a default `CopyStrategy` overload for copyItem. by [@​UncleMattHope](https://togithub.com/UncleMattHope) in [https://github.com/apple/swift-nio/pull/2818](https://togithub.com/apple/swift-nio/pull/2818) ##### SemVer Patch - Better align shutdown semantics of testing event loops by [@​simonjbeaumont](https://togithub.com/simonjbeaumont) in [https://github.com/apple/swift-nio/pull/2800](https://togithub.com/apple/swift-nio/pull/2800) - Clone files on Darwin rather than copying them by [@​rnro](https://togithub.com/rnro) in [https://github.com/apple/swift-nio/pull/2823](https://togithub.com/apple/swift-nio/pull/2823) ##### Other Changes - Fix compose file used in update-benchmark-thresholds script by [@​simonjbeaumont](https://togithub.com/simonjbeaumont) in [https://github.com/apple/swift-nio/pull/2808](https://togithub.com/apple/swift-nio/pull/2808) - Remove advice to generate linux tests. by [@​PeterAdams-A](https://togithub.com/PeterAdams-A) in [https://github.com/apple/swift-nio/pull/2807](https://togithub.com/apple/swift-nio/pull/2807) - Make `testInstantTCPConnectionResetThrowsError` more reliable by [@​hamzahrmalik](https://togithub.com/hamzahrmalik) in [https://github.com/apple/swift-nio/pull/2810](https://togithub.com/apple/swift-nio/pull/2810) - \[CI] Add `shellcheck` and fix up warnings by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2809](https://togithub.com/apple/swift-nio/pull/2809) - \[CI] Fix docs check by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2811](https://togithub.com/apple/swift-nio/pull/2811) - \[CI] Add Swift 6 language mode workflow by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2812](https://togithub.com/apple/swift-nio/pull/2812) - Fix test compilation on non-macOS Darwin platforms by [@​simonjbeaumont](https://togithub.com/simonjbeaumont) in [https://github.com/apple/swift-nio/pull/2817](https://togithub.com/apple/swift-nio/pull/2817) - Add `.index-build` to `.gitignore` by [@​MaxDesiatov](https://togithub.com/MaxDesiatov) in [https://github.com/apple/swift-nio/pull/2819](https://togithub.com/apple/swift-nio/pull/2819) - \[CI] Add action and workflow to check for semver label by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2814](https://togithub.com/apple/swift-nio/pull/2814) - Update repository docs for swift-version support and recent CI check changes by [@​UncleMattHope](https://togithub.com/UncleMattHope) in [https://github.com/apple/swift-nio/pull/2815](https://togithub.com/apple/swift-nio/pull/2815) - Fix failing build for test by [@​ayush1794](https://togithub.com/ayush1794) in [https://github.com/apple/swift-nio/pull/2824](https://togithub.com/apple/swift-nio/pull/2824) - Fix typo in comment in `WebSocketErrorCodes.swift` by [@​valeriyvan](https://togithub.com/valeriyvan) in [https://github.com/apple/swift-nio/pull/2604](https://togithub.com/apple/swift-nio/pull/2604) - \[CI] Add a scheduled workflow for tests and benchmarks by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2822](https://togithub.com/apple/swift-nio/pull/2822) - \[CI] Fix label check by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2827](https://togithub.com/apple/swift-nio/pull/2827) #### New Contributors - [@​UncleMattHope](https://togithub.com/UncleMattHope) made their first contribution in [https://github.com/apple/swift-nio/pull/2806](https://togithub.com/apple/swift-nio/pull/2806) - [@​ayush1794](https://togithub.com/ayush1794) made their first contribution in [https://github.com/apple/swift-nio/pull/2816](https://togithub.com/apple/swift-nio/pull/2816) - [@​valeriyvan](https://togithub.com/valeriyvan) made their first contribution in [https://github.com/apple/swift-nio/pull/2604](https://togithub.com/apple/swift-nio/pull/2604) **Full Changelog**: apple/swift-nio@2.69.0...2.70.0 ### [`v2.69.0`](https://togithub.com/apple/swift-nio/releases/tag/2.69.0): SwiftNIO 2.69.0 [Compare Source](https://togithub.com/apple/swift-nio/compare/2.68.0...2.69.0) <!-- Release notes generated using configuration in .github/release.yml at main --> #### What's Changed ##### SemVer Minor - Add manual control to `NIOLockedValueBox` by [@​glbrntt](https://togithub.com/glbrntt) in [https://github.com/apple/swift-nio/pull/2786](https://togithub.com/apple/swift-nio/pull/2786) - ChannelHandler: provide static `(un)wrap(In|Out)bound(In|Out)` by [@​weissi](https://togithub.com/weissi) in [https://github.com/apple/swift-nio/pull/2791](https://togithub.com/apple/swift-nio/pull/2791) ##### SemVer Patch - Pre-box some errors to reduce allocations by [@​glbrntt](https://togithub.com/glbrntt) in [https://github.com/apple/swift-nio/pull/2765](https://togithub.com/apple/swift-nio/pull/2765) - Allow in-place mutation of `NIOLoopBoundBox.value` by [@​dnadoba](https://togithub.com/dnadoba) in [https://github.com/apple/swift-nio/pull/2771](https://togithub.com/apple/swift-nio/pull/2771) - Avoid creating a yield ID counter per async writer by [@​glbrntt](https://togithub.com/glbrntt) in [https://github.com/apple/swift-nio/pull/2768](https://togithub.com/apple/swift-nio/pull/2768) - Combine the two `NIOAsyncChannel` channel handlers by [@​glbrntt](https://togithub.com/glbrntt) in [https://github.com/apple/swift-nio/pull/2779](https://togithub.com/apple/swift-nio/pull/2779) - Use the new Android overlay and Bionic module from Swift 6 by [@​finagolfin](https://togithub.com/finagolfin) in [https://github.com/apple/swift-nio/pull/2784](https://togithub.com/apple/swift-nio/pull/2784) - Change `unsafeDownCast` to `as!` by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2802](https://togithub.com/apple/swift-nio/pull/2802) ##### Other Changes - CI migration to GitHub Action by [@​FranzBusch](https://togithub.com/FranzBusch) in ([https://github.com/apple/swift-nio/pull/2760](https://togithub.com/apple/swift-nio/pull/2760) [https://github.com/apple/swift-nio/pull/2762](https://togithub.com/apple/swift-nio/pull/2762) [https://github.com/apple/swift-nio/pull/2763](https://togithub.com/apple/swift-nio/pull/2763) [https://github.com/apple/swift-nio/pull/2764](https://togithub.com/apple/swift-nio/pull/2764) [https://github.com/apple/swift-nio/pull/2767](https://togithub.com/apple/swift-nio/pull/2767) [https://github.com/apple/swift-nio/pull/2766](https://togithub.com/apple/swift-nio/pull/2766) [https://github.com/apple/swift-nio/pull/2776](https://togithub.com/apple/swift-nio/pull/2776) [https://github.com/apple/swift-nio/pull/2780](https://togithub.com/apple/swift-nio/pull/2780) [https://github.com/apple/swift-nio/pull/2785](https://togithub.com/apple/swift-nio/pull/2785) [https://github.com/apple/swift-nio/pull/2781](https://togithub.com/apple/swift-nio/pull/2781) [https://github.com/apple/swift-nio/pull/2787](https://togithub.com/apple/swift-nio/pull/2787) [https://github.com/apple/swift-nio/pull/2783](https://togithub.com/apple/swift-nio/pull/2783) [https://github.com/apple/swift-nio/pull/2789](https://togithub.com/apple/swift-nio/pull/2789) [https://github.com/apple/swift-nio/pull/2790](https://togithub.com/apple/swift-nio/pull/2790)) - Ignore format commit from git blame by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2796](https://togithub.com/apple/swift-nio/pull/2796) [https://github.com/apple/swift-nio/pull/2797](https://togithub.com/apple/swift-nio/pull/2797) [https://github.com/apple/swift-nio/pull/2801](https://togithub.com/apple/swift-nio/pull/2801) [https://github.com/apple/swift-nio/pull/2803](https://togithub.com/apple/swift-nio/pull/2803) - Adopt swift-format by [@​FranzBusch](https://togithub.com/FranzBusch) in [https://github.com/apple/swift-nio/pull/2794](https://togithub.com/apple/swift-nio/pull/2794) - `HTTPPart` Documentation Clarification by [@​dimitribouniol](https://togithub.com/dimitribouniol) in [https://github.com/apple/swift-nio/pull/2775](https://togithub.com/apple/swift-nio/pull/2775) - Add benchmark for creating `NIOAsyncChannel` by [@​glbrntt](https://togithub.com/glbrntt) in [https://github.com/apple/swift-nio/pull/2774](https://togithub.com/apple/swift-nio/pull/2774) - Disable warnings as errors on Swift 6 and main by [@​glbrntt](https://togithub.com/glbrntt) in [https://github.com/apple/swift-nio/pull/2793](https://togithub.com/apple/swift-nio/pull/2793) **Full Changelog**: apple/swift-nio@2.68.0...2.69.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://togithub.com/renovatebot/renovate/discussions) if that's undesired. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://togithub.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4xOC4xIiwidXBkYXRlZEluVmVyIjoiMzguMzkuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==--> Co-authored-by: cgrindel-self-hosted-renovate[bot] <139595543+cgrindel-self-hosted-renovate[bot]@users.noreply.github.com>
Motivation
Some repositories may want to exclude files from being format checked.
Modification
This PR moves the formatting into a separate script and adds an optional
.swiftformatignore
file that can be placed at the repo root similar to the.licenseignore
file.Result
Repos can now decide what files are ignored when running the formatter.