Skip to content

Fix a build failure on Windows, where NAME_MAX doesn't exist #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/TSCBasic/Lock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public final class FileLock {
if lockFileName.hasPrefix(AbsolutePath.root.pathString) {
lockFileName = String(lockFileName.dropFirst(AbsolutePath.root.pathString.count))
}
#if !os(Windows)
// back off until it occupies at most `NAME_MAX` UTF-8 bytes but without splitting scalars
// (we might split clusters but it's not worth the effort to keep them together as long as we get a valid file name)
var lockFileUTF8 = lockFileName.utf8.suffix(Int(NAME_MAX))
Expand All @@ -191,6 +192,7 @@ public final class FileLock {
}
// we will never end up with nil since we have ASCII characters at the end
lockFileName = String(lockFileUTF8) ?? lockFileName
#endif
let lockFilePath = lockFilesDirectory.appending(component: lockFileName)

let lock = FileLock(at: lockFilePath)
Expand Down
3 changes: 2 additions & 1 deletion Tests/TSCBasicTests/FileSystemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,13 @@ class FileSystemTests: XCTestCase {
let lockFile = tempDir.appending(component: "lockfile")

try _testFileSystemFileLock(fileSystem: localFileSystem, fileA: fileA, fileB: fileB, lockFile: lockFile)

#if !os(Windows)
// Test some long and edge case paths. We arrange to split between the C and the Cedilla if NAME_MAX is 255.
let longEdgeCase1 = tempDir.appending(component: String(repeating: "Façade! ", count: Int(NAME_MAX)).decomposedStringWithCanonicalMapping)
try localFileSystem.withLock(on: longEdgeCase1, type: .exclusive, {})
let longEdgeCase2 = tempDir.appending(component: String(repeating: "🏁", count: Int(NAME_MAX)).decomposedStringWithCanonicalMapping)
try localFileSystem.withLock(on: longEdgeCase2, type: .exclusive, {})
#endif
}
}

Expand Down