@@ -1555,6 +1555,7 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
15551555 func waitForCompletion( buildSucceeded: Bool ) async {
15561556 let completionToken = await dynamicOperationContext. waitForCompletion ( )
15571557 cleanupCompilationCache ( )
1558+ cleanupGlobalModuleCache ( )
15581559
15591560 await queue. sync {
15601561 self . isCompleted = true
@@ -1601,6 +1602,33 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
16011602 }
16021603 }
16031604
1605+ func cleanupGlobalModuleCache( ) {
1606+ let settings = operation. requestContext. getCachedSettings ( operation. request. parameters)
1607+ if settings. globalScope. evaluate ( BuiltinMacros . KEEP_GLOBAL_MODULE_CACHE_DIRECTORY) {
1608+ return // Keep the cache directory.
1609+ }
1610+
1611+ let cachePath = settings. globalScope. evaluate ( BuiltinMacros . MODULE_CACHE_DIR)
1612+ guard !cachePath. isEmpty, operation. fs. exists ( cachePath) else {
1613+ return
1614+ }
1615+
1616+ let signatureCtx = InsecureHashContext ( )
1617+ signatureCtx. add ( string: " CleanupGlobalModuleCache " )
1618+ signatureCtx. add ( string: cachePath. str)
1619+ let signature = signatureCtx. signature
1620+
1621+ withActivity ( ruleInfo: " CleanupGlobalModuleCache \( cachePath. str) " , executionDescription: " Cleanup global module cache at \( cachePath) " , signature: signature, target: nil , parentActivity: nil ) { activity in
1622+ do {
1623+ try operation. fs. removeDirectory ( cachePath)
1624+ } catch {
1625+ // Log error but do not fail the build.
1626+ emit ( diagnostic: Diagnostic . init ( behavior: . warning, location: . unknown, data: DiagnosticData ( " Failed to remove \( cachePath) : \( error. localizedDescription) " ) ) , for: activity, signature: signature)
1627+ }
1628+ return . succeeded
1629+ }
1630+ }
1631+
16041632 /// Get the active output delegate for an executing command.
16051633 ///
16061634 /// - returns: The active delegate, or nil if not found.
0 commit comments