Repository navigation
Conversation
…hain Expose the toolchain's `swift` multi-tool driver as a new `SwiftToolchainInfo.swift_multitool` field, populated by both `swift_toolchain` and `xcode_swift_toolchain`. It is a small struct with `executable` (a `File` or a string), `args`, `env`, and `files` rather than a `ToolConfigInfo`, because no action runs `swift` itself and the worker, param-file, and resource-set settings mean nothing for it. Its `env` omits `TOOLCHAIN_PATH`, which is exec-root-relative and wrong under `bazel run`. - New `swift_toolchain_tool(name, tool = "swift")` rule writes a wrapper that runs the driver (or a sibling tool such as `swift-format`) with the toolchain's environment, through `xcrun` on macOS. It works with `bazel run` and as an exec-configured tool. - On macOS the wrapper runs the Xcode that Bazel selected. `xcrun` honors only `DEVELOPER_DIR`, which Bazel sets for actions but not for `bazel run`, so when it is unset the wrapper maps `XCODE_VERSION_OVERRIDE` to a developer directory with Bazel's `xcode-locator` (from `@bazel_tools`, a stub off macOS) and fails clearly if that Xcode is missing. - `swift_tools` gains optional `swift` and `swift_runfiles` attributes (and `SwiftToolsInfo` fields). The standalone toolchain and Swift SDK templates set them to `usr/bin/swift` and a new `swift_multitool_files` filegroup (`usr/bin/*`, `usr/lib/swift/pm/**`, the clang resource directory, and the top-level toolchain libraries), so `swift package` works under remote execution and strict sandboxes. This is a separate attribute rather than a wider `additional_inputs`, because `additional_inputs` is added to every compile, symbol-graph, and autolink action and would bloat their inputs. The wrapper still falls back to the driver's real path to find siblings. - Custom drivers (`swift_executable` / `default_swift_executable`) run with `--driver-mode=swift`, the same way `swiftc` is handled. - Run tests cover `swift --version`, `swift package --version`, and `swift-format --version`, with manual `transition_test` wrappers for the autoconfigured Linux toolchain in the non-hermetic CI job.
Validate the `tool` attribute so that an empty name or a path fails analysis with a clear message, and check that a bare tool name is on PATH before running `swift` as well as other tools. A `swift_toolchain` with a relative `root` now runs `swift` from PATH, since an execution-root-relative path does not work under `bazel run`. Drop a stray `types` dep from the `tool_config` bzl_library. Add analysis tests for a driver set with the `swift_executable` attribute, a relative toolchain root, hermetic `swift_tools` toolchains with and without a `swift` multi-tool, and the invalid `tool` failure, and regenerate the docs.
When the target platform is Windows, swift_toolchain_tool now writes a .bat wrapper instead of a bash script, since bazel run launches the executable directly there. The wrapper runs a string driver by its absolute path, or finds a File driver through the runfiles manifest or a runfiles directory, and sets the toolchain environment with percent signs escaped. SwiftToolchainInfo.swift_multitool gains tool_executable_suffix, which both templates apply to sibling tools such as swift-format.exe. Analysis tests render the .bat for a fixture Windows platform and toolchain, backed by a fake C++ toolchain so that resolution works on any host. The existing bash script tests now target a fixture Linux platform so that they still run on Windows hosts, and the tests of the host toolchain skip Windows. A genrule test runs the wrapper as an action tool, and the Windows CI job now runs the analysis tests and the swift run tests.
On Windows, the multi-tool environment now sets SDKROOT to the toolchain's SDK, so that swift run outside of an action finds it; actions keep their environment. The .bat wrapper also tries the runfiles manifest next to itself when the one from the environment lacks the driver, quotes its error messages, and turns off delayed expansion. The rule now fails on values that a batch file cannot escape (double quotes and line breaks) and restricts tool to [A-Za-z0-9._+-]. The genrule test gets a cmd_bat so Windows runs it under cmd.exe, and a new Windows-only transition_test runs the wrapper with a .bat custom driver set through default_swift_executable, which transition_test can now set. The rule doc notes that cmd.exe re-parses arguments forwarded with %*. The template attribute is renamed to _sh_template, and swift_multitool_files drops usr/lib/swift/pm/**, which additional_inputs already covers.
cgrindel
requested review from
aaronsky,
adincebic,
brentleyjones,
keith and
luispadron
as code owners
November 21, 2026 19:53
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
swift_toolchain_toolrule that runsswift, or a sibling tool such asswift-format, from the resolved Swift toolchain, so downstream rulesets can runswift packagewith the toolchain's Swift instead of whatever is onPATH.SwiftToolchainInfo.swift_multitool(executable,args,env,files,tool_executable_suffix), set by bothswift_toolchainandxcode_swift_toolchainswiftandswift_runfilestoswift_tools; the standalone and Swift SDK templates point them atusr/bin/swiftand the filesswift packageneeds under sandboxes and remote executionswift_toolchain_tool(name, tool = "swift"), a wrapper that runs the tool with the toolchain's environment, throughxcrunon macOS, resolvingDEVELOPER_DIRfromXCODE_VERSION_OVERRIDEwhen unset.batthat runs<root>\bin\<tool>.exewith the toolchain env andSDKROOT, and resolves a custom driver through the runfiles manifestbazel runtests forswift,swift package, andswift-format, a genrule test that uses the wrapper as an action tool, and a Windows test with a.batcustom driver; the Linux non-hermetic and Windows CI jobs run themdoc/rules.mdanddoc/providers.mdClaude implemented this and verified it on macOS with both the Xcode and standalone toolchains;
bazel test //swift/... //test/...passes. Linux and Windows are covered by CI only; the Windows job is the first real run of the.batwrapper.🤖 Generated with Claude Code