Skip to content

Commit a864391

Browse files
authored
Merge pull request swiftlang#1832 from ahoppen/reduce-absolute-path
2 parents 8eb5bfc + be54630 commit a864391

File tree

52 files changed

+1218
-1104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1218
-1104
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ var targets: [Target] = [
9797
"SKTestSupport",
9898
"SourceKitLSP",
9999
"ToolchainRegistry",
100+
"TSCExtensions",
100101
],
101102
swiftSettings: globalSwiftSettings
102103
),
@@ -348,7 +349,6 @@ var targets: [Target] = [
348349
"Csourcekitd",
349350
"SKLogging",
350351
"SwiftExtensions",
351-
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
352352
],
353353
exclude: ["CMakeLists.txt", "sourcekitd_uids.swift.gyb"],
354354
swiftSettings: globalSwiftSettings

Sources/BuildSystemIntegration/BuildSystemManager.swift

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#if compiler(>=6)
1414
package import BuildServerProtocol
1515
import Dispatch
16-
import Foundation
16+
package import Foundation
1717
package import LanguageServerProtocol
1818
package import LanguageServerProtocolExtensions
1919
import SKLogging
@@ -23,8 +23,7 @@ package import SwiftExtensions
2323
package import ToolchainRegistry
2424
import TSCExtensions
2525

26-
package import struct TSCBasic.AbsolutePath
27-
package import struct TSCBasic.RelativePath
26+
import struct TSCBasic.RelativePath
2827
#else
2928
import BuildServerProtocol
3029
import Dispatch
@@ -38,7 +37,6 @@ import SwiftExtensions
3837
import ToolchainRegistry
3938
import TSCExtensions
4039

41-
import struct TSCBasic.AbsolutePath
4240
import struct TSCBasic.RelativePath
4341
#endif
4442

@@ -138,31 +136,31 @@ private enum BuildSystemAdapter {
138136

139137
private extension BuildSystemSpec {
140138
private static func createBuiltInBuildSystemAdapter(
141-
projectRoot: AbsolutePath,
139+
projectRoot: URL,
142140
messagesToSourceKitLSPHandler: any MessageHandler,
143141
buildSystemTestHooks: BuildSystemTestHooks,
144142
_ createBuildSystem: @Sendable (_ connectionToSourceKitLSP: any Connection) async throws -> BuiltInBuildSystem?
145143
) async -> BuildSystemAdapter? {
146144
let connectionToSourceKitLSP = LocalConnection(
147-
receiverName: "BuildSystemManager for \(projectRoot.asURL.lastPathComponent)"
145+
receiverName: "BuildSystemManager for \(projectRoot.lastPathComponent)"
148146
)
149147
connectionToSourceKitLSP.start(handler: messagesToSourceKitLSPHandler)
150148

151149
let buildSystem = await orLog("Creating build system") {
152150
try await createBuildSystem(connectionToSourceKitLSP)
153151
}
154152
guard let buildSystem else {
155-
logger.log("Failed to create build system at \(projectRoot.pathString)")
153+
logger.log("Failed to create build system at \(projectRoot)")
156154
return nil
157155
}
158-
logger.log("Created \(type(of: buildSystem), privacy: .public) at \(projectRoot.pathString)")
156+
logger.log("Created \(type(of: buildSystem), privacy: .public) at \(projectRoot)")
159157
let buildSystemAdapter = BuiltInBuildSystemAdapter(
160158
underlyingBuildSystem: buildSystem,
161159
connectionToSourceKitLSP: connectionToSourceKitLSP,
162160
buildSystemTestHooks: buildSystemTestHooks
163161
)
164162
let connectionToBuildSystem = LocalConnection(
165-
receiverName: "\(type(of: buildSystem)) for \(projectRoot.asURL.lastPathComponent)"
163+
receiverName: "\(type(of: buildSystem)) for \(projectRoot.lastPathComponent)"
166164
)
167165
connectionToBuildSystem.start(handler: buildSystemAdapter)
168166
return .builtIn(buildSystemAdapter, connectionToBuildSystem: connectionToBuildSystem)
@@ -185,10 +183,10 @@ private extension BuildSystemSpec {
185183
)
186184
}
187185
guard let buildSystem else {
188-
logger.log("Failed to create external build system at \(projectRoot.pathString)")
186+
logger.log("Failed to create external build system at \(projectRoot)")
189187
return nil
190188
}
191-
logger.log("Created external build server at \(projectRoot.pathString)")
189+
logger.log("Created external build server at \(projectRoot)")
192190
return .external(buildSystem)
193191
case .compilationDatabase:
194192
return await Self.createBuiltInBuildSystemAdapter(
@@ -245,7 +243,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
245243
/// For compilation databases it is the root folder based on which the compilation database was found.
246244
///
247245
/// `nil` if the `BuildSystemManager` does not have an underlying build system.
248-
package let projectRoot: AbsolutePath?
246+
package let projectRoot: URL?
249247

250248
/// The files for which the delegate has requested change notifications, ie. the files for which the delegate wants to
251249
/// get `fileBuildSettingsChanged` and `filesDependenciesUpdated` callbacks.
@@ -402,7 +400,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
402400
displayName: "SourceKit-LSP",
403401
version: "",
404402
bspVersion: "2.2.0",
405-
rootUri: URI(buildSystemSpec.projectRoot.asURL),
403+
rootUri: URI(buildSystemSpec.projectRoot),
406404
capabilities: BuildClientCapabilities(languageIds: [.c, .cpp, .objective_c, .objective_cpp, .swift])
407405
)
408406
)
@@ -608,7 +606,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
608606
in target: BuildTargetIdentifier?,
609607
language: Language
610608
) async -> Toolchain? {
611-
let toolchainPath = await orLog("Getting toolchain from build targets") { () -> AbsolutePath? in
609+
let toolchainPath = await orLog("Getting toolchain from build targets") { () -> URL? in
612610
guard let target else {
613611
return nil
614612
}
@@ -624,7 +622,7 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
624622
logger.error("Toolchain is not a file URL")
625623
return nil
626624
}
627-
return try AbsolutePath(validating: toolchainUrl.filePath)
625+
return toolchainUrl
628626
}
629627
if let toolchainPath {
630628
if let toolchain = await self.toolchainRegistry.toolchain(withPath: toolchainPath) {
@@ -681,11 +679,9 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
681679
if let targets = filesAndDirectories.files[document]?.targets {
682680
result.formUnion(targets)
683681
}
684-
if !filesAndDirectories.directories.isEmpty,
685-
let documentPath = AbsolutePath(validatingOrNil: try? document.fileURL?.filePath)
686-
{
682+
if !filesAndDirectories.directories.isEmpty, let documentPath = document.fileURL {
687683
for (directory, info) in filesAndDirectories.directories {
688-
guard let directoryPath = AbsolutePath(validatingOrNil: try? directory.fileURL?.filePath) else {
684+
guard let directoryPath = directory.fileURL else {
689685
continue
690686
}
691687
if documentPath.isDescendant(of: directoryPath) {

Sources/BuildSystemIntegration/BuiltInBuildSystem.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,18 @@
1212

1313
#if compiler(>=6)
1414
package import BuildServerProtocol
15+
package import Foundation
1516
package import LanguageServerProtocol
1617
import SKLogging
1718
import SKOptions
1819
import ToolchainRegistry
19-
20-
package import struct TSCBasic.AbsolutePath
21-
package import struct TSCBasic.RelativePath
2220
#else
2321
import BuildServerProtocol
22+
import Foundation
2423
import LanguageServerProtocol
2524
import SKLogging
2625
import SKOptions
2726
import ToolchainRegistry
28-
29-
import struct TSCBasic.AbsolutePath
30-
import struct TSCBasic.RelativePath
3127
#endif
3228

3329
/// An error build systems can throw from `prepare` if they don't support preparation of targets.
@@ -42,16 +38,16 @@ package protocol BuiltInBuildSystem: AnyObject, Sendable {
4238
/// The root of the project that this build system manages. For example, for SwiftPM packages, this is the folder
4339
/// containing Package.swift. For compilation databases it is the root folder based on which the compilation database
4440
/// was found.
45-
var projectRoot: AbsolutePath { get async }
41+
var projectRoot: URL { get async }
4642

4743
/// The files to watch for changes.
4844
var fileWatchers: [FileSystemWatcher] { get async }
4945

5046
/// The path to the raw index store data, if any.
51-
var indexStorePath: AbsolutePath? { get async }
47+
var indexStorePath: URL? { get async }
5248

5349
/// The path to put the index database, if any.
54-
var indexDatabasePath: AbsolutePath? { get async }
50+
var indexDatabasePath: URL? { get async }
5551

5652
/// Whether the build system is capable of preparing a target for indexing, ie. if the `prepare` methods has been
5753
/// implemented.

Sources/BuildSystemIntegration/BuiltInBuildSystemAdapter.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import BuildServerProtocol
14-
import Foundation
1514
import LanguageServerProtocol
1615
import LanguageServerProtocolExtensions
1716
import SKLogging
@@ -20,11 +19,9 @@ import SwiftExtensions
2019
import ToolchainRegistry
2120

2221
#if compiler(>=6)
23-
package import struct TSCBasic.AbsolutePath
24-
package import struct TSCBasic.RelativePath
22+
package import Foundation
2523
#else
26-
import struct TSCBasic.AbsolutePath
27-
import struct TSCBasic.RelativePath
24+
import Foundation
2825
#endif
2926

3027
/// The details necessary to create a `BuildSystemAdapter`.
@@ -38,9 +35,9 @@ package struct BuildSystemSpec {
3835

3936
package var kind: Kind
4037

41-
package var projectRoot: AbsolutePath
38+
package var projectRoot: URL
4239

43-
package init(kind: BuildSystemSpec.Kind, projectRoot: AbsolutePath) {
40+
package init(kind: BuildSystemSpec.Kind, projectRoot: URL) {
4441
self.kind = kind
4542
self.projectRoot = projectRoot
4643
}
@@ -95,8 +92,12 @@ actor BuiltInBuildSystemAdapter: QueueBasedMessageHandler {
9592
capabilities: BuildServerCapabilities(),
9693
dataKind: .sourceKit,
9794
data: SourceKitInitializeBuildResponseData(
98-
indexDatabasePath: await underlyingBuildSystem.indexDatabasePath?.pathString,
99-
indexStorePath: await underlyingBuildSystem.indexStorePath?.pathString,
95+
indexDatabasePath: await orLog("getting index database file path") {
96+
try await underlyingBuildSystem.indexDatabasePath?.filePath
97+
},
98+
indexStorePath: await orLog("getting index store file path") {
99+
try await underlyingBuildSystem.indexStorePath?.filePath
100+
},
100101
watchers: await underlyingBuildSystem.fileWatchers,
101102
prepareProvider: underlyingBuildSystem.supportsPreparation,
102103
sourceKitOptionsProvider: true

Sources/BuildSystemIntegration/CompilationDatabase.swift

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212

1313
#if compiler(>=6)
1414
package import BuildServerProtocol
15-
import Foundation
15+
package import Foundation
1616
package import LanguageServerProtocol
1717
import LanguageServerProtocolExtensions
1818
import SKLogging
1919
import SwiftExtensions
2020
import TSCExtensions
2121

22-
package import struct TSCBasic.AbsolutePath
23-
package import protocol TSCBasic.FileSystem
2422
package import struct TSCBasic.RelativePath
25-
package import var TSCBasic.localFileSystem
2623
#else
2724
import BuildServerProtocol
2825
import Foundation
@@ -32,10 +29,7 @@ import SKLogging
3229
import SwiftExtensions
3330
import TSCExtensions
3431

35-
import struct TSCBasic.AbsolutePath
36-
import protocol TSCBasic.FileSystem
3732
import struct TSCBasic.RelativePath
38-
import var TSCBasic.localFileSystem
3933
#endif
4034

4135
#if os(Windows)
@@ -124,9 +118,8 @@ package protocol CompilationDatabase {
124118

125119
/// Loads the compilation database located in `directory`, if one can be found in `additionalSearchPaths` or in the default search paths of "." and "build".
126120
package func tryLoadCompilationDatabase(
127-
directory: AbsolutePath,
128-
additionalSearchPaths: [RelativePath] = [],
129-
_ fileSystem: FileSystem = localFileSystem
121+
directory: URL,
122+
additionalSearchPaths: [RelativePath] = []
130123
) -> CompilationDatabase? {
131124
let searchPaths =
132125
additionalSearchPaths + [
@@ -140,10 +133,10 @@ package func tryLoadCompilationDatabase(
140133
.map { directory.appending($0) }
141134
.compactMap { searchPath in
142135
orLog("Failed to load compilation database") { () -> CompilationDatabase? in
143-
if let compDb = try JSONCompilationDatabase(directory: searchPath, fileSystem) {
136+
if let compDb = try JSONCompilationDatabase(directory: searchPath) {
144137
return compDb
145138
}
146-
if let compDb = try FixedCompilationDatabase(directory: searchPath, fileSystem) {
139+
if let compDb = try FixedCompilationDatabase(directory: searchPath) {
147140
return compDb
148141
}
149142
return nil
@@ -178,30 +171,26 @@ package struct FixedCompilationDatabase: CompilationDatabase, Equatable {
178171

179172
/// Loads the compilation database located in `directory`, if any.
180173
/// - Returns: `nil` if `compile_flags.txt` was not found
181-
package init?(directory: AbsolutePath, _ fileSystem: FileSystem = localFileSystem) throws {
182-
let path = directory.appending(component: "compile_flags.txt")
183-
try self.init(file: path, fileSystem)
174+
package init?(directory: URL) throws {
175+
let path = directory.appendingPathComponent("compile_flags.txt")
176+
try self.init(file: path)
184177
}
185178

186179
/// Loads the compilation database from `file`
187180
/// - Returns: `nil` if the file does not exist
188-
package init?(file: AbsolutePath, _ fileSystem: FileSystem = localFileSystem) throws {
189-
self.directory = file.dirname
181+
package init?(file: URL) throws {
182+
self.directory = try file.deletingLastPathComponent().filePath
190183

191-
guard fileSystem.exists(file) else {
184+
let fileContents: String
185+
do {
186+
fileContents = try String(contentsOf: file, encoding: .utf8)
187+
} catch {
192188
return nil
193189
}
194-
let bytes = try fileSystem.readFileContents(file)
195190

196191
var fixedArgs: [String] = ["clang"]
197-
try bytes.withUnsafeData { data in
198-
guard let fileContents = String(data: data, encoding: .utf8) else {
199-
throw CompilationDatabaseDecodingError.fixedDatabaseDecodingError
200-
}
201-
202-
fileContents.enumerateLines { line, _ in
203-
fixedArgs.append(line.trimmingCharacters(in: .whitespacesAndNewlines))
204-
}
192+
fileContents.enumerateLines { line, _ in
193+
fixedArgs.append(line.trimmingCharacters(in: .whitespacesAndNewlines))
205194
}
206195
self.fixedArgs = fixedArgs
207196
}
@@ -242,21 +231,21 @@ package struct JSONCompilationDatabase: CompilationDatabase, Equatable, Codable
242231
/// Loads the compilation database located in `directory`, if any.
243232
///
244233
/// - Returns: `nil` if `compile_commands.json` was not found
245-
package init?(directory: AbsolutePath, _ fileSystem: FileSystem = localFileSystem) throws {
246-
let path = directory.appending(component: "compile_commands.json")
247-
try self.init(file: path, fileSystem)
234+
package init?(directory: URL) throws {
235+
let path = directory.appendingPathComponent("compile_commands.json")
236+
try self.init(file: path)
248237
}
249238

250239
/// Loads the compilation database from `file`
251240
/// - Returns: `nil` if the file does not exist
252-
package init?(file: AbsolutePath, _ fileSystem: FileSystem = localFileSystem) throws {
253-
guard fileSystem.exists(file) else {
241+
package init?(file: URL) throws {
242+
let data: Data
243+
do {
244+
data = try Data(contentsOf: file)
245+
} catch {
254246
return nil
255247
}
256-
let bytes = try fileSystem.readFileContents(file)
257-
try bytes.withUnsafeData { data in
258-
self = try JSONDecoder().decode(JSONCompilationDatabase.self, from: data)
259-
}
248+
self = try JSONDecoder().decode(JSONCompilationDatabase.self, from: data)
260249
}
261250

262251
package func encode(to encoder: Encoder) throws {

0 commit comments

Comments
 (0)