Skip to content

Commit 5ce858f

Browse files
committed
Add a fallback solution for fetchCurrentProjectRootURL
1 parent aeb5870 commit 5ce858f

4 files changed

Lines changed: 20 additions & 6 deletions

File tree

Core/Sources/Service/AutoTrigger.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ actor AutoTrigger {
2222
triggerTask = Task { @ServiceActor in
2323
try? await Task.sleep(nanoseconds: 2_000_000_000)
2424
if Task.isCancelled { return }
25-
guard let folderURL = try? await Environment.fetchCurrentProjectRootURL(),
25+
let fileURL = try? await Environment.fetchCurrentFileURL()
26+
guard let folderURL = try? await Environment.fetchCurrentProjectRootURL(fileURL),
2627
let workspace = workspaces[folderURL],
2728
workspace.isRealtimeSuggestionEnabled
2829
else { return }

Core/Sources/Service/Environment.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private struct FailedToFetchFileURLError: Error, LocalizedError {
1717
enum Environment {
1818
static var now = { Date() }
1919

20-
static var fetchCurrentProjectRootURL: () async throws -> URL? = {
20+
static var fetchCurrentProjectRootURL: (_ fileURL: URL?) async throws -> URL? = { fileURL in
2121
let appleScript = """
2222
tell application "Xcode"
2323
return path of document of the first window
@@ -34,7 +34,20 @@ enum Environment {
3434
}
3535
return url
3636
}
37-
return nil
37+
38+
guard var currentURL = fileURL else { return nil }
39+
var firstDirectoryURL: URL? = nil
40+
while currentURL.pathComponents.count > 1 {
41+
defer { currentURL.deleteLastPathComponent() }
42+
guard FileManager.default.fileIsDirectory(atPath: currentURL.path) else { continue }
43+
if firstDirectoryURL == nil { firstDirectoryURL = currentURL }
44+
let gitURL = currentURL.appendingPathComponent(".git")
45+
if FileManager.default.fileIsDirectory(atPath: gitURL.path) {
46+
return currentURL
47+
}
48+
}
49+
50+
return firstDirectoryURL ?? fileURL
3851
}
3952

4053
static var fetchCurrentFileURL: () async throws -> URL = {

Core/Sources/Service/Helpers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import Foundation
33
extension FileManager {
44
func fileIsDirectory(atPath path: String) -> Bool {
55
var isDirectory: ObjCBool = false
6-
fileExists(atPath: path, isDirectory: &isDirectory)
7-
return isDirectory.boolValue
6+
let exists = fileExists(atPath: path, isDirectory: &isDirectory)
7+
return isDirectory.boolValue && exists
88
}
99
}
1010

Core/Sources/Service/XPCService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public class XPCService: NSObject, XPCServiceProtocol {
271271
extension XPCService {
272272
@ServiceActor
273273
func fetchOrCreateWorkspaceIfNeeded(fileURL: URL) async throws -> Workspace {
274-
let projectURL = try await Environment.fetchCurrentProjectRootURL()
274+
let projectURL = try await Environment.fetchCurrentProjectRootURL(fileURL)
275275
let workspaceURL = projectURL ?? fileURL
276276
let workspace = workspaces[workspaceURL] ?? Workspace(projectRootURL: workspaceURL)
277277
workspaces[workspaceURL] = workspace

0 commit comments

Comments
 (0)