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

Switch to swift-format #1655

Merged
merged 2 commits into from
Oct 5, 2023
Merged

Conversation

glbrntt
Copy link
Collaborator

@glbrntt glbrntt commented Oct 2, 2023

Motivation:

SwiftFormat has served us well but doesn't produce
stable formatting over time; the output changes based
on the version used. This can cause churn over time as
the project has to be periodically reformatted.

Having tested multiple versions of swift-format, it's output
is significantly more stable between versions.

Modifications:

  • Switch to swift-format
  • Re-format code

Result:

  • Formatting should be more stable
  • Less flexibility: no rule for explicit-self, for example

@glbrntt
Copy link
Collaborator Author

glbrntt commented Oct 2, 2023

WDYT @FranzBusch?

Copy link
Collaborator

@FranzBusch FranzBusch left a comment

Choose a reason for hiding this comment

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

Yes please! Left two comments inline but agree with the rest of this!

@@ -0,0 +1,58 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this file is slightly different than the one in swift-certificates. Can we align on one please.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Aligning on formatting rules will be a significant amount of churn (52,000 lines) that I don't think is worth doing. The aim of this PR is to reduce churn by switching to a tool which produces more stable formatting; in that light the rules chosen follow the existing ones where possible to minimise the changes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see. On the other hand we are planning to do some significant work in gRPC and that might be a good opportunity to do this one time churn. Having an aligned formatting file across our repos is IMO worth it in the long run. We could take this churn and just tell git to ignore this one commit in blames so it shouldn't even show up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not convinced: we still need to work on and maintain v1 for quite some time so will still bear the cost of the significant churn. Formatters let users avoid having to do the manual work of sticking to style guidelines so the benefit of having a consistent style across projects is very low, IMO.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure we have to maintain the v1 but this is a one time churn right? If we decide that swift-format is the future for our project then IMO we should take that one time churn once and then have everything aligned.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, it's one time but it's a very large change. As I said above, I think the benefit of having consistent style across projects is low, especially as we have tools which enforce the style for you.

What we're effectively debating is which style to use. I would much prefer we stick to the style used in this PR because it minimises the changes made, so much so that they can be reviewed with relative ease.

@@ -16,43 +16,98 @@

set -eu

function log() { printf -- "** %s\n" "$*" >&2; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here. Can we align the script with the one in swift-certificates?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This divergence is useful: this version checks out, builds, and caches an appropriate version of swift-format making it easier for new developers to run the formatter (they don't need to know what version to get, how to check it out, etc.).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that this here does more. We should bring it over to the other repos IMO. I would love to have an aligned approach across all of our repos.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, @dnadoba and I talked about this a little while ago – I believe he was interested in this scripts ability to checkout/build/cache the formatter.

Copy link
Collaborator

@dnadoba dnadoba left a comment

Choose a reason for hiding this comment

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

LGTM. The two spaces before a trailing comment seems odd and unintentional. We should report this.

@@ -180,7 +181,7 @@ func makeClient(
.withTLS(trustRoots: .certificates([caCert.certificate]))
#else
fatalError("'useTLS: true' passed to \(#function) but NIOSSL is not available")
#endif // canImport(NIOSSL)
#endif // canImport(NIOSSL)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like a bug in swift-format. There should only be one space.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure it is; it's quite common to include two spaces before an inline comment.

@@ -415,7 +419,7 @@ internal final class AsyncServerHandler<
internal func receiveInterceptedMetadata(_ headers: HPACKHeaders) {
switch self.interceptorStateMachine.interceptedRequestMetadata() {
case .forward:
() // continue
() // continue
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems wrong too.

self.format.windowBits, // window size, i.e. deflate/gzip
8, // memory level (this is the default value in the docs)
Z_DEFAULT_STRATEGY // compression strategy
Z_DEFAULT_COMPRESSION, // compression level
Copy link
Collaborator

Choose a reason for hiding this comment

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

also here.

@@ -58,7 +60,7 @@ final class ServerInterceptorStateMachineTests: GRPCTestCase {
stateMachine.interceptedRequestMetadata().assertForward()
stateMachine.interceptRequestEnd().assertIntercept()
stateMachine.interceptedRequestEnd().assertForward()
stateMachine.interceptedRequestEnd().assertCancel() // Can't intercept end twice.
stateMachine.interceptedRequestEnd().assertCancel() // Can't intercept end twice.
Copy link
Collaborator

Choose a reason for hiding this comment

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

seems like as all trailing comments have two spaces.

@@ -207,7 +209,7 @@ class LengthPrefixedMessageReaderTests: GRPCTestCase {

// Ensure we can read a message when the rest of the bytes are delivered
let restOfBytes: [UInt8] = [
0x01, // final byte of message
0x01 // final byte of message
Copy link
Collaborator

Choose a reason for hiding this comment

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

The trailing comma should also not be removed. Another bug worth reporting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's intentional, from https://github.com/apple/swift-format/blob/main/Documentation/Configuration.md:

multiElementCollectionTrailingCommas (boolean): Determines whether multi-element collection literals should have trailing commas. Defaults to true.

Note multi-element. IIRC from digging through issues previously this was to avoid cases where e.g. let x: [Int] could be reformatted as:

let x: [
  Int,
]

@glbrntt glbrntt marked this pull request as ready for review October 5, 2023 12:52
@glbrntt glbrntt enabled auto-merge (rebase) October 5, 2023 12:52
@glbrntt glbrntt added the semver/patch No public API change. label Oct 5, 2023
@glbrntt glbrntt merged commit b76f4b4 into grpc:main Oct 5, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants