Skip to content

Commit 2aba5f1

Browse files
tausbnCopilot
andcommitted
unified: Make Bazel the only supported build
Nothing builds this extractor with `cargo` any more: the tests, the extractor pack and the corpus regeneration all go through Bazel. The `cargo` half of the build is now dead weight, and it is not free -- it is a second, independent way of compiling the same Swift code, kept in step by hand. `build.rs` shelled out to `swift build` on the SwiftPM package in `swift/`, scraped `swiftc -print-target-info` for the runtime directory, and emitted the resulting link and rpath flags. Bazel does none of that: it compiles `SwiftSyntaxFFI.swift` directly against `@swift-syntax`, resolved from the Bazel Central Registry. `Package.swift` and `Package.resolved` existed only for that `swift build`. Deleting them collapses three version pins into one. `MODULE.bazel` pinned the toolchain, `.swift-version` pinned it again for `swiftly` and `build.rs`, and `Package.swift` pinned the `swift-syntax` release a second time -- with a comment asking that they be kept in sync and nothing enforcing it. Two Swift builds that could silently diverge become one that cannot. `cargo check` still works, since checking does not link, so rust-analyzer keeps working across the crate -- completion, go-to-definition and inline diagnostics included. What no longer works is `cargo build`/`cargo test`, and hence the editor's "run test" lens; use `bazel test //unified/extractor:all_tests`. The Cargo manifests stay because `misc/bazel/3rdparty` resolves third-party crate versions from them. `scripts/create-extractor-pack.sh` becomes a wrapper around the `codeql_pack` installer that already existed, rather than a parallel cargo-based assembly of the same pack. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 297ff885-7c13-4202-a9d0-bc0b17f68d8e
1 parent 6dc61eb commit 2aba5f1

9 files changed

Lines changed: 36 additions & 245 deletions

File tree

MODULE.bazel

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,8 @@ use_repo(
228228
# auto-registers `xcode_swift_toolchain` (host Xcode + OS-provided Swift
229229
# runtime), which is not hermetic.
230230
#
231-
# The version is pinned as a literal rather than read from
232-
# `unified/swift-syntax-rs/.swift-version` via `swift_version_file`: the latter
233-
# makes the extension `module_ctx.read` a `//unified/...` label, which fails to
234-
# resolve when this repo is consumed as a dependency module (`@@ql+`) whose
235-
# `unified/swift-syntax-rs` package is not loadable in that context. Keep this
236-
# in sync with `unified/swift-syntax-rs/.swift-version` (used by the `cargo`
237-
# build) and the `swift-syntax` release in `swift/Package.swift`.
231+
# This and the `swift-syntax` version above are the only pins: the extractor is
232+
# built solely by Bazel, so there is no second Swift build to keep in step.
238233
swift = use_extension("@rules_swift//swift:extensions.bzl", "swift")
239234
swift.toolchain(
240235
name = "swift_toolchain",

unified/AGENTS.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ This is a CodeQL extractor that maps a language's parse tree onto a shared AST
44
using the `yeast` desugaring engine. Swift, the only language so far, is parsed
55
by Apple's swift-syntax rather than by tree-sitter.
66

7-
Build and test with Bazel, whose Swift toolchain is hermetic on Linux, so
8-
nothing needs to be installed locally. The extractor links `swift-syntax`, so a
9-
`cargo` build additionally needs a local Swift toolchain.
7+
Everything is built and tested with Bazel, whose Swift toolchain is hermetic on
8+
Linux, so nothing needs to be installed locally. `cargo build`/`cargo test` do
9+
not work here: the extractor links a Swift FFI shim that only Bazel builds.
10+
(`cargo check` still works, so rust-analyzer is unaffected.)
1011

1112
## Building
1213
- To build the extractor pack, run `scripts/create-extractor-pack.sh`.

unified/swift-syntax-rs/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
/target
2-
/swift/.build

unified/swift-syntax-rs/.swift-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

unified/swift-syntax-rs/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# `BUILD.bazel` is the build. This file feeds `misc/bazel/3rdparty`, which
2+
# resolves third-party crate versions from it, and `cargo check`; `cargo
3+
# build`/`test` cannot link the Swift shim.
14
[package]
25
name = "swift-syntax-rs"
36
description = "Rust wrapper around the swift-syntax package for parsing Swift source"

unified/swift-syntax-rs/README.md

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ A Rust wrapper around the [swift-syntax](https://github.com/swiftlang/swift-synt
44
package, allowing Swift source code to be parsed from Rust.
55

66
Parsing is delegated to a small Swift shim (in [`swift/`](swift/)) that links
7-
against `SwiftSyntax`/`SwiftParser` and exposes a tiny C ABI. The Rust crate
8-
builds that shim (via `build.rs`) and provides safe bindings on top of it.
7+
against `SwiftSyntax`/`SwiftParser` and exposes a tiny C ABI. This crate provides
8+
safe bindings on top of it.
9+
10+
Bazel builds both halves; there is no `cargo` build (see
11+
[Building & testing](#building--testing)).
912

1013
## Output format
1114

@@ -122,54 +125,26 @@ an unparenthesised `a *** b + c` is a single flat sequence whose structure
122125
cannot be determined without knowing `***`'s precedence, so it is left flat in
123126
its entirety.
124127

125-
## Prerequisites
126-
127-
The build does not depend on any particular version manager. You need:
128-
129-
- **Rust** — pinned to `1.88` by the repo-root [`rust-toolchain.toml`](../../rust-toolchain.toml),
130-
which `rustup` picks up automatically.
131-
- **Swift** — pinned to the version in [`.swift-version`](.swift-version)
132-
(currently `6.3.2`), used to build `swift-syntax` `603.0.2`. Install it any way
133-
you like — [swift.org](https://www.swift.org/install/) or
134-
[swiftly](https://www.swift.org/swiftly/) (which reads `.swift-version`), or a
135-
system package. Just make sure `swift` is on your `PATH` (or point `build.rs`
136-
at it with the `SWIFT` environment variable).
137-
138-
On Debian/Ubuntu the Swift runtime also needs `libncurses6` (and related libs)
139-
available on the system.
140-
141128
## Building & testing
142129

143-
With `cargo` and `swift` on `PATH`:
144-
145-
```sh
146-
cargo build
147-
cargo test
148-
```
149-
150-
If your `swift`/`swiftc` are not on `PATH`, point the build at them explicitly:
151-
152-
```sh
153-
SWIFT=/path/to/swift SWIFTC=/path/to/swiftc cargo build
154-
```
155-
156-
The first build compiles `swift-syntax` and can take several minutes.
157-
158-
## Building with Bazel (CI)
159-
160-
CI builds this crate hermetically with Bazel. A Swift toolchain is downloaded
161-
from swift.org by the official `rules_swift` standalone toolchain extension
162-
(wired up in the repo-root `MODULE.bazel`), `swift-syntax` is pulled from the
163-
Bazel Central Registry, and the FFI shim is compiled as a `swift_library` that
164-
the Rust targets link against. `build.rs` is not used under Bazel; it only
165-
builds the Swift shim for the local `cargo` workflow.
130+
Everything is built by Bazel, which downloads a Swift toolchain from swift.org
131+
via the official `rules_swift` standalone toolchain extension (wired up in the
132+
repo-root `MODULE.bazel`) and pulls `swift-syntax` from the Bazel Central
133+
Registry. Nothing has to be installed locally on Linux:
166134

167135
```sh
168136
bazel build //unified/swift-syntax-rs:swift-syntax-parse
169137
bazel test //unified/swift-syntax-rs:swift_syntax_rs_test
170138
bazel run //unified/swift-syntax-rs:swift-syntax-parse < some.swift
171139
```
172140

141+
The first build compiles `swift-syntax` and can take several minutes.
142+
143+
`cargo build`/`cargo test` do **not** work: the Swift shim is compiled by a
144+
`swift_library` in [`BUILD.bazel`](BUILD.bazel), so a `cargo` link finds no
145+
`ssr_*` symbols. `cargo check` does work — it does not link — which is all
146+
rust-analyzer needs.
147+
173148
Requirements:
174149

175150
- **`clang`** must be installed on the runner. `rules_swift` requires the Bazel
@@ -186,20 +161,12 @@ Requirements:
186161
[`xcode_transition.bzl`](xcode_transition.bzl)), so other targets on macOS
187162
keep using Bazel's default CC toolchain.
188163

189-
The Swift compiler version is kept in sync across three places: the
190-
[`.swift-version`](.swift-version) file (read by the local `cargo`/`swift build`
191-
and by [swiftly](https://www.swift.org/swiftly/)), the literal `swift_version`
192-
pinned on `swift.toolchain(...)` in the root `MODULE.bazel` (the hermetic
193-
swift.org **Linux** Bazel toolchain), and the `swift-syntax` release in
194-
`swift/Package.swift`. On **macOS** the version is *not* pinned by the Bazel
195-
build: `rules_swift` auto-registers the host `xcode_swift_toolchain`, which uses
196-
whichever Swift ships with the installed Xcode. So the pin governs Linux (and
197-
local) builds, while the macOS compiler version depends on the host Xcode.
198-
199-
(The Bazel toolchain pins a literal rather than reading `.swift-version` via
200-
`swift_version_file`, because the latter makes the module extension read a
201-
`//unified/...` label, which fails when this repo is consumed as a dependency
202-
module.)
164+
Versions are pinned in the root `MODULE.bazel` and nowhere else: the
165+
`swift_version` literal on `swift.toolchain(...)` selects the hermetic swift.org
166+
**Linux** toolchain, and `bazel_dep(name = "swift-syntax", ...)` selects the
167+
`swift-syntax` release. On **macOS** the compiler version is *not* pinned:
168+
`rules_swift` auto-registers the host `xcode_swift_toolchain`, so it follows
169+
whichever Swift ships with the installed Xcode.
203170

204171
## Usage
205172

@@ -213,22 +180,20 @@ println!("{json}");
213180
CLI (reads a file argument or stdin, prints the syntax tree as JSON):
214181

215182
```sh
216-
echo 'let x = 1' | cargo run --bin swift-syntax-parse
183+
echo 'let x = 1' | bazel run //unified/swift-syntax-rs:swift-syntax-parse
217184
```
218185

219186
## Converting to a yeast AST
220187

221188
The JSON tree is consumed by the CodeQL extractor, which converts it into a
222189
[`yeast::Ast`](../../shared/yeast) — the in-memory format its rewrite rules
223190
operate on. That adapter is a pure-Rust module living in the extractor
224-
(`unified/extractor/src/languages/swift/adapter.rs`), so the extractor never
225-
needs the Swift toolchain: it consumes the JSON produced out-of-process by this
226-
crate's `parse_to_json` / the `swift-syntax-parse` binary.
191+
(`unified/extractor/src/languages/swift/adapter.rs`); the extractor links this
192+
crate and calls `parse_to_json` in-process.
227193

228194
## Layout
229195

230-
- `swift/` — Swift package exposing the `ssr_parse_json` / `ssr_string_free` C ABI.
231-
- `build.rs` — builds the Swift package and emits link/rpath flags (local `cargo` only).
232-
- `BUILD.bazel` — Bazel targets for the hermetic CI build (swift_library + rust targets).
196+
- `swift/` — Swift sources exposing the `ssr_parse_json` / `ssr_string_free` C ABI.
197+
- `BUILD.bazel` — the build (swift_library + rust targets).
233198
- `src/lib.rs` — safe Rust bindings (`parse_to_json`).
234199
- `src/main.rs` — demo CLI.

unified/swift-syntax-rs/build.rs

Lines changed: 0 additions & 120 deletions
This file was deleted.

unified/swift-syntax-rs/swift/Package.resolved

Lines changed: 0 additions & 15 deletions
This file was deleted.

unified/swift-syntax-rs/swift/Package.swift

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)