Chuck Grindel’s Technical Musings

Technical articles about Bazel, Swift, and software development.
Swift Package Manager and Bazel

How to Use Swift Packages in Bazel Using swift_bazel

What is swift_bazel? The swift_bazel project combines a Gazelle extension with Bazel repository rules (swift_package, local_swift_package). The Gazelle extension inspects source files, resolves external Swift package dependencies, writes package declarations, and optionally generates build file declarations for projects. During builds, the swift_package repository rule downloads Swift packages and generates corresponding Bazel build files. What is a Gazelle Extension? “Gazelle is a Bazel build file generator” that began supporting Go but now includes a framework for extending it to other languages. The swift_bazel project leverages this framework to support Swift packages. ...

January 11, 2023 · 3 min

How to Format Bazel Starlark Files Using bzlformat

The Problem Bazel projects benefit from consistent code formatting. While Buildifier handles formatting when run manually through text editors, this approach lacks reliability across teams and automated processes. Code from various sources—scripts, contributors without Buildifier installed—can bypass formatting standards. Continuous integration typically catches formatting issues, but this feedback arrives late in the development cycle. Wouldn’t it be great to get this feedback when building and testing your code before submitting it to the CI system? ...

February 2, 2022 · 2 min
Copy Files from Bazel Output to Source

How to Copy Bazel Build Outputs to Your Source Directory

Overview This article explains how to use the updatesrc tools from the cgrindel/bazel-starlib repository to automatically copy Bazel build outputs back to your source directory. This is useful for generated documentation, formatted code, and similar artifacts. Adding updatesrc to Your Workspace First, add the cgrindel/bazel-starlib dependency to your WORKSPACE file: load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "cgrindel_bazel_starlib", sha256 = "fc2ee0fce914e3aee1a6af460d4ba1eed9d82e8125294d14e7d3f236d4a10a5d", strip_prefix = "bazel-starlib-0.3.2", urls = [ "http://github.com/cgrindel/bazel-starlib/archive/v0.3.2.tar.gz", ], ) load("@cgrindel_bazel_starlib//:deps.bzl", "bazel_starlib_dependencies") bazel_starlib_dependencies() load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") bazel_skylib_workspace() Two Approaches to Specify Files Option 1: Using srcs and outs Attributes Define an updatesrc_update target with explicit source and output file mappings: ...

October 28, 2021 · 2 min
SwiftFormat and Bazel

How to Integrate SwiftFormat into Bazel

Good Code Hygiene Consistent coding styles enhance readability and collaboration. Consistently using the same style throughout your code makes it easier to read and reduces mistakes when working together. Automated tools like linters and formatters help teams maintain these standards throughout development and CI/CD processes. Formatting Swift Code in Bazel This guide assumes you’re using Swift with Bazel and rules_swift, have selected nicklockwood/SwiftFormat as your formatter, and created a team configuration file. ...

October 19, 2021 · 2 min
Swift Package Manager and Bazel

How to Build and Run Swift Package Binaries in Bazel

Introduction This article demonstrates leveraging Swift packages containing executable products within Bazel builds using rules_spm. The post covers building and executing utilities like SwiftLint and SwiftFormat as part of your project’s development workflow. Note: The rules_spm project has been superseded by swift_bazel, with updated guidance available in a separate blog post. Executables in Swift Packages Swift packages extend beyond library modules to include executable products. Notable examples include Realm’s SwiftLint (code linting) and Nick Lockwood’s SwiftFormat (automated code formatting). ...

October 7, 2021 · 2 min
Swift Package Manager and Bazel

How to Use External Swift Packages in Bazel

What is rules_spm? The rules_spm project provides Bazel rules and macros enabling projects to depend on external Swift packages. It functions similarly to Swift Package Manager (SPM) but integrates with Bazel’s build system. The tool downloads external packages, builds them using SPM, and makes artifacts available as Bazel binary or library targets for consumption by rules_swift rules. A Simple Example This walkthrough demonstrates importing Apple’s Logging module from the swift-log package into a Bazel-built Swift binary. ...

September 27, 2021 · 2 min