Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Example/HelloWorld/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,5 @@ setup_sourcekit_bsp(
targets = [
"//HelloWorld/...",
],
no_extra_output_base = True,
)
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,18 @@ final class InitializeHandler {
logger.debug("regularOutputBase: \(regularOutputBase, privacy: .public)")

// Setup the special output base path where we will run indexing commands from.
let regularOutputBaseLastPath = regularOutputBase.lastPathComponent
let outputBase = regularOutputBase.deletingLastPathComponent().appendingPathComponent(
"\(regularOutputBaseLastPath)-sourcekit-bazel-bsp"
).path
logger.debug("outputBase: \(outputBase, privacy: .public)")
let outputBase: String
if baseConfig.noExtraOutputBase {
outputBase = regularOutputBase.path
logger.debug("Will use the regular output base for all actions")
} else {
let regularOutputBaseLastPath = regularOutputBase.lastPathComponent
outputBase =
regularOutputBase.deletingLastPathComponent().appendingPathComponent(
"\(regularOutputBaseLastPath)-sourcekit-bazel-bsp"
).path
logger.debug("outputBase: \(outputBase, privacy: .public)")
}

// Now, get the full output path based on the above output base.
let outputPath: String = try commandRunner.bazelIndexAction(
Expand Down
5 changes: 4 additions & 1 deletion Sources/SourceKitBazelBSP/Server/BaseServerConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ package struct BaseServerConfig: Equatable {
let dependencyRulesToDiscover: [DependencyRuleType]
let topLevelTargetsToExclude: [String]
let dependencyTargetsToExclude: [String]
let noExtraOutputBase: Bool

package init(
bazelWrapper: String,
Expand All @@ -44,7 +45,8 @@ package struct BaseServerConfig: Equatable {
topLevelRulesToDiscover: [TopLevelRuleType] = TopLevelRuleType.allCases,
dependencyRulesToDiscover: [DependencyRuleType] = DependencyRuleType.allCases,
topLevelTargetsToExclude: [String] = [],
dependencyTargetsToExclude: [String] = []
dependencyTargetsToExclude: [String] = [],
noExtraOutputBase: Bool = false
) {
self.bazelWrapper = bazelWrapper
self.targets = targets
Expand All @@ -55,5 +57,6 @@ package struct BaseServerConfig: Equatable {
self.dependencyRulesToDiscover = dependencyRulesToDiscover
self.topLevelTargetsToExclude = topLevelTargetsToExclude
self.dependencyTargetsToExclude = dependencyTargetsToExclude
self.noExtraOutputBase = noExtraOutputBase
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct InitializedServerConfig: Equatable {
}

var indexStorePath: String {
// This is the same path hardcoded by rules_swift
// when the global index store feature is enabled.
outputPath + "/_global_index_store"
}
}
25 changes: 13 additions & 12 deletions Sources/SourceKitBazelBSP/SharedUtils/Shell/CommandRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,21 @@ extension CommandRunner {
additionalStartupFlags: [String] = []
) -> String {
let indexFlags = baseConfig.indexFlags
let additionalFlags = additionalFlags.map { " \($0)" }
let flagsString: String
if indexFlags.isEmpty {
flagsString = additionalFlags.joined(separator: "")
} else {
flagsString = (additionalFlags + indexFlags.map { " \($0)" }).joined(separator: "")
var components: [String] = []
if !baseConfig.noExtraOutputBase {
components.append("--output_base=\(outputBase)")
}
let startupFlagsString: String
if additionalStartupFlags.isEmpty {
startupFlagsString = ""
} else {
startupFlagsString = " " + additionalStartupFlags.joined(separator: " ")
if !additionalStartupFlags.isEmpty {
components.append(contentsOf: additionalStartupFlags)
}
return "--output_base=\(outputBase)\(startupFlagsString) \(cmd)\(flagsString)"
components.append(cmd)
if !additionalFlags.isEmpty {
components.append(contentsOf: additionalFlags)
}
if !indexFlags.isEmpty {
components.append(contentsOf: indexFlags)
}
return components.joined(separator: " ")
}

/// A regular bazel command, but at this BSP's special output base and taking into account the special index flags.
Expand Down
9 changes: 8 additions & 1 deletion Sources/sourcekit-bazel-bsp/Commands/Serve.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ struct Serve: ParsableCommand {
)
var dependencyTargetToExclude: [String] = []

@Flag(
help:
"If enabled, the BSP will not create a separate output base for its indexing actions. You can use this in conjunction with rules_swift's `index_while_building` and `use_global_module_cache` to improve indexing performance and reduce disk usage at the cost of potentially slower builds."
)
var noExtraOutputBase: Bool = false

func run() throws {
logger.info("`serve` invoked, initializing BSP server...")

Expand All @@ -98,7 +104,8 @@ struct Serve: ParsableCommand {
topLevelRulesToDiscover: topLevelRulesToDiscover,
dependencyRulesToDiscover: dependencyRulesToDiscover,
topLevelTargetsToExclude: topLevelTargetToExclude,
dependencyTargetsToExclude: dependencyTargetToExclude
dependencyTargetsToExclude: dependencyTargetToExclude,
noExtraOutputBase: noExtraOutputBase
)

logger.debug("Initializing BSP with targets: \(targets)")
Expand Down
6 changes: 6 additions & 0 deletions rules/setup_sourcekit_bsp.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def _setup_sourcekit_bsp_impl(ctx):
if files_to_watch:
bsp_config_argv.append("--files-to-watch")
bsp_config_argv.append(files_to_watch)
if ctx.attr.no_extra_output_base:
bsp_config_argv.append("--no-extra-output-base")
ctx.actions.expand_template(
template = ctx.file._bsp_config_template,
output = rendered_bsp_config,
Expand Down Expand Up @@ -146,5 +148,9 @@ setup_sourcekit_bsp = rule(
doc = "Instead of attempting to build targets individually, build the top-level parent. If your project contains build_test targets for your individual libraries and you're passing them as the top-level targets for the BSP, you can use this flag to build those targets directly for better predictability and caching.",
default = False,
),
"no_extra_output_base": attr.bool(
doc = "If enabled, the BSP will not create a separate output base for its indexing actions. You can use this in conjunction with rules_swift's `index_while_building` and `use_global_module_cache` to improve indexing performance and reduce disk usage at the cost of potentially slower builds.",
default = False,
),
},
)