Skip to content

Add swift_toolchain_tool to run tools from the resolved Swift toolchain - #2034

Open
cgrindel wants to merge 4 commits into
bazelbuild:mainfrom
cgrindel:cgrindel/swift_toolchain_tool
Open

cgrindel wants to merge 4 commits into
bazelbuild:mainfrom
cgrindel:cgrindel/swift_toolchain_tool

Conversation

@cgrindel

@cgrindel cgrindel commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Adds a swift_toolchain_tool rule that runs swift, or a sibling tool such as swift-format, from the resolved Swift toolchain, so downstream rulesets can run swift package with the toolchain's Swift instead of whatever is on PATH.

  • Added SwiftToolchainInfo.swift_multitool (executable, args, env, files, tool_executable_suffix), set by both swift_toolchain and xcode_swift_toolchain
  • Added swift and swift_runfiles to swift_tools; the standalone and Swift SDK templates point them at usr/bin/swift and the files swift package needs under sandboxes and remote execution
  • Added swift_toolchain_tool(name, tool = "swift"), a wrapper that runs the tool with the toolchain's environment, through xcrun on macOS, resolving DEVELOPER_DIR from XCODE_VERSION_OVERRIDE when unset
  • On Windows the wrapper is a .bat that runs <root>\bin\<tool>.exe with the toolchain env and SDKROOT, and resolves a custom driver through the runfiles manifest
  • Added analysis tests, bazel run tests for swift, swift package, and swift-format, a genrule test that uses the wrapper as an action tool, and a Windows test with a .bat custom driver; the Linux non-hermetic and Windows CI jobs run them
  • Regenerated doc/rules.md and doc/providers.md

Claude 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 .bat wrapper.

🤖 Generated with Claude Code

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant