Skip to content

Commit 9f5552c

Browse files
committed
Prevent filespace of workspace files
1 parent aa59c01 commit 9f5552c

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

Tool/Sources/Workspace/Workspace.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public final class Workspace {
6666
"Can't find workspace."
6767
}
6868
}
69+
70+
public struct CantFindFileError: Error, LocalizedError {
71+
public var fileURL: URL
72+
public var errorDescription: String? {
73+
"Can't find \(fileURL)."
74+
}
75+
}
6976

7077
private var additionalProperties = WorkspacePropertyValues()
7178
public internal(set) var plugins = [ObjectIdentifier: WorkspacePlugin]()
@@ -107,7 +114,7 @@ public final class Workspace {
107114
let openedFiles = openedFileRecoverableStorage.openedFiles
108115
Task { @WorkspaceActor in
109116
for fileURL in openedFiles {
110-
_ = createFilespaceIfNeeded(fileURL: fileURL)
117+
_ = try? createFilespaceIfNeeded(fileURL: fileURL)
111118
}
112119
}
113120
}
@@ -117,7 +124,19 @@ public final class Workspace {
117124
}
118125

119126
@WorkspaceActor
120-
public func createFilespaceIfNeeded(fileURL: URL) -> Filespace {
127+
public func createFilespaceIfNeeded(fileURL: URL) throws -> Filespace {
128+
let extensionName = fileURL.pathExtension
129+
if ["xcworkspace", "xcodeproj"].contains(extensionName) {
130+
throw UnsupportedFileError(extensionName: extensionName)
131+
}
132+
var isDirectory: ObjCBool = false
133+
if !FileManager.default.fileExists(atPath: fileURL.path, isDirectory: &isDirectory) {
134+
throw CantFindFileError(fileURL: fileURL)
135+
}
136+
if isDirectory.boolValue {
137+
throw UnsupportedFileError(extensionName: extensionName)
138+
}
139+
121140
let existedFilespace = filespaces[fileURL]
122141
let filespace = existedFilespace ?? .init(
123142
fileURL: fileURL,

Tool/Sources/Workspace/WorkspacePool.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public class WorkspacePool {
9292
if let currentWorkspaceURL = await XcodeInspector.shared.safe.realtimeActiveWorkspaceURL {
9393
if let existed = workspaces[currentWorkspaceURL] {
9494
// Reuse the existed workspace.
95-
let filespace = existed.createFilespaceIfNeeded(fileURL: fileURL)
95+
let filespace = try existed.createFilespaceIfNeeded(fileURL: fileURL)
9696
return (existed, filespace)
9797
}
9898

9999
let new = createNewWorkspace(workspaceURL: currentWorkspaceURL)
100100
workspaces[currentWorkspaceURL] = new
101-
let filespace = new.createFilespaceIfNeeded(fileURL: fileURL)
101+
let filespace = try new.createFilespaceIfNeeded(fileURL: fileURL)
102102
return (new, filespace)
103103
}
104104

@@ -132,7 +132,7 @@ public class WorkspacePool {
132132
return createNewWorkspace(workspaceURL: workspaceURL)
133133
}()
134134

135-
let filespace = workspace.createFilespaceIfNeeded(fileURL: fileURL)
135+
let filespace = try workspace.createFilespaceIfNeeded(fileURL: fileURL)
136136
workspaces[workspaceURL] = workspace
137137
workspace.refreshUpdateTime()
138138
return (workspace, filespace)

Tool/Sources/WorkspaceSuggestionService/Workspace+SuggestionService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public extension Workspace {
3333
) async throws -> [CodeSuggestion] {
3434
refreshUpdateTime()
3535

36-
let filespace = createFilespaceIfNeeded(fileURL: fileURL)
36+
let filespace = try createFilespaceIfNeeded(fileURL: fileURL)
3737

3838
guard !(await filespace.isGitIgnored) else { return [] }
3939

0 commit comments

Comments
 (0)