Skip to content

Commit 4aa661a

Browse files
tausbnCopilot
andcommitted
swift-syntax-rs: Degrade gracefully without a Swift toolchain
`swift-syntax-rs` is a workspace member, so its build script runs on a plain `cargo check`/`fmt`/`clippy` at the repo root. Previously it panicked when `swift build` could not be run, breaking those Swift-free workflows for anyone without a Swift toolchain. Instead, when `swift build` cannot be spawned, emit a `cargo:warning` and skip the link directives rather than panicking. `cargo check`/`fmt`/`clippy` don't link, so they keep working; only `cargo build`/`cargo test` then fail, at link time — which is fair, since those genuinely need Swift (and CI builds go through Bazel). A Swift toolchain that is present but whose build fails is still surfaced as a hard error. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8fbe908 commit 4aa661a

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

unified/swift-syntax-rs/build.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,34 @@ fn main() {
2424
println!("cargo:rerun-if-env-changed=SWIFTC");
2525

2626
// Build the Swift FFI package as a release dynamic library.
27+
//
28+
// Degrade gracefully when there is no runnable Swift toolchain. This crate
29+
// is a workspace member, so a plain `cargo check`/`fmt`/`clippy` at the repo
30+
// root runs this build script; if `swift build` cannot even be spawned we
31+
// emit a warning and skip the link directives rather than panicking, so
32+
// those Swift-free workflows keep working. Only `cargo build`/`cargo test`
33+
// then fail — at link time, which is fair: they genuinely need Swift (and CI
34+
// builds go through Bazel anyway). A Swift toolchain that *is* present but
35+
// whose build fails is still surfaced as a hard error below.
2736
let mut command = Command::new(swift_bin());
2837
command
2938
.args(["build", "-c", "release"])
3039
.current_dir(&swift_dir);
3140
apply_bare_repository_workaround(&mut command);
32-
let status = command.status().unwrap_or_else(|e| {
33-
panic!(
34-
"failed to run `{swift} build`: {e}\n\
35-
Install a Swift toolchain (see https://www.swift.org/install/, e.g. via \
36-
swiftly) and ensure `swift` is on PATH, or set the `SWIFT` environment \
37-
variable to the `swift` executable. The pinned version is in `.swift-version`.",
38-
swift = swift_bin(),
39-
)
40-
});
41+
let status = match command.status() {
42+
Ok(status) => status,
43+
Err(e) => {
44+
println!(
45+
"cargo:warning=skipping the Swift FFI build: failed to run `{swift} build`: {e}. \
46+
Install a Swift toolchain (see https://www.swift.org/install/, e.g. via swiftly) \
47+
and ensure `swift` is on PATH, or set the `SWIFT` environment variable, to build \
48+
or test this crate. `cargo check`/`fmt`/`clippy` work without it. The pinned \
49+
version is in `.swift-version`.",
50+
swift = swift_bin(),
51+
);
52+
return;
53+
}
54+
};
4155
assert!(status.success(), "`swift build` failed");
4256

4357
// Link against the freshly built dynamic library.

0 commit comments

Comments
 (0)