From 3594df2de358346df676c88df685ddfc5b668f20 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 11 Oct 2022 17:35:44 -0700 Subject: [PATCH] Fix parallel mode on larger projects Since the parallel mode requires the count of the files that it's going to process, the application of `openAndPrepareFile` was not lazy, this meant that swift-format immediately opened all the files in the project, which because of macOS's low open file limit quickly results in "too many open files". This changes the logic to only open the file as needed instead. This shouldn't be any different from before as far as file discovery, since we were already resolving everything to fetch the count. --- Sources/swift-format/Frontend/Frontend.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/swift-format/Frontend/Frontend.swift b/Sources/swift-format/Frontend/Frontend.swift index fa9611fac..44b75b722 100644 --- a/Sources/swift-format/Frontend/Frontend.swift +++ b/Sources/swift-format/Frontend/Frontend.swift @@ -131,9 +131,11 @@ class Frontend { "processURLs(_:) should only be called when 'urls' is non-empty.") if parallel { - let filesToProcess = FileIterator(urls: urls).compactMap(openAndPrepareFile) + let filesToProcess = Array(FileIterator(urls: urls)) DispatchQueue.concurrentPerform(iterations: filesToProcess.count) { index in - processFile(filesToProcess[index]) + if let file = openAndPrepareFile(at: filesToProcess[index]) { + processFile(file) + } } } else { FileIterator(urls: urls).lazy.compactMap(openAndPrepareFile).forEach(processFile)