Support for swift-format on Xcode-beta 13.0b1?

So I'm building a library that supports 5.4 but would also like to support some newer async APIs when swift 5.5 is available.

The problem I have is that the formatter in the 5.4 branch doesn't understand that try await is valid syntax, and instead does this:

--- a/Sources/APIBuilder/DefaultRequestExecutor.swift
+++ b/Sources/APIBuilder/DefaultRequestExecutor.swift
@@ -25,7 +25,7 @@ public class DefaultRequestExecutor: RequestExecutor {
     @available(swift 5.5)
     @available(macOS 12.0, *)
     public func execute(_ request: URLRequest) async throws -> Response {
-        let (data, response) = try await urlSession.data(for: request)
+        let (data, response) = try awaiturlSession.data(for: request)
         guard let httpResponse = response as? HTTPURLResponse else {
             throw StringError("Unknown error")
         }

where it joins the await keyword to the statement to await (which is somewhat weird since I'd expect it to fail on unknown syntax instead, but I digress)

I guess my question is about general guidance on what to do here. I tried building swift-format from main, but it then fails with The loaded '_InternalSwiftSyntaxParser' library is from a toolchain that is not compatible with this version of SwiftSyntax, this is on macOS 12.0 beta 1 with Xcode 13 beta 1.

Am I out of luck for now and need to disable the formatter? Ideally what I want is for the formatter to skip #if swift(>=5.5) ... #endif but I can see how that might not be trivial.

1 Like

To build the formatter against a newer toolchain, you'd need to update the swift-syntax dependency to be the one that matches the toolchain you're building against, since the syntax hash in the swift-syntax sources need to match the one in the toolchain's parser dylib to verify that they're compatible.

After the extended holiday weekend, I can start a 5.5-compatible branch that's compatible with the Xcode 13 beta and adds breaks in appropriate places for some of the new syntax additions (like the AwaitExpr node, in this case).

1 Like

This is (finally!) done; the swift-5.5-branch is currently compatible with Xcode 13.0 beta 3, and I'll keep it updated as needed when new betas and the final release land.

The latest updates should produce better results when using the newer concurrency constructs. One new language feature, postfix-#if expressions, is still a work in progress.

5 Likes

:partying_face: For anyone using GitHub Actions for their formatting, this is what's working for us:

1 Like

And since Xcode 13.0 beta 4 (13A5201i) was released a day after I cut that branch, I'm relieved happy to report that it's still compatible with that version too.

(There is an incompatible change in the swift-syntax 5.5 release branch that I thought might require an update, but it looks like it's not included in the most recent Xcode yet.)

Beta 5 seems to have broken things for me and I'm getting those errors again.

The loaded '_InternalSwiftSyntaxParser' library is from a toolchain that is not compatible with this version of SwiftSyntax

User error? Or did that change slip in?

Has anyone had luck getting swift-format working in Xcode 13 beta 5?

Looks like this bump is needed for beta 5 support Update SwiftSyntax by keith · Pull Request #262 · apple/swift-format · GitHub

1 Like

Thanks for the PR, @Keith! I've just merged it so the swift-5.5-branch should be compatible with Xcode 13 beta 5 now.

1 Like

I've just posted the 0.50500.0 release of swift-format, which is compatible with the Swift 5.5 release.

There were no compatibility changes between Xcode 13 beta 5 and the final release, but the 0.50500.0 release depends on the 0.50500.0 tag of swift-syntax instead of an arbitrary commit, for anyone who may be using swift-format and swift-syntax as package dependencies.

2 Likes