Skip to content

Commit fd6d608

Browse files
committed
Use cquery when getting target information
1 parent 4c04b1e commit fd6d608

File tree

13 files changed

+90
-289
lines changed

13 files changed

+90
-289
lines changed

Package.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,9 @@ let package = Package(
2626
url: "https://github.com/apple/swift-argument-parser",
2727
revision: "1.5.0"
2828
),
29-
.package(
30-
url: "https://github.com/swiftlang/sourcekit-lsp",
31-
revision: "0e061c5c1075152bc2e6187679a11b81d0c3e326" // latest main commit November 29, 2025
32-
// TODO: Ideally it would be better to upstream these changes to sourceKit-lsp
33-
// url: "https://github.com/rockbruno/sourcekit-lsp",
34-
// revision: "c052baae81ec6532bb2f939a21acc4650fb1dc86"
35-
),
3629
.package(
3730
url: "https://github.com/apple/swift-protobuf.git",
38-
revision: "102a647b573f60f73afdce5613a51d71349fe507"
31+
revision: "1.33.3"
3932
),
4033
],
4134
targets: [
@@ -78,7 +71,7 @@ let package = Package(
7871
],
7972
resources: [
8073
.copy("Resources/aquery.pb"),
81-
.copy("Resources/streamdeps.pb"),
74+
.copy("Resources/cquery.pb"),
8275
],
8376
),
8477
.target(
@@ -98,7 +91,6 @@ let package = Package(
9891
dependencies: ["BazelProtobufBindings"],
9992
resources: [
10093
.copy("Resources/actions.pb"),
101-
.copy("Resources/streamdeps.pb"),
10294
],
10395
),
10496
]

Sources/BazelProtobufBindings/BazelProtobufBindings.swift

Lines changed: 2 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -20,116 +20,11 @@
2020
import Foundation
2121

2222
package enum BazelProtobufBindings {
23-
package static func parseQueryTargets(data: Data) throws -> [BlazeQuery_Target] {
24-
var targets: [BlazeQuery_Target] = []
25-
let messages = try parseMultipleDelimitedMessages(from: data)
26-
for message in messages {
27-
let target = try BlazeQuery_Target(serializedBytes: message)
28-
targets.append(target)
29-
}
30-
31-
return targets
32-
}
33-
3423
package static func parseActionGraph(data: Data) throws -> Analysis_ActionGraphContainer {
3524
try Analysis_ActionGraphContainer(serializedBytes: data)
3625
}
37-
}
38-
39-
extension BazelProtobufBindings {
40-
/// Bazel query outputs a series of messages and each one is prefixed with length to indicate
41-
/// the number of bytes in the payload. Returns a tuple of (value, bytesConsumed).
42-
/// Protobuf [documentation](https://protobuf.dev/programming-guides/encoding/) provides more
43-
/// details on how `varint` works.
44-
private static func parseVarint(
45-
from data: Data,
46-
startIndex: Int
47-
) throws -> (UInt64, Int) {
48-
guard startIndex < data.count else {
49-
throw VarintError.truncated
50-
}
51-
52-
var result: UInt64 = 0
53-
var shift = 0
54-
var bytesRead = 0
55-
var index = startIndex
56-
57-
while index < data.count {
58-
let byte = data[index]
59-
bytesRead += 1
60-
index += 1
61-
62-
// Check for overflow (varints can be at most 10 bytes for 64-bit values)
63-
if bytesRead > 10 {
64-
throw VarintError.overflow
65-
}
66-
67-
// Extract the 7 data bits
68-
let dataBits = UInt64(byte & 0x7F)
69-
70-
// Check for shift overflow
71-
if shift >= 64 {
72-
throw VarintError.overflow
73-
}
74-
75-
// little-endian -> big-endian
76-
result |= dataBits << shift
77-
78-
// If the continuation bit (MSB) is not set, we're done
79-
if (byte & 0x80) == 0 {
80-
return (result, bytesRead)
81-
}
82-
83-
shift += 7
84-
}
8526

86-
// If we get here, the varint was truncated
87-
throw VarintError.truncated
27+
package static func parseCqueryResult(data: Data) throws -> Analysis_CqueryResult {
28+
try Analysis_CqueryResult(serializedBytes: data)
8829
}
89-
90-
/// Parse the length prefix and return the message data
91-
private static func parseDelimitedMessage(
92-
from data: Data,
93-
startIndex: Int = 0
94-
) throws -> (Data, Int) {
95-
let (messageLength, lengthBytes) = try parseVarint(
96-
from: data,
97-
startIndex: startIndex
98-
)
99-
100-
let messageStart = startIndex + lengthBytes
101-
let messageEnd = messageStart + Int(messageLength)
102-
103-
guard messageEnd <= data.count else {
104-
throw VarintError.truncated
105-
}
106-
107-
let messageData = data.subdata(in: messageStart..<messageEnd)
108-
let totalBytesConsumed = lengthBytes + Int(messageLength)
109-
110-
return (messageData, totalBytesConsumed)
111-
}
112-
113-
/// Parse multiple delimited messages from a data stream
114-
private static func parseMultipleDelimitedMessages(from data: Data) throws -> [Data] {
115-
var messages: [Data] = []
116-
var currentIndex = 0
117-
118-
while currentIndex < data.count {
119-
let (messageData, bytesConsumed) = try parseDelimitedMessage(
120-
from: data,
121-
startIndex: currentIndex
122-
)
123-
messages.append(messageData)
124-
currentIndex += bytesConsumed
125-
}
126-
127-
return messages
128-
}
129-
}
130-
131-
package enum VarintError: Error {
132-
case truncated
133-
case overflow
134-
case invalidData
13530
}

0 commit comments

Comments
 (0)