A Bazel port of ExampleTuistIntegration, demonstrating SafeDI's
cross-module .safedi artifact handoff in a Bazel-native way. The
Swift source code (types, mocks, previews) is the Tuist example's,
except for the Models module, Subproject's seam WindowTitle, the
TitleFormatter it builds, and what uses it, NoteStorage's
NoteFormatter, EventLogger, which also checks NDEBUG and logs its
own example's name, and a comment in NameEntryView on how a mismatch
is reported. The Tuist example, built with SafeDI's latest release,
adopts the seam once a release ships seams.
Supported host: macOS arm64 only (matches the
rules_apple macOS
toolchain). The SafeDI rules themselves are platform-agnostic.
- Three
swift_librarytargets (//Models:Models,//Subproject:Subproject,//ExampleBazelIntegration:ExampleBazelIntegration) that compile separately. - Each module lists the modules it depends on directly.
SubprojectimportsModels, so itssafedi_depslistsModels_safedi. The app imports onlySubproject, so itssafedi_depslists onlySubproject_safedi. The app's root buildsNoteStorage, which buildsModels'NoteFormatter, and that works becausesafedi_swift_librarycollects the modules beneath the ones a module lists, and SafeDI reads every one of them that declares a type a dependency tree of the module needs. A mock asks for none: it builds a deeper type only if a tree of its module uses that module or the module is listed. - A seam reaches across a
packagetype.Subproject's seamWindowTitle(generateDependencyTree: true) receivesModels'packagetypeNoteThemeonly if available, so the initializerSubprojectgenerates for it ispublicwithoutthemeandpackagewith it. SafeDI counts a type it reads no declaration of aspublic, so withoutModels_safediinSubproject'ssafedi_depsthepublicinitializer would take apackagetype and fail to compile.Subproject's.safedialso carries whatWindowTitle's signature needs ofModels. All three modules set the samepackage_name. safedi_swift_librarymacro —load("@safedi//bazel:safedi.bzl", "safedi_swift_library"). It takes aswift_library's attributes and declares two targets from them:<name>, theswift_library, which compilessrcstogether with the generated code.<name>_safedi, asafedi_compiletarget that runs SafeDITool once over the samesrcsand writes two outputs:<name>_safedi.safedi— module-info artifact for cross-module consumers (carried via the rule'sSafeDIInfoprovider).<name>_safedi.swift— combined dependency-tree + mocks + mock configuration source. With the manifestscanplans, as the SPM plugin uses, SafeDITool writes one generated.swiftper source file that declares a root, one per source file that declares agenerateMock: truetype, one peradditionalMocksToGenerateentry, and, when there are mocks, the shared mock configuration.--combined-outputconcatenates them into one statically-declared output instead — no hand-maintained output list regardless of how many@Instantiable(isRoot:)/generateMock: truetypes the module declares.
- One set of compilation conditions. Both targets receive the same
definesandcopts, so SafeDITool scans the#ifclauses the compiler compiles. SafeDITool also seesDEBUGindbg/fastbuildbuilds,NDEBUGinoptbuilds, and every-Dpassed with--@rules_swift//swift:copt. - Cross-module type resolution via
.safedi. The host target reaches the subproject viasafedi_deps = ["//Subproject:Subproject_safedi"]andModelsthrough it, and never re-parses their sources. - SafeDI consumed via
bazel_dep. MODULE.bazel declaresbazel_dep(name = "safedi", ...). In this repo the dep is resolved vialocal_path_overrideto the workspace root so the example tracks the working copy; downstream consumers would get SafeDI from the Bazel Central Registry instead. SafeDIToolbuilt from this checkout.safedi_compiletakes SafeDITool from a toolchain. A SafeDI release from the Bazel Central Registry runs the release's prebuilt binary for the build machine; this example'slocal_path_overridetakes SafeDI from the checkout, whosebazel/private/prebuilt_safeditool.bzlnames no binaries, so it builds@safedi//Sources/SafeDITool:SafeDIToolfrom source and tests the working copy's tool. CI checks that the generate step ran that target.
cd Examples/ExampleBazelIntegration
bazelisk build //...First build compiles swift-syntax + SafeDIMacros under the hood (~few minutes). Subsequent builds are incremental.
In a real downstream consumer, you'd edit bazel_dep(name = "safedi", version = "…")
in your MODULE.bazel. In this example, the version is effectively
whatever's checked out under ../.. (via local_path_override), so
no bump needed — just git pull the outer repo.
| Tuist | Bazel | |
|---|---|---|
| Codegen entry point | SafeDI.preCompileScript(module:dependencies:) Tuist plugin helper |
safedi_swift_library macro, which declares a safedi_compile rule and a swift_library |
| Cross-module handoff | Subproject.safedi in $(BUILT_PRODUCTS_DIR) consumed via script-phase input; a target that declares a root names every target beneath it in dependencies:, and one that declares a seam each target whose types its tree builds |
safedi_compile's SafeDIInfo provider; downstream modules list the producer's <name>_safedi label in safedi_deps, and the rule collects the modules beneath it |
| Input enumeration | FileListGlob in Project.swift |
glob() in BUILD.bazel |
| Output enumeration | SafeDITool scan at tuist generate time → .generated(…) per-file entries |
Single concatenated output file per module — no per-file enumeration needed |
| SafeDITool acquisition | tuist install pulls via SafeDI's prebuilt trait |
A toolchain: the release's prebuilt binary, or, for this example's local_path_override, built from source by Bazel |
| Generated code location | $(DERIVED_FILE_DIR) (Xcode build sandbox) |
bazel-bin/… (Bazel action sandbox) |
Both end up at the same shape: generated Swift is a build artifact,
never in the source tree; cross-module type info flows through a
.safedi artifact.
Bazel rule outputs must be known at analysis time (Starlark can't run
subprocesses or read file contents then). SafeDI's output set
depends on what's inside each Swift file — how many
@Instantiable(isRoot:) / generateMock: true declarations it
finds — which analysis can't see. Hand-maintaining the output list
in BUILD.bazel would force every author to update their build file
on every annotation change, so safedi_compile uses SafeDITool's
--combined-output mode to emit a single declared output regardless
of source contents. The concatenated file is still valid Swift
(SafeDI's generated files are all top-level declarations) and
downstream swift_library.srcs treats it like any other source.