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?
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.
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?
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 !
@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.
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: