Skip to content

Commit

Permalink
Fixed Issue when Caching of SwiftTemplate Binary Failes (#1323)
Browse files Browse the repository at this point in the history
* Fixed issue when caching compiled binary

* Removed commented code
  • Loading branch information
art-divin committed Apr 14, 2024
1 parent e9e1875 commit f795e49
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
13 changes: 5 additions & 8 deletions SourcerySwift/Sources/SwiftTemplate.swift
Expand Up @@ -202,16 +202,13 @@ open class SwiftTemplate {
Log.verbose("Reusing built SwiftTemplate binary for SwiftTemplate with cache key: \(hash)...")
} else {
Log.verbose("Building new SwiftTemplate binary for SwiftTemplate...")
try? cachePath.delete() // clear old cache
try cachePath.mkdir()
do {
try build().move(binaryPath)
let path = try build()
try? cachePath.delete() // clear old cache
try? cachePath.mkdir()
try? path.move(binaryPath)
} catch let error as NSError {
if error.domain == "NSCocoaErrorDomain", error.code == 516 {
Log.warning("This error can be ignored: Attempt to copy `SwiftTemplate` binary to \(binaryPath) failed. Probably multiple Sourcery processes trying to cache into the same directory.")
} else {
throw error
}
throw error
}
}
} else {
Expand Down
37 changes: 31 additions & 6 deletions SourceryTests/Generating/SwiftTemplateSpecs.swift
Expand Up @@ -199,14 +199,8 @@ class SwiftTemplateTests: QuickSpec {
}

context("with existing cache") {
// beforeEach {

// }

context("and missing build dir") {
#if canImport(ObjectiveC)
expect { try Sourcery(cacheDisabled: false).processFiles(.sources(Paths(include: [Stubs.sourceDirectory])), usingTemplates: Paths(include: [templatePath]), output: output, baseIndentation: 0) }.toNot(throwError())
#endif
expect((try? (outputDir + Sourcery().generatedPath(for: templatePath)).read(.utf8))).to(equal(expectedResult))
guard let buildDir = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("SwiftTemplate").map({ Path($0.path) }) else {
fail("Could not create buildDir path")
Expand All @@ -228,6 +222,37 @@ class SwiftTemplateTests: QuickSpec {
let result = (try? (outputDir + Sourcery().generatedPath(for: templatePath)).read(.utf8))
expect(result).to(equal(expectedResult))
}

it("generates the code asynchronously without throwing error") {
let iterations = 2
let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true) as [String]
let path = paths[0]
let caches = Path(path) + Path("Sourcery")
try? caches.delete()
@Sendable func generateCode() async throws -> Int {
expect { try Sourcery(cacheDisabled: false).processFiles(.sources(Paths(include: [Stubs.sourceDirectory])), usingTemplates: Paths(include: [templatePath]), output: output, baseIndentation: 0) }.toNot(throwError())
let result = (try? (outputDir + Sourcery().generatedPath(for: templatePath)).read(.utf8))
expect(result).to(equal(expectedResult))
return 1
}
let semaphore = DispatchSemaphore(value: 0)
Task {
_ = try await withThrowingTaskGroup(of: Int.self) { taskGroup in
for _ in 0 ..< iterations {
taskGroup.addTask {
try await generateCode()
}
}
var counter = 0
for try await _ in taskGroup {
counter += 1
}
return counter
}
semaphore.signal()
}
semaphore.wait()
}
}
}

Expand Down

0 comments on commit f795e49

Please sign in to comment.