Skip to content

Commit 6e3e768

Browse files
authored
Improve handling on label conversions (#128)
1 parent 0c5065e commit 6e3e768

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

Sources/SourceKitBazelBSP/RequestHandlers/BuildTargets/BazelTargetQuerierParser.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,12 @@ final class BazelTargetQuerierParserImpl: BazelTargetQuerierParser {
148148
// Now, separate the parsed content between top-level and non-top-level targets.
149149
// We don't need to handle the case where a top-level target is missing entirely
150150
// because Bazel itself will fail when this is the case.
151-
let userProvidedTargetsSet = Set(userProvidedTargets)
152151
let supportedTopLevelRuleTypesSet = Set(supportedTopLevelRuleTypes)
153152
var topLevelTargets: [(BlazeQuery_Target, TopLevelRuleType)] = []
154153
var dependencyTargets: [BlazeQuery_Target] = []
154+
// Convert the user's provided targets to full labels if needed, since this is what
155+
// the cquery result will contain.
156+
let userProvidedTargetsSet = Set(userProvidedTargets.map { $0.toFullLabel() })
155157
for target in allRules {
156158
let kind = target.rule.ruleClass
157159
let name = target.rule.name
@@ -696,4 +698,18 @@ extension String {
696698

697699
return (packageName: packageName, targetName: targetName)
698700
}
701+
702+
// Converts a Bazel label to its "full" equivalent, if needed.
703+
// e.g: "//foo/bar" -> "//foo/bar:bar"
704+
fileprivate func toFullLabel() -> String {
705+
let paths = components(separatedBy: "/")
706+
let lastComponent = paths.last
707+
if lastComponent?.contains(":") == true {
708+
return self
709+
} else if let lastComponent = lastComponent {
710+
return "\(self):\(lastComponent)"
711+
} else {
712+
return self
713+
}
714+
}
699715
}

Sources/SourceKitBazelBSP/Server/BaseServerConfig.swift

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,10 @@ package struct BaseServerConfig: Equatable {
4141
topLevelRulesToDiscover: [TopLevelRuleType] = TopLevelRuleType.allCases,
4242
) {
4343
self.bazelWrapper = bazelWrapper
44+
self.targets = targets
4445
self.indexFlags = indexFlags
4546
self.filesToWatch = filesToWatch
4647
self.compileTopLevel = compileTopLevel
4748
self.topLevelRulesToDiscover = topLevelRulesToDiscover
48-
49-
// We need to post-process the target list provided by the user
50-
// because the queries will always return the "full" label.
51-
// e.g: "//foo/bar" -> "//foo/bar:bar"
52-
// We need to also de-dupe them if the user passed wildcards in Serve.swift.
53-
self.targets = Set(targets.map { $0.toFullLabel() }).sorted()
54-
}
55-
}
56-
57-
extension String {
58-
func toFullLabel() -> String {
59-
let paths = components(separatedBy: "/")
60-
let lastComponent = paths.last
61-
if lastComponent?.contains(":") == true {
62-
return self
63-
} else if let lastComponent = lastComponent {
64-
return "\(self):\(lastComponent)"
65-
} else {
66-
return self
67-
}
6849
}
6950
}

Tests/SourceKitBazelBSPTests/BazelTargetQuerierTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct BazelTargetQuerierTests {
8989
let config = Self.makeInitializedConfig()
9090

9191
let expectedCommand =
92-
"bazelisk --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld:HelloWorld)) in $topLevelTargets union kind(\"source file|swift_library|alias\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto --config=test"
92+
"bazelisk --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld)) in $topLevelTargets union kind(\"source file|swift_library|alias\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto --config=test"
9393
runnerMock.setResponse(for: expectedCommand, cwd: Self.mockRootUri, response: exampleCqueryOutput)
9494

9595
_ = try querier.cqueryTargets(
@@ -112,7 +112,7 @@ struct BazelTargetQuerierTests {
112112
let config = Self.makeInitializedConfig(targets: ["//HelloWorld", "//Tests"])
113113

114114
let expectedCommand =
115-
"bazelisk --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld:HelloWorld //Tests:Tests)) in $topLevelTargets union kind(\"objc_library|swift_library|alias\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto --config=test"
115+
"bazelisk --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld //Tests)) in $topLevelTargets union kind(\"objc_library|swift_library|alias\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto --config=test"
116116
runnerMock.setResponse(for: expectedCommand, cwd: Self.mockRootUri, response: exampleCqueryOutput)
117117

118118
_ = try querier.cqueryTargets(
@@ -136,13 +136,13 @@ struct BazelTargetQuerierTests {
136136

137137
runnerMock.setResponse(
138138
for:
139-
"bazel --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld:HelloWorld)) in $topLevelTargets union kind(\"swift_library|alias\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto",
139+
"bazel --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld)) in $topLevelTargets union kind(\"swift_library|alias\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto",
140140
cwd: Self.mockRootUri,
141141
response: exampleCqueryOutput
142142
)
143143
runnerMock.setResponse(
144144
for:
145-
"bazel --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld:HelloWorld)) in $topLevelTargets union kind(\"objc_library|alias\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto",
145+
"bazel --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld)) in $topLevelTargets union kind(\"objc_library|alias\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto",
146146
cwd: Self.mockRootUri,
147147
response: exampleCqueryOutput
148148
)
@@ -178,7 +178,7 @@ struct BazelTargetQuerierTests {
178178
let config = Self.makeInitializedConfig()
179179

180180
let expectedCommand =
181-
"bazelisk --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld:HelloWorld)) in $topLevelTargets union kind(\"source file|swift_library|alias|_watchos_internal_unit_test_bundle\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto --config=test"
181+
"bazelisk --output_base=/path/to/output/base cquery \'let topLevelTargets = kind(\"rule\", set(//HelloWorld)) in $topLevelTargets union kind(\"source file|swift_library|alias|_watchos_internal_unit_test_bundle\", deps($topLevelTargets))\' --noinclude_aspects --notool_deps --noimplicit_deps --output proto --config=test"
182182
runnerMock.setResponse(for: expectedCommand, cwd: Self.mockRootUri, response: exampleCqueryOutput)
183183

184184
_ = try querier.cqueryTargets(

0 commit comments

Comments
 (0)