Is corelibs-foundation still accepting fixes?

What's the current status of issues/PRs on swift-corelibs-foundation for fixing broken functionality (not implementing new functionality)? Are new changes being accepted?

I'm asking because I just bumped into a FileManager issue (FileManager.replaceItemAt(_:withItemAt:) doesn't appear to function · Issue #4655 · swiftlang/swift-corelibs-foundation · GitHub) which was filed & had a PR put up in 2022. It definitely reproduces on nightly docker images. But I don't want to go notifying everyone on the thread if there's no point!

Also: generally, what's the etiquette here? Is it to try and bump the original PR or is it to try and put up my own version of the fix?

Yes, but FileManager in particular was rewritten and moved over to swift-foundation, here. We should close the original PR and move it over (with needed changes) to the other repo.

1 Like

Ah interesting. I looked at swift-foundation but I don't think replaceItemAt made it into that rewrite (unless my GitHub search is missing something: Code search results · GitHub).

So I think I don't really understand how these repos connect then - how is it that I can call that method on FileManager in Linux if FileManager is implemented in the new repo without that method?

Starting with Swift 6 a couple years ago, swift-corelibs-foundation on linux was "re-cored" around swift-foundation, so several APIs like FileManager are now defined in the latter, with some former APIs like replaceItemAt() turned into extension methods.

The upshot is any changes for that method will indeed have to be made to swift-corelibs-foundation, which I see is still accepting pulls. I'd ping the old pull first, then open your own depending on their response.

1 Like

Ah duh, extensions.

For some reason I thought it was swift-foundation re-exporting swift-corelibs-foundation rather than the other way around. This makes a lot of sense - thanks for the explanation @Finagolfin !

I will ping the old PR

1 Like

@willft do you know if your swift-corelibs-foundation PR has made it into the nightly builds of the swiftlang swift docker image? I've found a discrepancy with the Darwin version of FileManager.replaceItemAt and I was wondering if your PR fixes it. It seems like the originalItemUrl and newItemUrl parameters are swapped on non-Darwin. The example below demonstrates. On macOS using

% swift --version

swift-driver version: 1.148.6 Apple Swift version 6.3.3 (swiftlang-6.3.3.1.3 clang-2100.1.1.101)

and on the nightly docker image:

$ swift --version
Swift version 6.3.3-dev (LLVM 82cdc19fa54d566, Swift c83acbf89dd1298)

import Foundation

@main
struct file_manager_test {
    static func main() throws {

        let fileManager = FileManager.default
        let tempFile = fileManager.temporaryDirectory.appendingPathComponent("tempFile.txt")
        let content = "Hello, World!"
        let contentData = content.data(using: .utf8)
        guard fileManager.createFile(atPath: tempFile.path, contents: contentData) else {
            throw NSError(
                domain: "FileManagerError", code: 0,
                userInfo: [NSLocalizedDescriptionKey: "Failed to create file"])
        }

        guard let readContentData = fileManager.contents(atPath: tempFile.path),
            readContentData == contentData
        else {
            throw NSError(
                domain: "FileManagerError", code: 1,
                userInfo: [NSLocalizedDescriptionKey: "Content mismatch"])
        }

        let replacementContent = "Hello, Swift!"
        let replacementContentData = replacementContent.data(using: .utf8)
        let replacementFile = fileManager.temporaryDirectory.appendingPathComponent(
            "replacementFile.txt")
        guard fileManager.createFile(atPath: replacementFile.path, contents: replacementContentData)
        else {
            throw NSError(
                domain: "FileManagerError", code: 0,
                userInfo: [NSLocalizedDescriptionKey: "Failed to create replacement file"])
        }

        guard let readReplacementContentData = fileManager.contents(atPath: replacementFile.path),
            readReplacementContentData == replacementContentData
        else {
            throw NSError(
                domain: "FileManagerError", code: 2,
                userInfo: [NSLocalizedDescriptionKey: "Replacement content mismatch"])
        }

        #if canImport(Darwin)
            try fileManager.replaceItemAt(tempFile, withItemAt: replacementFile)
        // try fileManager.replaceItem(
        // at: tempFile, withItemAt: replacementFile, backupItemName: nil,
        // resultingItemURL: nil)
        #else
            // Swap the `at` and `withItemAt` parameters
            do {
                // This line that works on Darwin results in a crash on Linux, so we swap the parameters for Linux.
                // _ = try fileManager.replaceItemAt(tempFile, withItemAt: replacementFile)
                _ = try fileManager.replaceItemAt(replacementFile, withItemAt: tempFile)
                // _ = try fileManager.replaceItem(
                // at: replacementFile, withItemAt: tempFile, backupItemName: nil, options: [])
            } catch {
                if let error = error as NSError?, error.domain == NSCocoaErrorDomain,
                    error.code == NSFileNoSuchFileError
                {
                    print("File not found error: \(error)")
                } else {
                    print("Unexpected error: \(error)")
                }
            }
        #endif

        guard let finalContentData = fileManager.contents(atPath: tempFile.path),
            finalContentData == replacementContentData
        else {
            throw NSError(
                domain: "FileManagerError", code: 3,
                userInfo: [NSLocalizedDescriptionKey: "Final content mismatch"])
        }
    }
}

My PR should fix that as a central part of that PR was swapping the original/new args to the underlying libc functions. I suspect it hasn't made its way to the 6.3 branches, but I'm only passingly familiar with the release process. I'm not even sure if another 6.3 patch release is planned before 6.4.

I'd recommend you try a 6.4 or main branch nightly to see if it works there.

1 Like

Thanks! I didn't think to check 6.4. I just tried a nightly image of 6.4 and it does indeed solve the issue. Now it runs properly using the correct ordering, except for two warnings:

warning: safeExec: signal(32, SIG_DFL) failed: Invalid argument
warning: safeExec: signal(33, SIG_DFL) failed: Invalid argument

But I'm guessing those could be from the docker image or elsewhere within swift 6.4.

1 Like

One more non-Darwin 6.3 patch release has been announced.

1 Like