Skip to content
Closed
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
16 changes: 13 additions & 3 deletions Tool/Sources/SystemUtils/SystemUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ public class SystemUtils {
}

public static func executeCommand(
inDirectory directory: String = NSHomeDirectory(),
path: String,
inDirectory directory: String = NSHomeDirectory(),
path: String,
arguments: [String]
) throws -> String? {
let task = Process()
Expand All @@ -216,11 +216,21 @@ public class SystemUtils {
task.arguments = arguments
task.standardOutput = pipe
task.currentDirectoryURL = URL(fileURLWithPath: directory)

var accumulatedOutput = Data()
pipe.fileHandleForReading.readabilityHandler = { handle in
let data = handle.availableData
guard data.isEmpty else { // End of file
return accumulatedOutput.append(data)
Copy link
Preview

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

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

The append method returns Void, but this is being used in a return statement. This line should just be accumulatedOutput.append(data) without the return keyword.

Suggested change
return accumulatedOutput.append(data)
accumulatedOutput.append(data)
return

Copilot uses AI. Check for mistakes.

}

handle.readabilityHandler = nil // Stop observing
Comment on lines +223 to +227
Copy link
Preview

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

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

The guard condition logic is inverted. When data.isEmpty is true, it indicates end of file, but the else block executes when data is NOT empty. This should be guard !data.isEmpty else { handle.readabilityHandler = nil; return } to properly handle the end-of-file condition.

Suggested change
guard data.isEmpty else { // End of file
return accumulatedOutput.append(data)
}
handle.readabilityHandler = nil // Stop observing
guard !data.isEmpty else { // End of file
handle.readabilityHandler = nil // Stop observing
return
}
accumulatedOutput.append(data)

Copilot uses AI. Check for mistakes.

}
Comment on lines +220 to +228
Copy link
Preview

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

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

The accumulatedOutput variable is accessed from both the main thread and the readability handler callback without synchronization. This creates a potential race condition. Consider using a concurrent queue with barriers or other synchronization mechanism to ensure thread-safe access.

Copilot uses AI. Check for mistakes.


try task.run()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8)
return String(data: accumulatedOutput + data, encoding: .utf8)
}

public func appendCommonBinPaths(path: String) -> String {
Expand Down