Skip to content

Commit 0571626

Browse files
committed
TSCBasic: repair the Windows build after #277
This fixes multiple issues with the file locking implementation on Windows. `AbsolutePath.root` is meaningless on Windows - there is no concept of a singular root on Windows, instead you have have 26 individual roots - A-Z. Stripping the count of characters for the root path leaves us with an invalid path string as the root drives have been stripped. The next character is an invalid character on most file systems, and so we must replace `:`. Instead of using a `MAX_NAME` to compute a length of the ARC, enforce a limit for the complete path (though an ARC would be limited to 255 codepoints on NTFS). This incidentally also corrects the emitted path, previous path computation left us with the file in PWD.
1 parent bccbd93 commit 0571626

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Sources/TSCBasic/Lock.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,20 @@ public final class FileLock {
178178
throw FileSystemError(.notDirectory, lockFilesDirectory)
179179
}
180180
// use the parent path to generate unique filename in temp
181-
var lockFileName = (resolveSymlinks(fileToLock.parentDirectory).appending(component: fileToLock.basename)).components.joined(separator: "_") + ".lock"
181+
var lockFileName = (resolveSymlinks(fileToLock.parentDirectory)
182+
.appending(component: fileToLock.basename))
183+
.components.joined(separator: "_")
184+
.replacingOccurrences(of: ":", with: "_") + ".lock"
185+
#if os(Windows)
186+
var lockFileUTF8 = lockFilesDirectory.appending(component: lockFileName).pathString.utf8.suffix(Int(MAX_PATH))
187+
#else
182188
if lockFileName.hasPrefix(AbsolutePath.root.pathString) {
183189
lockFileName = String(lockFileName.dropFirst(AbsolutePath.root.pathString.count))
184190
}
185191
// back off until it occupies at most `NAME_MAX` UTF-8 bytes but without splitting scalars
186192
// (we might split clusters but it's not worth the effort to keep them together as long as we get a valid file name)
187193
var lockFileUTF8 = lockFileName.utf8.suffix(Int(NAME_MAX))
194+
#endif
188195
while String(lockFileUTF8) == nil {
189196
// in practice this will only be a few iterations
190197
lockFileUTF8 = lockFileUTF8.dropFirst()

0 commit comments

Comments
 (0)