swiftservervaporfly

Swift Vapor Server Error When Building Docker on Fly.io


I have built a vapor server that is running fine on a mac server on HostMyApple. I'm trying to migrate to fly.io. When I run the command "fly launch" it begins building but then gives these errors:

255.8 /build/Sources/App/Models/Resident.swift:97:30: error: value of type 'Date' has no member 'formatted' 255.8 return (newDate?.formatted(date: .numeric, time: .omitted))! 255.8 ~~~~~~~~ ^~~~~~~~~ 255.8 /build/Sources/App/Models/Resident.swift:97:47: error: cannot infer contextual base in reference to member 'numeric' 255.8 return (newDate?.formatted(date: .numeric, time: .omitted))! 255.8 ~^~~~~~~ 255.8 /build/Sources/App/Models/Resident.swift:97:63: error: cannot infer contextual base in reference to member 'omitted' 255.8 return (newDate?.formatted(date: .numeric, time: .omitted))! 255.8 ~^~~~~~~ 255.8 /build/Sources/App/Models/Resident.swift:119:26: error: value of type 'Date' has no member 'formatted' 255.8 return (rentDate.formatted(date: .abbreviated, time: .omitted)) 255.8 ~~~~~~~~ ^~~~~~~~~ 255.8 /build/Sources/App/Models/Resident.swift:119:43: error: cannot infer contextual base in reference to member 'abbreviated' 255.8 return (rentDate.formatted(date: .abbreviated, time: .omitted)) 255.8 ~^~~~~~~~~~~ 255.8 /build/Sources/App/Models/Resident.swift:119:63: error: cannot infer contextual base in reference to member 'omitted' 255.8 return (rentDate.formatted(date: .abbreviated, time: .omitted))

The build fails. I can't understand why my server is working but when I try to migrate to fly these functions are not recognized. Can anyone help me make sense of what is going on here and how to fix the problem?


Solution

  • Your project at the moment is not compatible with Linux.

    The Linux version of the Foundation framework (swift-corelibs-foundation) is roughly compatible with up to iOS 13/macOS 11, any later additions to their Darwin counterpart are simply missing, regardless of your Swift version or how well it works on macOS. (Other parts of Foundation may also not work correctly despite compiling – but in its defense, a lot of compatibility issues were fixed over the years.)


    Date.formatted(date:time:) is one of the newer functions (available from macOS 12) that are missing completely on Linux. You will have to find a less fancy alternative solution with old classes, for example DateFormatter.


    To iterate faster without fly.io's overhead, you should try building the project locally with Docker. The errors will be the same, but you will get to them much faster on each attempt.