Skip to content

[6.0] Fix cache not cleaned up if download fails #7671

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

Merged
merged 2 commits into from
Jun 19, 2024
Merged
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
4 changes: 4 additions & 0 deletions Sources/SPMTestSupport/MockWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public final class MockWorkspace {
return self.sandbox.appending(components: ".build", "artifacts")
}

public var workspaceLocation: Workspace.Location? {
return self._workspace?.location
}

public func pathToRoot(withName name: String) throws -> AbsolutePath {
return try AbsolutePath(validating: name, relativeTo: self.rootsDir)
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Workspace/Workspace+BinaryArtifacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@ extension Workspace {
progress: progress,
completion: { result in
self.delegate?.willDownloadBinaryArtifact(from: artifact.url.absoluteString, fromCache: false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it already downloaded at this point?

if case .failure = result {
try? self.fileSystem.removeFileTree(cachedArtifactPath)
}
completion(result.flatMap {
Result.init(catching: {
// copy from cache to destination
Expand Down
14 changes: 13 additions & 1 deletion Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7374,6 +7374,7 @@ final class WorkspaceTests: XCTestCase {
let fs = InMemoryFileSystem()
let sandbox = AbsolutePath("/tmp/ws/")
try fs.createDirectory(sandbox, recursive: true)
let artifactUrl = "https://a.com/a.zip"

let httpClient = LegacyHTTPClient(handler: { request, _, completion in
do {
Expand Down Expand Up @@ -7404,7 +7405,7 @@ final class WorkspaceTests: XCTestCase {
MockTarget(
name: "A1",
type: .binary,
url: "https://a.com/a.zip",
url: artifactUrl,
checksum: "a1"
),
]
Expand All @@ -7429,6 +7430,17 @@ final class WorkspaceTests: XCTestCase {
// make sure artifact downloaded is deleted
XCTAssertTrue(fs.isDirectory(AbsolutePath("/tmp/ws/.build/artifacts/root")))
XCTAssertFalse(fs.exists(AbsolutePath("/tmp/ws/.build/artifacts/root/a.zip")))

// make sure the cached artifact is also deleted
let artifactCacheKey = artifactUrl.spm_mangledToC99ExtendedIdentifier()
guard let cachePath = workspace.workspaceLocation?
.sharedBinaryArtifactsCacheDirectory?
.appending(artifactCacheKey) else {
XCTFail("Required workspace location wasn't found")
return
}

XCTAssertFalse(fs.exists(cachePath))
}

func testArtifactDownloaderOrArchiverError() throws {
Expand Down