apple / swift-log

A Logging API for Swift
https://apple.github.io/swift-log/
Apache License 2.0
3.48k stars 284 forks source link

Fails to compile on Fedora 39 with Swift 5.8.1 (From repos) or official 5.9 binaries #278

Open JCWasmx86 opened 11 months ago

JCWasmx86 commented 11 months ago

Expected behavior

It compiles on Arch Linux and Fedora 39 (Works on Fedora 38)

Actual behavior

Does not compile on Arch Linux/Fedora 39 (The following part of the issue assumes Fedora 39)

Steps to reproduce

  1. Create fedora 39 VM/container
  2. Install swift using dnf or use the official binaries from swift.org
  3. Try to compile swift-log (From master or the latest release)

If possible, minimal yet complete reproducer code (or URL to code)

git clone https://github.com/apple/swift-log
cd swift-log
swift build
# Or
wget https://download.swift.org/swift-5.9-release/ubuntu2204/swift-5.9-RELEASE/swift-5.9-RELEASE-ubuntu22.04.tar.gz
tar xvf swift-5.9-RELEASE-ubuntu22.04.tar.gz
./swift-5.9-RELEASE-ubuntu22.04/usr/bin/swift build
/swift-log/Sources/Logging/Logging.swift:1392:64: error: value of optional type 'UnsafeMutablePointer<tm>?' must be unwrapped to a value of type 'UnsafeMutablePointer<tm>'
        strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", localTime)
                                                               ^
/swift-log/Sources/Logging/Logging.swift:1391:13: note: short-circuit using 'guard' to exit this function early if the optional value contains 'nil'
        let localTime = localtime(&timestamp)
            ^
        guard                                 else { return <#default value#> }
/swift-log/Sources/Logging/Logging.swift:1391:25: note: coalesce using '??' to provide a default when the optional value contains 'nil'
        let localTime = localtime(&timestamp)
                        ^
                                              ?? <#default value#>
/swift-log/Sources/Logging/Logging.swift:1391:25: note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
        let localTime = localtime(&timestamp)
                        ^
                                             !
/swift-log/Sources/Logging/Logging.swift:1392:64: note: coalesce using '??' to provide a default when the optional value contains 'nil'
        strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", localTime)
                                                               ^
                                                                         ?? <#default value#>
/swift-log/Sources/Logging/Logging.swift:1392:64: note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
        strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", localTime)
                                                               ^
                                                                        !
error: fatalError

SwiftLog version/commit hash

cb28750240a9389e0023ee3e3cb6c83ce0960f5c and 1.5.3

Swift & OS version (output of swift --version && uname -a)

Swift version 5.8.1 (swift-5.8.1-RELEASE)
Target: x86_64-unknown-linux-gnu
Linux 6d64617a9d4e 6.4.15-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Sep  7 00:25:01 UTC 2023 x86_64 GNU/Linux

and

Swift version 5.9 (swift-5.9-RELEASE)
Target: x86_64-unknown-linux-gnu
Linux 6d64617a9d4e 6.4.15-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Sep  7 00:25:01 UTC 2023 x86_64 GNU/Linux

I was able to fix this with the help of helje5 by doing this hack: https://github.com/JCWasmx86/swift-log/commit/332ed31d777c7acbf4790a790bcfb54fe264de27

1Jo1 commented 11 months ago

I have the exact same issue on Arch Linux, maybe its a glibc related issue @JCWasmx86 thanks for this nice simple hack :)

glibc 2.38-5
Swift version 5.9 (swift-5.9-RELEASE)
Target: x86_64-unknown-linux-gnu`Swift version 5.9 (swift-5.9-RELEASE)
Target: x86_64-unknown-linux-gnu
6.5.5
a40yostudent commented 11 months ago

Looking at the same function called in the line 1392:

func strftime( : UnsafeMutablePointer!, : Int, : UnsafePointer!, : UnsafePointer! ) -> Int`

func strftime(_ s: UnsafeMutablePointer, _ _maxsize: Int, format: UnsafePointer, _ __tp: UnsafePointer) -> Int`

Looks like it is safe to just force unwrap localtime like this on line failing: strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", localTime!)

It worked on my computer™

JCWasmx86 commented 11 months ago

But that would break code on Fedora 38 for example. Not sure what the best approach would be, as dropping somewhat recent OS versions like Fedora 38 is probably a bit too fast.

a40yostudent commented 11 months ago

Of course, I meant it's safe if you're on the OS experiencing this issue. I also wonder if the hack has been already tested on older versions.

consuelita commented 10 months ago

Also seeing this behavior on Ubuntu 23.10, Swift 5.9.1

trufanov-nok commented 10 months ago

Same problem Kubuntu 23.10, Swift 5.8.1/5.9.1

lhoward commented 8 months ago

Looks like this was fixed in f447991, but a new release would be handy.