diff --git a/Sources/SourceKitBazelBSP/RequestHandlers/BuildTargets/BazelTargetQuerierParser.swift b/Sources/SourceKitBazelBSP/RequestHandlers/BuildTargets/BazelTargetQuerierParser.swift index 88e52264..bca0654f 100644 --- a/Sources/SourceKitBazelBSP/RequestHandlers/BuildTargets/BazelTargetQuerierParser.swift +++ b/Sources/SourceKitBazelBSP/RequestHandlers/BuildTargets/BazelTargetQuerierParser.swift @@ -146,10 +146,12 @@ final class BazelTargetQuerierParserImpl: BazelTargetQuerierParser { // Now, separate the parsed content between top-level and non-top-level targets. // We don't need to handle the case where a top-level target is missing entirely // because Bazel itself will fail when this is the case. - let userProvidedTargetsSet = Set(userProvidedTargets) let supportedTopLevelRuleTypesSet = Set(supportedTopLevelRuleTypes) var topLevelTargets: [(BlazeQuery_Target, TopLevelRuleType)] = [] var dependencyTargets: [BlazeQuery_Target] = [] + // Convert the user's provided targets to full labels if needed, since this is what + // the cquery result will contain. + let userProvidedTargetsSet = Set(userProvidedTargets.map { $0.toFullLabel() }) for target in allRules { let kind = target.rule.ruleClass let name = target.rule.name @@ -611,4 +613,18 @@ extension String { return (packageName: packageName, targetName: targetName) } + + // Converts a Bazel label to its "full" equivalent, if needed. + // e.g: "//foo/bar" -> "//foo/bar:bar" + fileprivate func toFullLabel() -> String { + let paths = components(separatedBy: "/") + let lastComponent = paths.last + if lastComponent?.contains(":") == true { + return self + } else if let lastComponent = lastComponent { + return "\(self):\(lastComponent)" + } else { + return self + } + } } diff --git a/Sources/SourceKitBazelBSP/Server/BaseServerConfig.swift b/Sources/SourceKitBazelBSP/Server/BaseServerConfig.swift index 6dc1c73f..a9b9e959 100644 --- a/Sources/SourceKitBazelBSP/Server/BaseServerConfig.swift +++ b/Sources/SourceKitBazelBSP/Server/BaseServerConfig.swift @@ -41,29 +41,10 @@ package struct BaseServerConfig: Equatable { topLevelRulesToDiscover: [TopLevelRuleType] = TopLevelRuleType.allCases, ) { self.bazelWrapper = bazelWrapper + self.targets = targets self.indexFlags = indexFlags self.filesToWatch = filesToWatch self.compileTopLevel = compileTopLevel self.topLevelRulesToDiscover = topLevelRulesToDiscover - - // We need to post-process the target list provided by the user - // because the queries will always return the "full" label. - // e.g: "//foo/bar" -> "//foo/bar:bar" - // We need to also de-dupe them if the user passed wildcards in Serve.swift. - self.targets = Set(targets.map { $0.toFullLabel() }).sorted() - } -} - -extension String { - func toFullLabel() -> String { - let paths = components(separatedBy: "/") - let lastComponent = paths.last - if lastComponent?.contains(":") == true { - return self - } else if let lastComponent = lastComponent { - return "\(self):\(lastComponent)" - } else { - return self - } } } diff --git a/Tests/SourceKitBazelBSPTests/BazelTargetQuerierTests.swift b/Tests/SourceKitBazelBSPTests/BazelTargetQuerierTests.swift index 845298c7..5d3131d1 100644 --- a/Tests/SourceKitBazelBSPTests/BazelTargetQuerierTests.swift +++ b/Tests/SourceKitBazelBSPTests/BazelTargetQuerierTests.swift @@ -89,7 +89,7 @@ struct BazelTargetQuerierTests { let config = Self.makeInitializedConfig() let expectedCommand = - "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" + "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" runnerMock.setResponse(for: expectedCommand, cwd: Self.mockRootUri, response: exampleCqueryOutput) _ = try querier.cqueryTargets( @@ -112,7 +112,7 @@ struct BazelTargetQuerierTests { let config = Self.makeInitializedConfig(targets: ["//HelloWorld", "//Tests"]) let expectedCommand = - "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" + "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" runnerMock.setResponse(for: expectedCommand, cwd: Self.mockRootUri, response: exampleCqueryOutput) _ = try querier.cqueryTargets( @@ -136,13 +136,13 @@ struct BazelTargetQuerierTests { runnerMock.setResponse( for: - "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", + "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", cwd: Self.mockRootUri, response: exampleCqueryOutput ) runnerMock.setResponse( for: - "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", + "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", cwd: Self.mockRootUri, response: exampleCqueryOutput ) @@ -178,7 +178,7 @@ struct BazelTargetQuerierTests { let config = Self.makeInitializedConfig() let expectedCommand = - "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" + "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" runnerMock.setResponse(for: expectedCommand, cwd: Self.mockRootUri, response: exampleCqueryOutput) _ = try querier.cqueryTargets(