2

Using Xcode (11.4.1) on Catalina (10.15.4, only updated from Mojave 5 days ago), with a tiny SPM-based project opened in Xcode in "folder mode" (i.e. not an actual .xcodeproj), I have a test which should read some input data from a sample file. Following recommendations for constructing a URL to those files, Data(contentsOf:) cannot read it, although the generated URL is correct.

The code is:

func testCanReadConfigFromFile() {
    let thisDirectory = URL(fileURLWithPath: #file)
        .deletingLastPathComponent()

    let url = thisDirectory
        .appendingPathComponent("Test_data", isDirectory: true)
        .appendingPathComponent("Config.json", isDirectory: false)

    print(url.absoluteString)

    do {
        let _ = try Data(contentsOf: url)
    } catch let error {
        print(error)
        print(error.localizedDescription)
    }
}

The exception is:

Error Domain=NSCocoaErrorDomain Code=256 "The file “Config.json” couldn’t be opened."
UserInfo={NSFilePath=/full/path/to/project/Tests/ProjectTests/Test_data/Config.json,
NSUnderlyingError=0x100b0aa30
{Error Domain=NSPOSIXErrorDomain Code=4 "Interrupted system call"}}

If I take the string output by print(url.absoluteString) and in a terminal execute file <absoulteString>, it confirms that my file does exist at that path (and that it is JSON).

"Interrupted system call" makes me think Catalina's stricter sand-boxing rules are to blame, but I have not been shown a permissions dialog. This project is within my ~/Documents folder, which Xcode does have permission to read.

Edited to add:

swift test at the command line works. It's only running tests in Xcode that exhibits the problem.

Chris
  • 3,445
  • 3
  • 22
  • 28

0 Answers0