Skip to content

Commit bea63de

Browse files
authored
docs: remove/adjust references to Swift gazelle plugin (#1540)
Adjust the documentation now that the [Swift gazelle plugin](https://github.com/cgrindel/swift_gazelle_plugin) has its own repository. Related to #924.
1 parent d469217 commit bea63de

File tree

4 files changed

+24
-213
lines changed

4 files changed

+24
-213
lines changed

README.md

+20-76
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Gazelle Plugin for Swift and Swift Package Rules for Bazel
1+
# Swift Package Manager Rules for Bazel
22

33
[![Build](https://github.com/cgrindel/rules_swift_package_manager/actions/workflows/ci.yml/badge.svg?event=schedule)](https://github.com/cgrindel/rules_swift_package_manager/actions/workflows/ci.yml)
44

5-
This repository contains a [Gazelle plugin] and Bazel repository rules that can be used to download,
6-
build, and consume Swift packages. The rules in this repository build the external Swift packages
7-
using [rules_swift] and native C/C++ rulesets making the Swift package products and targets
8-
available as Bazel targets.
5+
This repository contains a Bazel ruleset that can be used to download, build, and consume Swift
6+
packages. The rules in this repository build the external Swift packages using [rules_swift],
7+
[rules_apple] and native C/C++ rulesets making the Swift package products and targets available as
8+
Bazel targets.
99

1010
This repository is designed to fully replace [rules_spm] and provide utilities to ease Swift
1111
development inside a Bazel workspace.
@@ -25,11 +25,9 @@ development inside a Bazel workspace.
2525
* [3. Create a minimal `Package.swift` file.](#3-create-a-minimal-packageswift-file)
2626
* [4. Run `swift package update`](#4-run-swift-package-update)
2727
* [5. Run `bazel mod tidy`.](#5-run-bazel-mod-tidy)
28-
* [6. Add Gazelle targets to `BUILD.bazel` at the root of your workspace.](#6-add-gazelle-targets-to-buildbazel-at-the-root-of-your-workspace)
29-
* [7. Create or update Bazel build files for your project.](#7-create-or-update-bazel-build-files-for-your-project)
30-
* [8. Build and test your project.](#8-build-and-test-your-project)
31-
* [9. Check in `Package.swift`, `Package.resolved`, and `MODULE.bazel`.](#9-check-in-packageswift-packageresolved-and-modulebazel)
32-
* [10. Start coding](#10-start-coding)
28+
* [6. Build and test your project.](#6-build-and-test-your-project)
29+
* [7. Check in `Package.swift`, `Package.resolved`, and `MODULE.bazel`.](#7-check-in-packageswift-packageresolved-and-modulebazel)
30+
* [8. Start coding](#8-start-coding)
3331
* [Using a Swift package registry](#using-a-swift-package-registry)
3432
* [Tips and Tricks](#tips-and-tricks)
3533
<!-- MARKDOWN TOC: END -->
@@ -55,8 +53,8 @@ Don't forget that `rules_swift` [expects the use of
5553
`clang`](https://github.com/bazelbuild/rules_swift#3-additional-configuration-linux-only). Hence,
5654
you will need to specify `CC=clang` before running Bazel.
5755

58-
Finally, help [rules_swift] and [rules_swift_package_manager] find the Swift toolchain by ensuring that a `PATH`
59-
that includes the Swift binary is available in the Bazel actions.
56+
Finally, help [rules_swift] and [rules_swift_package_manager] find the Swift toolchain by ensuring
57+
that a `PATH` that includes the Swift binary is available in the Bazel actions.
6058

6159
```sh
6260
cat >>local.bazelrc <<EOF
@@ -89,9 +87,11 @@ common --enable_bzlmod
8987
Add a dependency on `rules_swift_package_manager`.
9088

9189
<!-- BEGIN MODULE SNIPPET -->
90+
9291
```python
9392
bazel_dep(name = "rules_swift_package_manager", version = "0.47.2")
9493
```
94+
9595
<!-- END MODULE SNIPPET -->
9696

9797
In addition, add the following to load the external dependencies described in your `Package.swift`
@@ -173,7 +173,7 @@ swift_deps.from_package(
173173

174174
#### (Optional) Enable `swift_deps_info` generation for the Gazelle plugin
175175

176-
If you will be using the Gazelle plugin for Swift, you will need to enable the generation of
176+
If you will be using the [Gazelle plugin for Swift], you will need to enable the generation of
177177
the `swift_deps_info` repository by enabling `declare_swift_deps_info`.
178178

179179
```bazel
@@ -184,8 +184,6 @@ swift_deps.from_package(
184184
)
185185
```
186186

187-
You will also need to add a dependency on [Gazelle](https://registry.bazel.build/modules/gazelle).
188-
189187
### 3. Create a minimal `Package.swift` file.
190188

191189
Create a minimal `Package.swift` file that only contains the external dependencies that are directly
@@ -209,8 +207,8 @@ let package = Package(
209207
The name of the package can be whatever you like. It is required for the manifest, but it is not
210208
used by [rules_swift_package_manager]. If your project is published and consumed as a Swift package,
211209
feel free to populate the rest of the manifest so that your package works properly by Swift package
212-
manager. Just note that the Swift Gazelle plugin does not use the manifest to generate Bazel build
213-
files, at this time.
210+
manager. Just note that the [Swift Gazelle plugin] does not use the manifest to generate Bazel build
211+
files.
214212

215213
### 4. Run `swift package update`
216214

@@ -221,71 +219,23 @@ This will invoke Swift Package Manager and resolve all dependencies resulting in
221219

222220
This will update your `MODULE.bazel` with the correct `use_repo` declaration.
223221

224-
### 6. Add Gazelle targets to `BUILD.bazel` at the root of your workspace.
225-
226-
Add the following to the `BUILD.bazel` file at the root of your workspace.
227-
228-
```bzl
229-
load("@gazelle//:def.bzl", "gazelle", "gazelle_binary")
230-
231-
# Ignore the `.build` folder that is created by running Swift package manager
232-
# commands. Be sure to configure your source control to ignore it, as well.
233-
# (i.e., add it to your `.gitignore`).
234-
# NOTE: Swift package manager is not used to build any of the external packages.
235-
# gazelle:exclude .build
236-
237-
# This declaration builds a Gazelle binary that incorporates all of the Gazelle
238-
# plugins for the languages that you use in your workspace. In this example, we
239-
# are only listing the Gazelle plugin for Swift from rules_swift_package_manager.
240-
gazelle_binary(
241-
name = "gazelle_bin",
242-
languages = [
243-
"@rules_swift_package_manager//gazelle",
244-
],
245-
)
246-
247-
# This target updates the Bazel build files for your project. Run this target
248-
# whenever you add or remove source files from your project. The
249-
# `swift_deps_info` repository is generated by `rules_swift_package_manager`. It
250-
# creates a target, `@swift_deps_info//:swift_deps_index`, that generates a JSON
251-
# file which maps Swift module names to their respective Bazel target.
252-
gazelle(
253-
name = "update_build_files",
254-
data = [
255-
"@swift_deps_info//:swift_deps_index",
256-
],
257-
extra_args = [
258-
"-swift_dependency_index=$(location @swift_deps_info//:swift_deps_index)",
259-
],
260-
gazelle = ":gazelle_bin",
261-
)
262-
```
263-
264-
### 7. Create or update Bazel build files for your project.
265-
266-
Generate/update the Bazel build files for your project by running the following:
267-
268-
```sh
269-
bazel run //:update_build_files
270-
```
271-
272-
### 8. Build and test your project.
222+
### 6. Build and test your project.
273223

274224
Build and test your project.
275225

276226
```sh
277227
bazel test //...
278228
```
279229

280-
### 9. Check in `Package.swift`, `Package.resolved`, and `MODULE.bazel`.
230+
### 7. Check in `Package.swift`, `Package.resolved`, and `MODULE.bazel`.
281231

282232
- The `Package.swift` file is used by `rules_swift_package_manager` to generate information about
283233
your project's dependencies.
284234
- The `Package.resolved` file specifies that exact versions of the downloaded dependencies that were
285235
identified.
286236
- The `MODULE.bazel` contains the declarations for your external dependencies.
287237

288-
### 10. Start coding
238+
### 8. Start coding
289239

290240
You are ready to start coding.
291241

@@ -297,14 +247,6 @@ See [our document on using a Swift package registry](/docs/swift_package_registr
297247

298248
The following are a few tips to consider as you work with your repository:
299249

300-
- When you add or remove source files, run `bazel run //:update_build_files`. This will
301-
create/update the Bazel build files in your project. It is designed to be fast and unobtrusive.
302-
- If things do not appear to be working properly, run the following:
303-
- `bazel run //:update_build_files`
304-
- Do yourself a favor and create a Bazel target (e.g., `//:tidy`) that runs your repository
305-
maintenance targets (e.g., `//:update_build_files`, formatting utilities)
306-
in the proper order. If you are looking for an easy way to set this up, check out the
307-
[`//:tidy` declaration in this repository](BUILD.bazel) and the documentation for the [tidy] macro.
308250
- Are you trying to use a Swift package and it just won't build under Bazel? If you can figure out
309251
how to fix it, you can patch the Swift package. Check out [our document on patching Swift packages].
310252

@@ -317,6 +259,8 @@ The following are a few tips to consider as you work with your repository:
317259
[CI GitHub workflow]: .github/workflows/ci.yml
318260
[Gazelle plugin]: https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.md
319261
[Gazelle]: https://github.com/bazelbuild/bazel-gazelle
262+
[Gazelle plugin for Swift]: https://github.com/cgrindel/swift_gazelle_plugin
263+
[Swift Gazelle plugin]: https://github.com/cgrindel/swift_gazelle_plugin
320264
[examples]: examples/
321265
[rules_apple]: https://github.com/bazelbuild/rules_apple
322266
[rules_spm]: https://github.com/cgrindel/rules_spm

docs/design/BUILD.bazel

-6
This file was deleted.

docs/design/high-level.md

-60
This file was deleted.

docs/faq.md

+4-71
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,15 @@
33
## Table of Contents
44

55
<!-- MARKDOWN TOC: BEGIN -->
6-
* [Why use Gazelle and Go?](#why-use-gazelle-and-go)
7-
* [Why split the implementation between Go and Starlark?](#why-split-the-implementation-between-go-and-starlark)
8-
* [How does the Gazelle plugin for Go handle this?](#how-does-the-gazelle-plugin-for-go-handle-this)
9-
* [Is the same build file generation logic used for the Go/Gazelle and Starlark implementations?](#is-the-same-build-file-generation-logic-used-for-the-gogazelle-and-starlark-implementations)
10-
* [Does this replace rules_spm?](#does-this-replace-rules_spm)
11-
* [Can I migrate from rules_spm to `rules_swift_package_manager`?](#can-i-migrate-from-rules_spm-to-rules_swift_package_manager)
12-
* [Can I just manage my external Swift packages and not generate Bazel build files for my project?](#can-i-just-manage-my-external-swift-packages-and-not-generate-bazel-build-files-for-my-project)
6+
* [Does this replace [rules_spm]?](#does-this-replace-rules_spm)
7+
* [Where is the Swift gazelle plugin?](#where-is-the-swift-gazelle-plugin)
138
* [After running `//:swift_update_pkgs`, I see a `.build` directory. What is it? Do I need it?](#after-running-swift_update_pkgs-i-see-a-build-directory-what-is-it-do-i-need-it)
14-
* [Does the Gazelle plugin run Swift package manager with every execution?](#does-the-gazelle-plugin-run-swift-package-manager-with-every-execution)
159
* [Can I store the Swift dependency files in a sub-package (i.e., not in the root of the workspace)?](#can-i-store-the-swift-dependency-files-in-a-sub-package-ie-not-in-the-root-of-the-workspace)
16-
* [Why does this happen?](#why-does-this-happen)
1710
* [How do I handle the error `Unable to resolve byName reference XXX in @swiftpkg_yyy.`?](#how-do-i-handle-the-error-unable-to-resolve-byname-reference-xxx-in-swiftpkg_yyy)
1811
* [How do I fix this issue?](#how-do-i-fix-this-issue)
1912
* [What is the cause of the error? Why can't `rules_swift_package_manager` handle this situation?](#what-is-the-cause-of-the-error-why-cant-rules_swift_package_manager-handle-this-situation)
2013
<!-- MARKDOWN TOC: END -->
2114

22-
## Why use Gazelle and Go?
23-
24-
The [Gazelle framework] provides lots of great features for generating Bazel build and Starlark
25-
files. Right now, the best way to leverage the framework is to write the plugin in Go.
26-
27-
In addition, adoption of the Gazelle ecosystem has started to take off. There are [lots of useful
28-
plugins for other languages](https://github.com/bazelbuild/bazel-gazelle#supported-languages).
29-
Letting Gazelle generate and maintain Bazel build files is a real game changer for developer
30-
productivity.
31-
32-
## Why split the implementation between Go and Starlark?
33-
34-
As mentioned previously, the easiest way to implement a Gazelle plugin is to write it in Go. This
35-
works great for generating build files in the primary workspace. However, there is a chicken-and-egg
36-
problem when it comes time to generate build files in a repository rule. The repository rule needs
37-
to generate files during the [loading phase]. The Go toolchain and the Gazelle framework defined in
38-
the workspace are not available to the repository rule during this phase. So, one needs to either
39-
perform some gymnastics to build the Gazelle plugin (see below) or use a language/runtime that is
40-
guaranteed to be available during the loading phase. Since Starlark is available during the loading
41-
phase, the build file generation logic for the repository rules is implemented in Starlark.
42-
43-
### How does the Gazelle plugin for Go handle this?
44-
45-
In short, they assume that if you are using the Gazelle plugin for Go, then you must have a Go
46-
toolchain installed on the host system. In essence, they shell out and run Go from the system.
47-
48-
## Is the same build file generation logic used for the Go/Gazelle and Starlark implementations?
49-
50-
No. The Gazelle plugin inspects the Swift source files and the directory structure to determine the
51-
placement and content of the Bazel build files. The repository rules leverage information about the
52-
Swift packages (e.g., dump and describe JSON). However, both implementations use the
53-
`module_index.json` to resolve module references to Bazel targets for the external dependencies.
54-
5515
## Does this replace [rules_spm]?
5616

5717
Yes. There are some [limitations with the rules_spm
@@ -63,14 +23,9 @@ features and improvements:
6323
- Build the external dependencies with [rules_swift].
6424
- Pin the exact versions for the direct and transitive dependencies.
6525

66-
## Can I migrate from [rules_spm] to `rules_swift_package_manager`?
67-
68-
Absolutely. A [migration guide from rules_spm](https://github.com/cgrindel/rules_swift_package_manager/issues/99) is
69-
on the roadmap.
26+
## Where is the Swift gazelle plugin?
7027

71-
## Can I just manage my external Swift packages and not generate Bazel build files for my project?
72-
73-
Yes. Just omit the `//:update_build_files` target that is mentioned in the [quickstart].
28+
It has moved to https://github.com/cgrindel/swift_gazelle_plugin.
7429

7530
## After running `//:swift_update_pkgs`, I see a `.build` directory. What is it? Do I need it?
7631

@@ -80,28 +35,11 @@ commands. These commands result in a `.build` directory being created. The direc
8035
effect of running the Swift package manager commands. It can be ignored and should not be checked
8136
into source control. It is not used by the Gazelle plugin or the Starlark repository rules.
8237

83-
## Does the Gazelle plugin run Swift package manager with every execution?
84-
85-
No. The Gazelle plugin only executes the Swift package manager when run in `update-repos` mode. This
86-
mode only needs to be run when modifying your external dependencies (e.g., add/remove a dependency,
87-
update the version selection for a dependency). The `update` mode for the Gazelle plugin generates
88-
Bazel build files for your project. It uses information written to the `swift_deps_index.json` and
89-
the source files that exist in your project to generate the Bazel build files.
90-
9138
## Can I store the Swift dependency files in a sub-package (i.e., not in the root of the workspace)?
9239

9340
Yes. The [vapor example] demonstrates storing the Swift dependency files in a sub-package called
9441
`swift`.
9542

96-
### Why does this happen?
97-
98-
This can happen with some Swift packages (e.g. `firebase-ios-sdk`). [rules_xcodeproj removes the
99-
`sandboxed` spawn
100-
strategy](https://github.com/MobileNativeFoundation/rules_xcodeproj/blob/6c186331c82f3cbc82e2e7fdfacb4873e409e094/xcodeproj/internal/templates/xcodeproj.bazelrc#L66-L68)
101-
in their default build configuration due to slow performance of the MacOS sandbox. The above bazelrc
102-
stanza adds it back. [An issue](https://github.com/cgrindel/rules_swift_package_manager/issues/712)
103-
exists tracking the work to allow these Swift packages to be built using the `local` spawn strategy.
104-
10543
## How do I handle the error `Unable to resolve byName reference XXX in @swiftpkg_yyy.`?
10644

10745
tl;dr A transitive dependency uses a by-name product reference format that was deprecated in Swift
@@ -170,13 +108,8 @@ the new architecture:
170108
- As a result, `byName` lookups can only be resolved to the targets/products in the current package
171109
and to products in Swift packages where the identity exactly matches the product name.
172110

173-
[--strategy_regexp]: https://bazel.build/reference/command-line-reference#flag--strategy_regexp
174-
[Gazelle framework]: https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.md
175111
[Patch the Swift package]: /docs/patch_swift_package.md
176-
[loading phase]: https://bazel.build/run/build#loading
177112
[patch this file]: /docs/patch_swift_package.md
178113
[pusher-websocket-swift Package.swift]: https://github.com/pusher/pusher-websocket-swift/blob/886341f9dad453c9822f2525136ee2006a6c3c9e/Package.swift
179-
[quickstart]: https://github.com/cgrindel/rules_swift_package_manager/blob/main/README.md#quickstart
180-
[rules_spm]: https://github.com/cgrindel/rules_spm/
181114
[rules_swift]: https://github.com/bazelbuild/rules_swift
182115
[vapor example]: /examples/vapor_example

0 commit comments

Comments
 (0)