Skip to content

Free, open-source macOS cleaner. CleanMyMac alternative with zero telemetry. Native SwiftUI, scheduled auto-cleaning, Xcode/Homebrew/system cache cleanup. MIT licensed.

fix(xcode): move the legacy XcodeBuildMCP DerivedData root to the Trash

momenbasel24a10a45b429
diff --git a/PureMac/Logic/Utilities/XcodeBuildMCPDerivedDataSupport.swift b/PureMac/Logic/Utilities/XcodeBuildMCPDerivedDataSupport.swift
index 46d7b81..22be597 100644
--- a/PureMac/Logic/Utilities/XcodeBuildMCPDerivedDataSupport.swift
+++ b/PureMac/Logic/Utilities/XcodeBuildMCPDerivedDataSupport.swift
@@ -158,7 +158,7 @@ enum XcodeBuildMCPDerivedDataSupport {
/// when XcodeBuildMCP (or another PureMac process) wins the race.
///
/// A legacy shared DerivedData path has no workspace lock, so it returns
- /// nil and must not be deleted by a caller that requires coordination.
+ /// nil; callers move that root to the Trash without coordination.
static func acquireLifecycleLock(
forManagedDerivedDataPath path: String,
homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser,
diff --git a/PureMac/Services/CleaningEngine.swift b/PureMac/Services/CleaningEngine.swift
index eb4412b..bdf3e9c 100644
--- a/PureMac/Services/CleaningEngine.swift
+++ b/PureMac/Services/CleaningEngine.swift
@@ -170,21 +170,33 @@ actor CleaningEngine {
homeDirectory: homeDirectory
)
if isManagedDerivedData {
- guard let lifecycleLock = XcodeBuildMCPDerivedDataSupport.acquireLifecycleLock(
+ // The legacy shared root predates XcodeBuildMCP's
+ // per-workspace lock, so only workspace DerivedData waits
+ // for the lock before moving to the Trash.
+ var lifecycleLock: XcodeBuildMCPDerivedDataSupport.LifecycleLock?
+ if XcodeBuildMCPDerivedDataSupport.workspaceKey(
forManagedDerivedDataPath: resolved,
- homeDirectory: homeDirectory,
- fileManager: fileManager
- ) else {
- let detail = "Skipped XcodeBuildMCP DerivedData: lifecycle lock is unavailable: \(item.path)"
- Logger.shared.log(detail, level: .warning)
- result.errors.append(detail)
- continue
+ homeDirectory: homeDirectory
+ ) != nil {
+ guard let acquired = XcodeBuildMCPDerivedDataSupport.acquireLifecycleLock(
+ forManagedDerivedDataPath: resolved,
+ homeDirectory: homeDirectory,
+ fileManager: fileManager
+ ) else {
+ let detail = "Skipped XcodeBuildMCP DerivedData: lifecycle lock is unavailable: \(item.path)"
+ Logger.shared.log(detail, level: .warning)
+ result.errors.append(detail)
+ continue
+ }
+ lifecycleLock = acquired
}
defer {
- XcodeBuildMCPDerivedDataSupport.releaseLifecycleLock(
- lifecycleLock,
- fileManager: fileManager
- )
+ if let lifecycleLock {
+ XcodeBuildMCPDerivedDataSupport.releaseLifecycleLock(
+ lifecycleLock,
+ fileManager: fileManager
+ )
+ }
}
// PureMac's safety contract is Trash-only for recoverable
// cleanup. XcodeBuildMCP DerivedData must not be unlinked.
diff --git a/PureMacTests/XcodeBuildMCPDerivedDataSupportTests.swift b/PureMacTests/XcodeBuildMCPDerivedDataSupportTests.swift
index d9c0768..2988a84 100644
--- a/PureMacTests/XcodeBuildMCPDerivedDataSupportTests.swift
+++ b/PureMacTests/XcodeBuildMCPDerivedDataSupportTests.swift
@@ -136,6 +136,32 @@ final class XcodeBuildMCPDerivedDataSupportTests: XCTestCase {
XCTAssertFalse(item.isSelected)
}
+ func testCleaningLegacySharedDerivedDataMovesToTrashWithoutWorkspaceLock() async throws {
+ let fileManager = FileManager.default
+ let temporaryHome = fileManager.temporaryDirectory
+ .appendingPathComponent("PureMac-XcodeBuildMCP-LegacyClean-\(UUID().uuidString)", isDirectory: true)
+ defer { try? fileManager.removeItem(at: temporaryHome) }
+
+ let managedRoot = temporaryHome
+ .appendingPathComponent("Library/Developer/XcodeBuildMCP", isDirectory: true)
+ let legacyDerivedData = managedRoot.appendingPathComponent("DerivedData", isDirectory: true)
+ try fileManager.createDirectory(at: legacyDerivedData, withIntermediateDirectories: true)
+ try Data(repeating: 0xEF, count: 4_096)
+ .write(to: legacyDerivedData.appendingPathComponent("legacy-build-product"))
+
+ let scan = await ScanEngine(homeDirectory: temporaryHome).scanCategory(.xcodeJunk)
+ let item = try XCTUnwrap(scan.items.first { $0.name == "XcodeBuildMCP: Legacy DerivedData" })
+ let result = await CleaningEngine(homeDirectory: temporaryHome).cleanItems([item]) { _ in }
+
+ XCTAssertEqual(result.itemsCleaned, 1)
+ XCTAssertTrue(result.errors.isEmpty)
+ XCTAssertFalse(fileManager.fileExists(atPath: legacyDerivedData.path))
+ XCTAssertFalse(fileManager.fileExists(atPath: managedRoot.appendingPathComponent("locks").path))
+ let trashedPath = try XCTUnwrap(result.trashedPaths.first)
+ XCTAssertTrue(fileManager.fileExists(atPath: trashedPath))
+ try? fileManager.removeItem(atPath: trashedPath)
+ }
+
func testCleaningManagedDerivedDataLeavesWorkspaceStateIntact() async throws {
try await assertCleaningManagedDerivedDataLeavesWorkspaceStateIntact(tool: "XcodeBuildMCP")
}