nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix

swift: fix build w/ glibc-2.39

Failing Hydra build: https://hydra.nixos.org/build/249763077/nixlog/12

The problem is that glibc commit
64b1a44183a3094672ed304532bedb9acc707554 marked the `FILE*` argument of a few
functions including `fread` & `ferror` as non-null. The applied patch
("Android: add better nullability checks for nullability annotations added in NDK 26")
is targeted for the Android platform, but fixes said issue as well: the
handle returned from `fopen` is of type `Optional<T>` and the `guard`
expression unwraps that now (and throws an exception if `nil` is
returned). The previous `nil`-check didn't modify the type of `fp`, but
only raised the exception and moved on with `Optional<T>`.

It's a little sad that the patch needs to be applied at so many places,
but I guess that's what you get with language-level package managers 🤷
Also, seems good-enough to me given that it's actually temporary, the
patch is already upstream and will probably be obsolete at one of the
next Swift updates.

+30 -104
+6 -1
pkgs/development/compilers/swift/sourcekit-lsp/default.nix
··· 1 1 { lib 2 2 , stdenv 3 3 , callPackage 4 + , fetchpatch 4 5 , pkg-config 5 6 , swift 6 7 , swiftpm ··· 42 41 patch -p1 -d .build/checkouts/indexstore-db -i ${./patches/indexstore-db-macos-target.patch} 43 42 44 43 swiftpmMakeMutable swift-tools-support-core 45 - patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch} 44 + patch -p1 -d .build/checkouts/swift-tools-support-core -i ${fetchpatch { 45 + url = "https://github.com/apple/swift-tools-support-core/commit/990afca47e75cce136d2f59e464577e68a164035.patch"; 46 + hash = "sha256-PLzWsp+syiUBHhEFS8+WyUcSae5p0Lhk7SSRdNvfouE="; 47 + includes = [ "Sources/TSCBasic/FileSystem.swift" ]; 48 + }} 46 49 47 50 # This toggles a section specific to Xcode XCTest, which doesn't work on 48 51 # Darwin, where we also use swift-corelibs-xctest.
+5 -1
pkgs/development/compilers/swift/swift-driver/default.nix
··· 54 54 55 55 configurePhase = generated.configure + '' 56 56 swiftpmMakeMutable swift-tools-support-core 57 - patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch} 57 + patch -p1 -d .build/checkouts/swift-tools-support-core -i ${fetchpatch { 58 + url = "https://github.com/apple/swift-tools-support-core/commit/990afca47e75cce136d2f59e464577e68a164035.patch"; 59 + hash = "sha256-PLzWsp+syiUBHhEFS8+WyUcSae5p0Lhk7SSRdNvfouE="; 60 + includes = [ "Sources/TSCBasic/FileSystem.swift" ]; 61 + }} 58 62 ''; 59 63 60 64 # TODO: Tests depend on indexstore-db being provided by an existing Swift
-33
pkgs/development/compilers/swift/swift-driver/patches/force-unwrap-file-handles.patch
··· 1 - From 8d9ab4b6ed24a97e8af0cc338a52aacdcf438b8c Mon Sep 17 00:00:00 2001 2 - From: Pavel Sobolev <paveloom@riseup.net> 3 - Date: Tue, 21 Nov 2023 20:53:33 +0300 4 - Subject: [PATCH] Force-unwrap file handles. 5 - 6 - --- 7 - Sources/TSCBasic/FileSystem.swift | 4 ++-- 8 - 1 file changed, 2 insertions(+), 2 deletions(-) 9 - 10 - diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift 11 - index 3a63bdf..a1f3d9d 100644 12 - --- a/Sources/TSCBasic/FileSystem.swift 13 - +++ b/Sources/TSCBasic/FileSystem.swift 14 - @@ -425,7 +425,7 @@ private class LocalFileSystem: FileSystem { 15 - if fp == nil { 16 - throw FileSystemError(errno: errno, path) 17 - } 18 - - defer { fclose(fp) } 19 - + defer { fclose(fp!) } 20 - 21 - // Read the data one block at a time. 22 - let data = BufferedOutputByteStream() 23 - @@ -455,7 +455,7 @@ private class LocalFileSystem: FileSystem { 24 - if fp == nil { 25 - throw FileSystemError(errno: errno, path) 26 - } 27 - - defer { fclose(fp) } 28 - + defer { fclose(fp!) } 29 - 30 - // Write the data in one chunk. 31 - var contents = bytes.contents 32 - -- 33 - 2.42.0
+6 -1
pkgs/development/compilers/swift/swift-format/default.nix
··· 1 1 { lib 2 2 , stdenv 3 + , fetchpatch 3 4 , callPackage 4 5 , swift 5 6 , swiftpm ··· 22 21 23 22 configurePhase = generated.configure + '' 24 23 swiftpmMakeMutable swift-tools-support-core 25 - patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch} 24 + patch -p1 -d .build/checkouts/swift-tools-support-core -i ${fetchpatch { 25 + url = "https://github.com/apple/swift-tools-support-core/commit/990afca47e75cce136d2f59e464577e68a164035.patch"; 26 + hash = "sha256-PLzWsp+syiUBHhEFS8+WyUcSae5p0Lhk7SSRdNvfouE="; 27 + includes = [ "Sources/TSCBasic/FileSystem.swift" ]; 28 + }} 26 29 ''; 27 30 28 31 # We only install the swift-format binary, so don't need the other products.
-33
pkgs/development/compilers/swift/swift-format/patches/force-unwrap-file-handles.patch
··· 1 - From 8d9ab4b6ed24a97e8af0cc338a52aacdcf438b8c Mon Sep 17 00:00:00 2001 2 - From: Pavel Sobolev <paveloom@riseup.net> 3 - Date: Tue, 21 Nov 2023 20:53:33 +0300 4 - Subject: [PATCH] Force-unwrap file handles. 5 - 6 - --- 7 - Sources/TSCBasic/FileSystem.swift | 4 ++-- 8 - 1 file changed, 2 insertions(+), 2 deletions(-) 9 - 10 - diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift 11 - index 3a63bdf..a1f3d9d 100644 12 - --- a/Sources/TSCBasic/FileSystem.swift 13 - +++ b/Sources/TSCBasic/FileSystem.swift 14 - @@ -425,7 +425,7 @@ private class LocalFileSystem: FileSystem { 15 - if fp == nil { 16 - throw FileSystemError(errno: errno, path) 17 - } 18 - - defer { fclose(fp) } 19 - + defer { fclose(fp!) } 20 - 21 - // Read the data one block at a time. 22 - let data = BufferedOutputByteStream() 23 - @@ -455,7 +455,7 @@ private class LocalFileSystem: FileSystem { 24 - if fp == nil { 25 - throw FileSystemError(errno: errno, path) 26 - } 27 - - defer { fclose(fp) } 28 - + defer { fclose(fp!) } 29 - 30 - // Write the data in one chunk. 31 - var contents = bytes.contents 32 - -- 33 - 2.42.0
+13 -2
pkgs/development/compilers/swift/swiftpm/default.nix
··· 1 1 { lib 2 2 , stdenv 3 3 , callPackage 4 + , fetchpatch 4 5 , cmake 5 6 , ninja 6 7 , git ··· 196 195 ''; 197 196 }; 198 197 198 + # Part of this patch fixes for glibc 2.39: glibc patch 64b1a44183a3094672ed304532bedb9acc707554 199 + # marks the `FILE*` argument to a few functions including `ferror` & `fread` as non-null. However 200 + # the code passes an `Optional<T>` to these functions. 201 + # This patch uses a `guard` which effectively unwraps the type (or throws an exception). 202 + swift-tools-support-core-glibc-fix = fetchpatch { 203 + url = "https://github.com/apple/swift-tools-support-core/commit/990afca47e75cce136d2f59e464577e68a164035.patch"; 204 + hash = "sha256-PLzWsp+syiUBHhEFS8+WyUcSae5p0Lhk7SSRdNvfouE="; 205 + includes = [ "Sources/TSCBasic/FileSystem.swift" ]; 206 + }; 207 + 199 208 swift-tools-support-core = mkBootstrapDerivation { 200 209 name = "swift-tools-support-core"; 201 210 src = generated.sources.swift-tools-support-core; 202 211 203 212 patches = [ 204 - ./patches/force-unwrap-file-handles.patch 213 + swift-tools-support-core-glibc-fix 205 214 ]; 206 215 207 216 buildInputs = [ ··· 400 389 swiftpmMakeMutable swift-tools-support-core 401 390 substituteInPlace .build/checkouts/swift-tools-support-core/Sources/TSCTestSupport/XCTestCasePerf.swift \ 402 391 --replace 'canImport(Darwin)' 'false' 403 - patch -p1 -d .build/checkouts/swift-tools-support-core -i ${./patches/force-unwrap-file-handles.patch} 392 + patch -p1 -d .build/checkouts/swift-tools-support-core -i ${swift-tools-support-core-glibc-fix} 404 393 405 394 # Prevent a warning about SDK directories we don't have. 406 395 swiftpmMakeMutable swift-driver
-33
pkgs/development/compilers/swift/swiftpm/patches/force-unwrap-file-handles.patch
··· 1 - From 8d9ab4b6ed24a97e8af0cc338a52aacdcf438b8c Mon Sep 17 00:00:00 2001 2 - From: Pavel Sobolev <paveloom@riseup.net> 3 - Date: Tue, 21 Nov 2023 20:53:33 +0300 4 - Subject: [PATCH] Force-unwrap file handles. 5 - 6 - --- 7 - Sources/TSCBasic/FileSystem.swift | 4 ++-- 8 - 1 file changed, 2 insertions(+), 2 deletions(-) 9 - 10 - diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift 11 - index 3a63bdf..a1f3d9d 100644 12 - --- a/Sources/TSCBasic/FileSystem.swift 13 - +++ b/Sources/TSCBasic/FileSystem.swift 14 - @@ -425,7 +425,7 @@ private class LocalFileSystem: FileSystem { 15 - if fp == nil { 16 - throw FileSystemError(errno: errno, path) 17 - } 18 - - defer { fclose(fp) } 19 - + defer { fclose(fp!) } 20 - 21 - // Read the data one block at a time. 22 - let data = BufferedOutputByteStream() 23 - @@ -455,7 +455,7 @@ private class LocalFileSystem: FileSystem { 24 - if fp == nil { 25 - throw FileSystemError(errno: errno, path) 26 - } 27 - - defer { fclose(fp) } 28 - + defer { fclose(fp!) } 29 - 30 - // Write the data in one chunk. 31 - var contents = bytes.contents 32 - -- 33 - 2.42.0