swift-foundation icon indicating copy to clipboard operation
swift-foundation copied to clipboard

FileManager creating temporary directory has differing behavior between FoundationEssentials/Foundation (Linux)

Open WFT opened this issue 1 year ago • 0 comments

This call has differing behavior when importing Foundation vs when importing FoundationEssentials on Linux:

try FileManager.default.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: documents, create: true)

Reproduction

The following snippet exits successfully when importing Foundation (macOS or Linux), but if you instead import FoundationEssentials it throws an error when creating temp.

import Foundation // Comment this line and uncomment the next to see the issue
// import FoundationEssentials

print("Creating documents...")
let documents = try FileManager.default.url(
    for: .documentDirectory,
    in: .userDomainMask,
    appropriateFor: nil,
    create: true
)
print("Creating temporary...")
// Raises FoundationEssentials.CocoaError(code: FoundationEssentials.CocoaError.Code(rawValue: 256), userInfo: [:])
let temp = try FileManager.default.url(
    for: .itemReplacementDirectory,
    in: .userDomainMask,
    appropriateFor: documents,
    create: true
)

print("Results")

print(temp)
print(FileManager.default.fileExists(atPath: temp.path(percentEncoded: false)))

You can run this with the swift:6.0.1 Docker image to see the issue.

Expected behavior

I think temp should be a temporary directory which has been created, probably somewhere like /tmp/abc123/NSIRD_test.swift_xyz456/. I feel that’s the best approximation of what happens on macOS (where we get something like /var/folders/q0/abc123/T/TemporaryItems/NSIRD_test.swift_xyz456/). The particular format isn’t particularly important — I’m basically just looking for a portable form of mkdtemp.

Or at the very least the behavior shouldn’t change when the imported module changes.

Actual behavior

An error is raised from the indicated line (FoundationEssentials only).

WFT avatar Oct 18 '24 21:10 WFT