Skip to content
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

Merged
merged 1 commit into from
Jul 24, 2024
Merged

[CI] Add format ignore file #2803

merged 1 commit into from
Jul 24, 2024

Conversation

FranzBusch
Copy link
Member

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.

# 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.
@FranzBusch
Copy link
Member Author

Format check is expected to fail since I moved the inline scripts into a file.

Copy link

@czechboy0 czechboy0 left a 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 :)

@FranzBusch
Copy link
Member Author

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 .gitignore and .licenseignore. I briefly thought about .swift-formatignore but that just looks even worse

@glbrntt glbrntt merged commit 541a900 into main Jul 24, 2024
25 of 26 checks passed
@glbrntt glbrntt deleted the fb-format-ignore-file branch July 24, 2024 13:58
@glbrntt glbrntt added the semver/none No version bump required. label Jul 24, 2024

log "Running swift format lint..."

git ls-files -z '*.swift' $excluded_files | xargs -0 swift format lint --strict --parallel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quoting error here

Copy link
Member Author

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

Copy link
Member

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

Copy link
Contributor

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)% ")

Copy link
Member

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

Copy link
Contributor

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.

Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer [[

Copy link
Member Author

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

Copy link
Member

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; }
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Contributor

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.

Copy link
Member

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:

And yes, the exported 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.

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.

Copy link
Contributor

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'
Copy link
Member

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?

Copy link
Member Author

@FranzBusch FranzBusch Jul 24, 2024

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.

Copy link
Member

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

Copy link
Contributor

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

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member

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.

cgrindel-self-hosted-renovate bot referenced this pull request in cgrindel/rules_swift_package_manager Aug 19, 2024
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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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 [@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;FranzBusch](https://togithub.com/FranzBusch) in
[https://github.com/apple/swift-nio/pull/2827](https://togithub.com/apple/swift-nio/pull/2827)

#### New Contributors

- [@&#8203;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)
- [@&#8203;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)
- [@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/none No version bump required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants