Skip to content

Commit 048d72b

Browse files
committed
Upgrade SwiftPM dependency to master branch
We are tied to using a SwiftPM that matches the toolchain, so upgrade from 0.3.0 to .branch("master") and add a pins file to manage updating that dependency. This was driven by changes that broke loading packages being developed with newer version of SwiftPM when using the 0.3.0 tag of libSwiftPM. However, the changes seem to work when going in the other direction, so using the newer libSwiftPM hasn't caused any known regressions for using older toolchain versions (not guarantteed and not tested extensively though). This fixes using the latest (November 13) toolchain snapshot on macOS. On Linux there are other issues not specific to LSP.
1 parent 4b77ff9 commit 048d72b

18 files changed

+118
-78
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
default.profraw
3-
Package.resolved
43
/.build
54
/Packages
65
/*.xcodeproj

Package.resolved

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let package = Package(
77
products: [
88
],
99
dependencies: [
10-
.package(url: "https://github.com/apple/swift-package-manager.git", "0.3.0"..<"0.4.0"),
10+
.package(url: "https://github.com/apple/swift-package-manager.git", .branch("master")),
1111
.package(url: "https://github.com/apple/indexstore-db.git", .branch("master")),
1212
],
1313
targets: [

Sources/LanguageServerProtocol/Diagnostic.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum DiagnosticCode: Hashable {
2525
public struct Diagnostic: Codable, Hashable {
2626

2727
/// The primary position/range of the diagnostic.
28-
public var range: Range<Position>
28+
public var range: PositionRange
2929

3030
/// Whether this is a warning, error, etc.
3131
public var severity: DiagnosticSeverity?
@@ -43,7 +43,7 @@ public struct Diagnostic: Codable, Hashable {
4343
public var relatedInformation: [DiagnosticRelatedInformation]?
4444

4545
public init(range: Range<Position>, severity: DiagnosticSeverity?, code: DiagnosticCode? = nil, source: String?, message: String, relatedInformation: [DiagnosticRelatedInformation]? = nil) {
46-
self.range = range
46+
self.range = PositionRange(range)
4747
self.severity = severity
4848
self.code = code
4949
self.source = source

Sources/LanguageServerProtocol/DocumentHighlight.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public enum DocumentHighlightKind: Int, Codable, Hashable {
1919
public struct DocumentHighlight: ResponseType, Hashable {
2020

2121
/// The location of the highlight.
22-
public var range: Range<Position>
22+
public var range: PositionRange
2323

2424
/// What kind of reference this is. Default is `.text`.
2525
public var kind: DocumentHighlightKind?
2626

2727
public init(range: Range<Position>, kind: DocumentHighlightKind?) {
28-
self.range = range
28+
self.range = PositionRange(range)
2929
self.kind = kind
3030
}
3131
}

Sources/LanguageServerProtocol/Location.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,18 @@
1313
/// Range within a particular document.
1414
///
1515
/// For a location where the document is implied, use `Position` or `Range<Position>`.
16-
public struct Location {
16+
public struct Location: Hashable {
1717

1818
public var url: URL
1919

20-
public var range: Range<Position>
20+
public var range: PositionRange
2121

2222
public init(url: URL, range: Range<Position>) {
2323
self.url = url
24-
self.range = range
24+
self.range = PositionRange(range)
2525
}
2626
}
2727

28-
extension Location: Equatable {}
29-
extension Location: Hashable {}
30-
3128
// Encode using the key "uri" to match LSP.
3229
extension Location: Codable {
3330
private enum CodingKeys: String, CodingKey {

Sources/LanguageServerProtocol/Messages.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public struct HoverResponse: ResponseType, Hashable {
309309

310310
public var contents: MarkupContent
311311

312-
public var range: Range<Position>?
312+
public var range: PositionRange?
313313

314314
/// Extension!
315315
public var usr: String?
@@ -319,7 +319,7 @@ public struct HoverResponse: ResponseType, Hashable {
319319

320320
public init(contents: MarkupContent, range: Range<Position>?, usr: String?, definition: Location?) {
321321
self.contents = contents
322-
self.range = range
322+
self.range = range.map { PositionRange($0) }
323323
self.usr = usr
324324
self.definition = definition
325325
}
@@ -378,7 +378,7 @@ public struct DocumentRangeFormatting: TextDocumentRequest, Hashable {
378378

379379
public var textDocument: TextDocumentIdentifier
380380

381-
public var range: Range<Position>
381+
public var range: PositionRange
382382

383383
public var options: FormattingOptions
384384
}

Sources/LanguageServerProtocol/Position.swift

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// Position within a text document, expressed as a zero-based line and column (utf-16 code unit offset).
14-
public struct Position {
14+
public struct Position: Hashable {
1515

1616
/// Line number within a document (zero-based).
1717
public var line: Int
@@ -25,8 +25,6 @@ public struct Position {
2525
}
2626
}
2727

28-
extension Position: Equatable {}
29-
extension Position: Hashable {}
3028
extension Position: Codable {
3129
private enum CodingKeys: String, CodingKey {
3230
case line
@@ -39,29 +37,3 @@ extension Position: Comparable {
3937
return (lhs.line, lhs.utf16index) < (rhs.line, rhs.utf16index)
4038
}
4139
}
42-
43-
// Encode Range<Position> using the keys "start" and "end" to match the LSP protocol for "Range".
44-
extension Range: Codable where Bound == Position {
45-
private enum CodingKeys: String, CodingKey {
46-
case lowerBound = "start"
47-
case upperBound = "end"
48-
}
49-
50-
/// Create a range for a single position.
51-
public init(_ pos: Position) {
52-
self = pos ..< pos
53-
}
54-
55-
public init(from decoder: Decoder) throws {
56-
let values = try decoder.container(keyedBy: CodingKeys.self)
57-
let lowerBound = try values.decode(Position.self, forKey: .lowerBound)
58-
let upperBound = try values.decode(Position.self, forKey: .upperBound)
59-
self = lowerBound ..< upperBound
60-
}
61-
62-
public func encode(to encoder: Encoder) throws {
63-
var container = encoder.container(keyedBy: CodingKeys.self)
64-
try container.encode(lowerBound, forKey: .lowerBound)
65-
try container.encode(upperBound, forKey: .upperBound)
66-
}
67-
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// Half-open range of Positions `[start, end)` within a text document, for use in LSP messages.
14+
///
15+
/// It is generally preferred to work with Range<Position> directly, but when passing values in LSP
16+
/// messages, use PositionRange to provide the correct (de)serialization.
17+
public struct PositionRange: Hashable {
18+
19+
/// The `lowerBound` of the range (inclusive).
20+
public var lowerBound: Position
21+
22+
/// The `upperBound` of the range (exclusive).
23+
public var upperBound: Position
24+
25+
/// The equivalent range expressed as a `Swift.Range`.
26+
public var asRange: Range<Position> { return lowerBound..<upperBound}
27+
28+
public init(_ range: Range<Position>) {
29+
self.lowerBound = range.lowerBound
30+
self.upperBound = range.upperBound
31+
}
32+
}
33+
34+
extension PositionRange: Codable {
35+
private enum CodingKeys: String, CodingKey {
36+
case lowerBound = "start"
37+
case upperBound = "end"
38+
}
39+
}
40+
41+
extension Range where Bound == Position {
42+
43+
/// Create a range for a single position.
44+
public init(_ pos: Position) {
45+
self = pos ..< pos
46+
}
47+
}

Sources/LanguageServerProtocol/TextDocumentContentChangeEvent.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
/// The `range.end` and `rangeLength` are potentially redundant. Based on https://github.com/Microsoft/language-server-protocol/issues/9, servers should be lenient and accept either.
1818
public struct TextDocumentContentChangeEvent: Codable, Hashable {
1919

20-
public var range: Range<Position>?
20+
public var range: PositionRange?
2121

2222
public var rangeLength: Int?
2323

2424
public var text: String
2525

2626
public init(range: Range<Position>? = nil, rangeLength: Int? = nil, text: String) {
27-
self.range = range
27+
self.range = range.map { PositionRange($0) }
2828
self.rangeLength = rangeLength
2929
self.text = text
3030
}

Sources/LanguageServerProtocol/TextEdit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
public struct TextEdit: Codable, Hashable {
1515

1616
/// The range of text to be replaced.
17-
var range: Range<Position>
17+
var range: PositionRange
1818

1919
/// The new text.
2020
var newText: String
2121

2222
public init(range: Range<Position>, newText: String) {
23-
self.range = range
23+
self.range = PositionRange(range)
2424
self.newText = newText
2525
}
2626
}

Sources/LanguageServerProtocolJSONRPC/MessageSplitting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SKSupport
1616
struct MessageHeader: Hashable {
1717
static let contentLengthKey: [UInt8] = [UInt8]("Content-Length".utf8)
1818
static let separator: [UInt8] = [UInt8]("\r\n".utf8)
19-
static let colon: UInt8 = ":".utf8.only!
19+
static let colon: UInt8 = ":".utf8.spm_only!
2020
static let invalidKeyBytes: [UInt8] = [colon] + separator
2121

2222
var contentLength: Int? = nil

Sources/SKCore/FallbackBuildSettingsProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public final class FallbackBuildSettingsProvider: BuildSettingsProvider {
1919

2020
lazy var sdkpath: AbsolutePath? = {
2121
if case .darwin? = Platform.currentPlatform {
22-
if let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx"), let path = try? AbsolutePath(validating: str.chomp()) {
22+
if let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx"), let path = try? AbsolutePath(validating: str.spm_chomp()) {
2323
return path
2424
}
2525
}

Sources/SKCore/ToolchainRegistry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ extension ToolchainRegistry {
141141
]
142142

143143
var currentXcodeDeveloperPath: AbsolutePath? {
144-
if let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcode-select", "-p"), let path = try? AbsolutePath(validating: str.chomp()) {
144+
if let str = try? Process.checkNonZeroExit(args: "/usr/bin/xcode-select", "-p"), let path = try? AbsolutePath(validating: str.spm_chomp()) {
145145
return path
146146
}
147147
return nil

Sources/SKSupport/Result.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,3 @@ extension Result {
3434
}
3535
}
3636
}
37-
38-
extension Result: Equatable where Value: Equatable, ErrorType: Equatable {
39-
40-
@inlinable
41-
public static func ==(lhs: Result, rhs: Result) -> Bool {
42-
switch (lhs, rhs) {
43-
case (.success(let lhs), .success(let rhs)):
44-
return lhs == rhs
45-
case (.failure(let lhs), .failure(let rhs)):
46-
return lhs == rhs
47-
default:
48-
return false
49-
}
50-
}
51-
}

Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public final class SwiftPMWorkspace {
3737
let toolchainRegistry: ToolchainRegistry
3838
let fs: FileSystem
3939

40-
var fileToTarget: [AbsolutePath: TargetDescription] = [:]
41-
var sourceDirToTarget: [AbsolutePath: TargetDescription] = [:]
40+
var fileToTarget: [AbsolutePath: TargetBuildDescription] = [:]
41+
var sourceDirToTarget: [AbsolutePath: TargetBuildDescription] = [:]
4242

4343
/// Creates a `BuildSettingsProvider` using the Swift Package Manager, if this workspace is part of a package.
4444
///
@@ -77,10 +77,10 @@ public final class SwiftPMWorkspace {
7777
let target: Triple = Triple.hostTriple
7878
if case .darwin? = Platform.currentPlatform {
7979
if let path = try? Process.checkNonZeroExit(args: "/usr/bin/xcrun", "--show-sdk-path", "--sdk", "macosx") {
80-
sdkpath = try? AbsolutePath(validating: path.chomp())
80+
sdkpath = try? AbsolutePath(validating: path.spm_chomp())
8181
}
8282
if let path = try? Process.checkNonZeroExit(args: "/usr/bin/xcrun", "--show-sdk-platform-path", "--sdk", "macosx") {
83-
platformPath = try? AbsolutePath(validating: path.chomp())
83+
platformPath = try? AbsolutePath(validating: path.spm_chomp())
8484
}
8585
}
8686

@@ -113,7 +113,7 @@ public final class SwiftPMWorkspace {
113113
dataPath: packageRoot.appending(component: ".build"),
114114
editablesPath: packageRoot.appending(component: "Packages"),
115115
pinsFile: packageRoot.appending(component: "Package.resolved"),
116-
manifestLoader: ManifestLoader(resources: swiftpmToolchain),
116+
manifestLoader: ManifestLoader(manifestResources: swiftpmToolchain),
117117
delegate: BuildSettingProviderWorkspaceDelegate(),
118118
fileSystem: fs,
119119
skipUpdate: true
@@ -137,7 +137,7 @@ public final class SwiftPMWorkspace {
137137

138138
let plan = try BuildPlan(buildParameters: buildParameters, graph: packageGraph, diagnostics: diags, fileSystem: self.fs)
139139

140-
self.fileToTarget = [AbsolutePath: TargetDescription](
140+
self.fileToTarget = [AbsolutePath: TargetBuildDescription](
141141
packageGraph.allTargets.flatMap { target in
142142
return target.sources.paths.compactMap {
143143
guard let td = plan.targetMap[target] else {
@@ -150,7 +150,7 @@ public final class SwiftPMWorkspace {
150150
return td
151151
})
152152

153-
self.sourceDirToTarget = [AbsolutePath: TargetDescription](
153+
self.sourceDirToTarget = [AbsolutePath: TargetBuildDescription](
154154
packageGraph.allTargets.compactMap { target in
155155
guard let td = plan.targetMap[target] else {
156156
return nil
@@ -204,7 +204,11 @@ extension SwiftPMWorkspace {
204204

205205
// MARK: Implementation details
206206

207-
public func settings(for path: AbsolutePath, language: Language, targetDescription td: TargetDescription) -> FileBuildSettings? {
207+
public func settings(
208+
for path: AbsolutePath,
209+
language: Language,
210+
targetDescription td: TargetBuildDescription
211+
) -> FileBuildSettings? {
208212

209213
let buildPath = self.buildPath
210214

@@ -300,12 +304,12 @@ extension SwiftPMWorkspace {
300304
}
301305

302306
func packageDescriptionSettings(_ path: AbsolutePath) -> FileBuildSettings? {
303-
304307
for package in packageGraph.packages {
305308
if path == package.manifest.path {
306309
return FileBuildSettings(
307310
preferredToolchain: nil,
308-
compilerArguments: package.manifest.interpreterFlags + [path.asString])
311+
compilerArguments:
312+
workspace.interpreterFlags(for: package.path) + [path.asString])
309313
}
310314
}
311315
return nil
@@ -332,6 +336,8 @@ private struct SwiftPMToolchain: Build.Toolchain, ManifestResourceProvider {
332336
var extraSwiftCFlags: [String]
333337
var extraCPPFlags: [String]
334338
var dynamicLibraryExtension: String
339+
340+
func getClangCompiler() throws -> AbsolutePath { return clangCompiler }
335341
}
336342

337343
extension ToolchainRegistry {

0 commit comments

Comments
 (0)