Here's a SwiftUI view that compiles, passes code review and ships:
@Observable final class Cart { var items: [String] = [] } struct CartBadge: View { @Environment(Cart.self) private var cart var body: some View { Text("\(cart.items.count)") } } struct ContentView: View { var body: some View { NavigationStack { Text("Shop") .toolbar { CartBadge() } } } } @main struct ShopApp: App { var body: some Scene { WindowGroup { ContentView() } } }
The moment that toolbar appears, the app crashes. Nobody above CartBadge ever called .environment(Cart()), and SwiftUI only finds out at runtime. The compiler had no opinion. It never does.
In a demo app you find this in five minutes. In a real codebase, with hundreds of @Environment call sites spread across modules, sheets and navigation stacks, you find it when a user does.
I went looking for a way to make this compile-time safe. Every answer was the same: pass the value through init, make it optional, give it a default. All reasonable advice. All of it starts with "first, change 300 call sites." We weren't going to do that.
So I built Snagless. It reads your SwiftUI source, works out which views show which, and fails the build when a view can appear without something it needs:
ShopApp.swift:26:13: error: `CartBadge` needs `Cart`: App `ShopApp` → ContentView → CartBadge (ShopApp.swift:6)
Zero changes to your code. Add it to the build and the crash turns into a red line in Xcode.
Why Apple Won't Fix This
The environment is resolved at runtime, by walking up a view tree that only exists at runtime. At compile time, all the type system sees is some View. And honestly, I get why Apple hasn't fixed it. Any check like this needs heuristics, Swift compiles one module at a time, and the environment is dynamic on purpose. Apple's answer is API: optional values, keys with defaults. The non-optional form is a force unwrap with extra steps. If it crashes, that's on you.
Fair enough. A compiler can't ship "usually right." A tool you opt into can.
The SafeDI Idea
The idea came from SafeDI by Dan Federman, which validates your dependency graph at build time with a SwiftSyntax plugin. It proved this kind of tool can be fast enough for real projects. SafeDI wants you to design your dependencies its way, and if you can adopt it, that's the stronger guarantee. Our codebase already leaned on @Environment everywhere, so I pointed the same idea at SwiftUI's environment as it's already written.
How It Works
Snagless never imports SwiftUI and never type-checks anything. To the parser, @Environment(Cart.self) is just an attribute and .environment(cart) is just a function call. That's the whole trick, and it's why it's fast.
It reads every file on its own, in parallel, then puts them together into one graph of which view shows which, across modules. Wherever views start (your App, previews, widgets, hosting controllers), anything still needed and not provided becomes an error, with the path down to the view that wanted it. If it can't reach a view at all, it warns you instead of quietly skipping it. A clean run means checked, not skipped.
It doesn't guess. That's the rule I care about most. When Snagless can't tell what type a value is, it says so instead of assuming. .environment(container.resolve()) gets listed as unresolved, and you tell it what it is:
.environment(container.resolve()) // snagless: provides Cart
Making SwiftUI Prove It
Here's the thing about a tool like this: it's only as right as its idea of how SwiftUI works. If that idea is just my assumptions, Snagless becomes a very fast way to enforce my bugs.
So every rule Snagless believes about SwiftUI is backed by a real case that runs on the simulator. RuntimeCases is an app with 53 of them. Each one is built around one way of showing a view or providing a value, and each one either crashes or renders. On the iOS 27 simulator, 31 crash and 22 render. That's the ground truth Snagless is measured against.
Some of it surprised me:
- A
navigationDestinationdoesn't see what you'd expect. A pushed view gets itsNavigationStack's environment, plus whatever its own closure provides. Values injected between the stack and the view that declares the destination never reach it. Seven cases to pin that down. - An environment key whose default calls
fatalErrorisn't safe even when you set it. On iOS 27, SwiftUI can run the default when the key is set, not only when it's read without being set. Setting it everywhere doesn't save you. - Hosting controllers don't all behave the same. One returned from
makeUIViewControlleris part of the SwiftUI view around it and inherits its environment. One you create and present from UIKit starts empty. So does a widget.
30 out of 31
Against SwiftUI. Of the 31 RuntimeCases that crash, Snagless reports 30. The one it misses is a value provided inside an #if. Of the 22 that render, it flags 8: 2 are right for the app, 2 are by design, and 4 are real false positives I haven't fixed yet.
Against generated apps. The test suite generates apps of 500 and 2,000 views with crashes added at random, and Snagless has to report exactly those crashes and nothing else.
Against a real app. IceCubes is an open-source Mastodon client: 431 Swift files and 212 views. Snagless reaches all 212 views, leaves no value unresolved, and reports 39 problems. I checked every one by hand. 37 are in previews, and I ran each of them on the iOS 27 simulator with everything else the preview needs provided. 27 crash. The other 10 render, because the view that needs the value only shows up once data arrives that the preview doesn't have. The last 2 are in the share extension, on a path it skips, and an ignore comment handles them.
Speed. Median of seven runs on an M1 Pro:
| Project | Time |
|---|---|
| IceCubes, 431 files | 92 ms |
| Generated app, 500 views | 37 ms |
| Generated app, 2,000 views | 126 ms |
| Generated app, 10,000 views | 584 ms |
What it misses. The big ones: a value provided inside #if counts in every build, macros aren't expanded, and binary frameworks have no source to read. The full list is in the README. Better you read it there than find it in production.
Is This Vibe Coding?
In my last post I made a point of saying WatchLink was built in 2023, before LLMs were writing code for people. Two engineers and a lot of stubbornness.
This one is the opposite: one engineer, two AI sessions, and a lot of me saying "show me." I designed Snagless in conversation with Claude, mostly on my phone, and built it with Claude Code. Doing it by hand would have been crazy. Snagless is a pile of edge cases: every way SwiftUI code can show a view or provide a value. An agent can grind through those. What it can't do is decide what's true.
So is it vibe coding? No. The difference is what happened every time the AI was wrong. It was wrong a lot, and always with total confidence. Before writing this, I asked both sessions, the design chat and the builder, to list every time they'd been wrong and what caught them. They came back with about thirty entries, which is either very humble or very honest. They fall into three groups.
Caught by Me
- It tried to build Snagless in its own sandbox. One CPU core, 3 GB of RAM, compiling swift-syntax. The VM ran out of memory and rebooted itself mid-build. I said "just give me the prompt" and ran it on my Mac like a normal person.
- The first answer was the generic one. Pass the value through
init, make it optional. Great advice for a codebase with three call sites. I asked for something that didn't touch 300, and that's where Snagless started. - It built ahead of the evidence. The builder was halfway through a full
#ifevaluator, one analysis per build configuration. I asked one question: has this ever mattered? It scanned IceCubes: 293#ifblocks, 4 that touch the environment, 0 that would change a finding. It deleted the work.#ifis still first on the 1.0 roadmap, but now it gets built because of a real miss, not because it seemed like a good idea at the time.
Caught by the Rules I Set
- Its model of SwiftUI was wrong in three places. It was sure a hosting controller inside a representable starts empty, that a
navigationDestinationinherits like any child, and that setting a key with afatalErrordefault makes it safe. The simulator disagreed all three times. No rule goes in without a RuntimeCases case, so the simulator wins every argument. - A guess that produced confident wrong answers. The first draft assumed anything shaped like
Cart.sharedreturns aCart. SoStoreFactory.makeCart()was taken to provide aStoreFactory, which nobody has ever wanted in their environment. Tests caught it. Now Snagless says "unresolved" instead of guessing. - A cache that made it slower. Warm runs: 240 ms. No cache: 90 ms. Measured, then deleted.
- Bugs nobody was looking for. A review agent I asked for found that a typo'd path checked zero files and passed, and that the Xcode plugin watched too few folders. Both meant a green build that missed crashes. Both are fixed.
- Three silent misses, found by a second AI reading the code. Before launch I asked the design chat to review the analyzer itself. It flagged custom
Scenetypes,Viewconformance declared in an extension, and optional values counted as provided: three ways a crash could hide behind a clean build. Tests confirmed every one before anything was fixed, and RuntimeCases showed a nil optional really does crash. - "Done" that wasn't. About seven times, the builder announced something was finished without re-running the checks after its last edit. A stop hook I set up sent it back every time. It's "works on my machine," minus the machine.
Caught by the AI, Only by Running Things
- Members inside
#ifblocks were skipped entirely, so an@Environmentdeclared inside one was never checked. A missed crash. The builder found it by writing the test before the fix.
The pattern is hard to miss. The AI almost never caught a mistake by thinking harder. It caught them by running something: a test, a simulator case, a real build. My job was making sure it had to. Every SwiftUI rule needs a simulator case. Every number is measured. "Done" isn't accepted until the checks run again. When the tool isn't sure, it says unresolved.
The AI wrote most of the code: about 4,500 lines of Swift and 125 tests, in Swift 6 language mode with warnings treated as errors. The decisions about what counts as true were mine. That's the line between building with AI and vibe coding.
Snagless
Snagless is open source on GitHub. It runs as a SwiftPM build tool plugin, in Swift packages and Xcode projects, or from the command line on macOS and Linux.
In a Swift package:
dependencies: [ .package(url: "https://github.com/tareksabry1337/snagless", from: "0.10.0"), ], targets: [ .target( name: "MyApp", plugins: [.plugin(name: "SnaglessPlugin", package: "snagless")] ), ]
In an Xcode project, add the package and add SnaglessPlugin to your app target's Run Build Tool Plug-ins. It needs Xcode 16 or later and ships as a prebuilt binary, so your clean builds don't compile swift-syntax.
On CI you don't even need a Mac. Each release has static Linux binaries for x86_64 and arm64, and with --format github every problem shows up as an annotation on its line in the pull request. The plugin turns that on by itself on GitHub Actions. The README has a ready-made workflow.
What's Next
Next up for 1.0: #if support, since that's the one crash it misses today, then the deployment target, binary frameworks and macros, and the false positives. Plus a Bitrise step and brew install snagless. The roadmap has the full order.
If Snagless reports something wrong, or misses something, that's the most useful issue you can file. Two or three views that reproduce it is usually enough.
SwiftUI crashes, caught at build time. Go break your build before your users do.