URLResourceValue.creationDate is nil on Windows

Hi all, I'm new to Swift and have been playing around with it on Windows and Linux. Given the following code:

import Foundation

let fm = FileManager.default
let path = URL(fileURLWithPath: Bundle.main.resourcePath!)

let files = try! fm.contentsOfDirectory(at: path, 
                    includingPropertiesForKeys: [.nameKey, .creationDateKey], 
                    options: .skipsHiddenFiles)

for file in files {
    let fileAttributes = try! file.resourceValues(forKeys: [.nameKey, .creationDateKey])

    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .medium
    
    let formattedName = fileAttributes.name ?? "unknown name"
    let formattedDate = fileAttributes.creationDate.map { formatter.string(from: $0) } 
        ?? "unknown date"

    print("Found \(formattedName), created at \(formattedDate)")
}

The creationDate is populated for Linux but not Windows. I'm not sure whether this is an actual issue or just an expected quirk of Windows. Is there some configuration I'm missing with FileManager in order to have access to creationDate on Windows or is it possible this is a bug?

swift --version gives me:

compnerd.org Swift version 5.3-dev (LLVM e471fa48a152468, Swift 70859e867e36bbd)
Target: x86_64-unknown-windows-msvc

Other info:

Windows 10 build: 18362.1082
Build tools version: MSVC v142 - VS 2019 C++ x64/x86 build tools (v14.27)
Windows SDK installed: 10.0.18362.0

This seems like a bug in Foundation! Would you mind creating a JIRA report for this? (Please do tag it with WIndows).

I can definitely reproduce this with a simple test case. It seems that for some reason Foundation is failing to retrieve the creation time for the files.

Will do, thanks for checking into it! I'll update this thread with the JIRA number once I create it.

Edit: SR-13617 created to report this issue. @compnerd, let me know if there's anything about the report that can be improved.

With the couple of changes that I've put up, it seems to report the time as expected now:

S:\tmp\test>swift run
S:\tmp\test: warning: Creating library C:\Users\compnerd\AppData\Local\Temp\TemporaryDirectory.aNqceM\test-manifest.lib and object C:\Users\compnerd\AppData\Local\Temp\TemporaryDirectory.aNqceM\test-manifest.exp
[1/3] Compiling test main.swift
[2/4] Merging module test
[3/4] Wrapping AST for test for debugging
   Creating library S:\tmp\test\.build\x86_64-unknown-windows-msvc\debug\test.lib and object S:\tmp\test\.build\x86_64-unknown-windows-msvc\debug\test.exp
[4/4] Linking S:\tmp\test\.build\x86_64-unknown-windows-msvc\debug\test.exe
Found testPackageTests.product, created at 9/26/20, 1:19:56 PM
Found test.swiftsourceinfo, created at 9/26/20, 1:20:03 PM
Found test.swiftmodule, created at 9/26/20, 1:20:03 PM
Found test.swiftdoc, created at 9/26/20, 1:20:03 PM
Found test.product, created at 9/26/20, 1:19:56 PM
Found test.lib, created at 9/26/20, 1:20:03 PM
Found test.exp, created at 9/26/20, 1:20:03 PM
Found test.exe, created at 9/26/20, 1:20:03 PM

Found test.build, created at 9/26/20, 1:19:56 PM
S:\tmp\test>Found ModuleCache, created at 9/26/20, 1:19:56 PM
Found index, created at 9/26/20, 1:19:59 PM
Found description.json, created at 9/26/20, 1:19:56 PM

It will probably take a little bit for review and to get it merged, but hopefully it can be included in a future snapshot. It would be possible for you to locally apply and build build Foundation, but may be a bit tricky with Foundation also being part of the SDK.

2 Likes

Wow, thanks for the super prompt response. On average, how long does something like this take to get into a snapshot build? I’m thinking I’ll probably experiment with other stuff until this makes it in.

I definitely would recommend experimenting with other stuff while we wait for this to get into a snapshot.

The expectation is that most of the functionality should be identical (or rather near identical - there are some differences inherent to windows which normally are expressed as differences in how to solve the problem). Differences would be good to identify and resolve.

2 Likes

I think Foundation: populate the `.creationDate` attribute on Windows by compnerd · Pull Request #2884 · apple/swift-corelibs-foundation · GitHub must have fixed this. I just tried out the latest snapshot build, and I'm now getting creationDate! I also noticed that sourcekit-lsp is now included in the toolchain. Seems to work pretty well in VS Code from what I'm seeing. Thanks @compnerd!

You're welcome! And, yes, that was the change that fixed the file creation date reporting. Thanks for the bug report!