Skip to content

Commit 5e953fe

Browse files
Fix initial manifest caching with nested scratch dir and local cache (#8088)
### Motivation: When `--scratch-path` is set to a directory whose parent directory does not exist and `--manifest-cache local` is set, the initial manifest cache creation fails with the following warning: ``` warning: 'tmp.vulugm1mqv': failed loading cached manifest for 'tmp.vulugm1mqv': Error Domain=NSCocoaErrorDomain Code=4 "The file “check” doesn’t exist." UserInfo={NSFilePath=/private/var/folders/k2/q6yzck31253_q1j4kxkzq5jh0000gn/T/tmp.vUlUgM1Mqv/.build/check, NSUnderlyingError=0x600001072ac0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} warning: 'tmp.vulugm1mqv': failed storing manifest for 'tmp.vulugm1mqv' in cache: Error Domain=NSCocoaErrorDomain Code=4 "The file “check” doesn’t exist." UserInfo={NSFilePath=/private/var/folders/k2/q6yzck31253_q1j4kxkzq5jh0000gn/T/tmp.vUlUgM1Mqv/.build/check, NSUnderlyingError=0x600001078060 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} warning: 'tmp.vulugm1mqv': failed closing manifest db cache: Error Domain=NSCocoaErrorDomain Code=4 "The file “check” doesn’t exist." UserInfo={NSFilePath=/private/var/folders/k2/q6yzck31253_q1j4kxkzq5jh0000gn/T/tmp.vUlUgM1Mqv/.build/check, NSUnderlyingError=0x600001078360 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} ``` This is because directory creation in the manifest.db file creation process is not recursive. ### Modifications: This patch fixes the issue by making the directory creation recursive. ### Result: The following command sequence should work without warnings. ```console $ swift package init $ swift package dump-package --manifest-cache local --scratch-path .build/check ```
1 parent 0b569a1 commit 5e953fe

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Sources/Basics/SQLiteBackedCache.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ package final class SQLiteBackedCache<Value: Codable>: Closable {
238238
switch self.location {
239239
case .path(let path):
240240
if !self.fileSystem.exists(path.parentDirectory) {
241-
try self.fileSystem.createDirectory(path.parentDirectory)
241+
try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
242242
}
243243
return try self.fileSystem.withLock(on: path, type: .exclusive, body)
244244
case .memory, .temporary:

Tests/BasicsTests/SQLiteBackedCacheTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ final class SQLiteBackedCacheTests: XCTestCase {
182182
}
183183
}
184184
}
185+
186+
func testInitialFileCreation() throws {
187+
try testWithTemporaryDirectory { tmpPath in
188+
let paths = [
189+
tmpPath.appending("foo", "test.db"),
190+
// Ensure it works recursively.
191+
tmpPath.appending("bar", "baz", "test.db"),
192+
]
193+
194+
for path in paths {
195+
let cache = SQLiteBackedCache<String>(tableName: "SQLiteBackedCacheTest", path: path)
196+
// Put an entry to ensure the file is created.
197+
XCTAssertNoThrow(try cache.put(key: "foo", value: "bar"))
198+
XCTAssertNoThrow(try cache.close())
199+
XCTAssertTrue(localFileSystem.exists(path), "expected file to be created at \(path)")
200+
}
201+
}
202+
}
185203
}
186204

187205
private func makeMockData(fileSystem: FileSystem, rootPath: AbsolutePath, count: Int = Int.random(in: 50 ..< 100)) throws -> [String: String] {

0 commit comments

Comments
 (0)