r/iOSProgramming • u/V0RT3X_L33T_ • 3h ago
Article Open-sourced Snagless: SwiftUI @Environment crashes caught at build time, using SwiftSyntax
Our codebase has hundreds of `@Environment` call sites. Any one of them can crash in production with "No Observable object of type X found" if a view gets shown without the value injected somewhere above it, and the compiler can't see it. Every fix I found started with "first, change all your call sites."
So I built Snagless, a SwiftPM/Xcode build plugin. It reads your SwiftUI source with SwiftSyntax, works out which view shows which across modules, and fails the build when a view can appear without a value it needs. Zero code changes, about 90 ms on a 431-file app.
Built with Claude Code, and the write-up is upfront about every time the AI was wrong and what caught it.
Write-up: https://tarek-builds.dev/p/swiftui-environment-crashes-compile-just-fine-so-i-made-them-fail-the-build/
Repo: https://github.com/tareksabry1337/snagless
Happy to answer questions.
1
u/jaymakesapps 1h ago
every fix starting with "first, change all your call sites" is exactly why nobody fixes this until it crashes in prod. 90ms on 431 files is basically free
•
u/V0RT3X_L33T_ 59m ago
Right, that's what pushed me to build this tbh, with AI it was pretty much just "I'll let Claude do it while I'm sleeping" and it did work lol
1
u/Neat_Area_4781 1h ago
The cross-module part is what caught my eye. How does it handle views that are only reachable through a closure or custom container, where the environment injection is conditional? That seems like the tricky edge: missing a path that can crash, or flagging one that's safe.