Description
After adding SwiftGD as a dependency in Package.swift and importing it, the build fails on Linux due to optional FILE* pointers being passed directly to fclose.
System
OS - Linux Ubuntu 24.04.3 LTS
Swift Toolchain - Swift version 6.2.2 (swift-6.2.2-RELEASE)
Steps to reproduce
- Add SwiftGD as a dependency in Package.swift to a executable package
- Run: swift build
- Build fails during compilation of SwiftGD with the following error
Error
swift build
[1/1] Planning build
Building for debugging...
/home/pradeep/Projects/Issue/.build/checkouts/SwiftGD/Sources/SwiftGD/Image.swift:221:24: error: value of optional type 'UnsafeMutablePointer?' (aka 'Optional<UnsafeMutablePointer<_IO_FILE>>') must be unwrapped to a value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<_IO_FILE>')
219 | public convenience init?(url: URL) {
220 | let inputFile = fopen(url.path, "rb")
221 | defer { fclose(inputFile) }
| |- error: value of optional type 'UnsafeMutablePointer?' (aka 'Optional<UnsafeMutablePointer<_IO_FILE>>') must be unwrapped to a value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<_IO_FILE>')
| |- note: coalesce using '??' to provide a default when the optional value contains 'nil'
| `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
222 |
223 | guard inputFile != nil else { return nil }
/home/pradeep/Projects/Issue/.build/checkouts/SwiftGD/Sources/SwiftGD/Image.swift:253:24: error: value of optional type 'UnsafeMutablePointer?' (aka 'Optional<UnsafeMutablePointer<_IO_FILE>>') must be unwrapped to a value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<_IO_FILE>')
251 | // open our output file, then defer it to close
252 | let outputFile = fopen(url.path, "wb")
253 | defer { fclose(outputFile) }
| |- error: value of optional type 'UnsafeMutablePointer?' (aka 'Optional<UnsafeMutablePointer<_IO_FILE>>') must be unwrapped to a value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<_IO_FILE>')
| |- note: coalesce using '??' to provide a default when the optional value contains 'nil'
| `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
254 |
255 | // write the correct output format based on the path extension
[8/12] Compiling SwiftGD Error.swift
Description
After adding SwiftGD as a dependency in Package.swift and importing it, the build fails on Linux due to optional FILE* pointers being passed directly to fclose.
System
OS - Linux Ubuntu 24.04.3 LTS
Swift Toolchain - Swift version 6.2.2 (swift-6.2.2-RELEASE)
Steps to reproduce
Error
swift build
[1/1] Planning build
Building for debugging...
/home/pradeep/Projects/Issue/.build/checkouts/SwiftGD/Sources/SwiftGD/Image.swift:221:24: error: value of optional type 'UnsafeMutablePointer?' (aka 'Optional<UnsafeMutablePointer<_IO_FILE>>') must be unwrapped to a value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<_IO_FILE>')
219 | public convenience init?(url: URL) {
220 | let inputFile = fopen(url.path, "rb")
221 | defer { fclose(inputFile) }
| |- error: value of optional type 'UnsafeMutablePointer?' (aka 'Optional<UnsafeMutablePointer<_IO_FILE>>') must be unwrapped to a value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<_IO_FILE>')
| |- note: coalesce using '??' to provide a default when the optional value contains 'nil'
| `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
222 |
223 | guard inputFile != nil else { return nil }
/home/pradeep/Projects/Issue/.build/checkouts/SwiftGD/Sources/SwiftGD/Image.swift:253:24: error: value of optional type 'UnsafeMutablePointer?' (aka 'Optional<UnsafeMutablePointer<_IO_FILE>>') must be unwrapped to a value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<_IO_FILE>')
251 | // open our output file, then defer it to close
252 | let outputFile = fopen(url.path, "wb")
253 | defer { fclose(outputFile) }
| |- error: value of optional type 'UnsafeMutablePointer?' (aka 'Optional<UnsafeMutablePointer<_IO_FILE>>') must be unwrapped to a value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePointer<_IO_FILE>')
| |- note: coalesce using '??' to provide a default when the optional value contains 'nil'
| `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
254 |
255 | // write the correct output format based on the path extension
[8/12] Compiling SwiftGD Error.swift