r/iOSProgramming • • 3d 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.

2 Upvotes

11 comments sorted by

1

u/Neat_Area_4781 3d 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.

1

u/V0RT3X_L33T_ 3d ago

It reads each file on its own first: which views it declares, which `@Environment` values each one needs, which views it shows, where .environment(...) is called, and what the file imports. Then it connects everything across files and modules. If two modules have a view with the same name, the file's imports decide which one is meant, and if they can't, it checks against both. After that, modules don't matter: if a view in a package needs a Cart, that need follows it up through every app view that shows it, until something provides a Cart or it reaches the App or a preview.

For conditional injection, each branch counts on its own, so if flag { Child().environment(cart) } else { Child() } gets flagged. The one miss is a value provided under #if: it counts as provided in every build. That's the one crashing case it misses today, and it's first on the roadmap.

1

u/Neat_Area_4781 3d ago

That makes sense—checking each branch separately seems like the right way to catch the conditional case. And thanks for calling out the #if miss plainly; knowing the exact boundary helps.

1

u/jaymakesapps 3d 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

1

u/V0RT3X_L33T_ 3d 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/dawedev 3d ago

Curious how it treats views hosted from UIKit through UIHostingController, where the environment gets injected on the host's rootView instead of a parent view. Does that count as a valid injection path or does it flag them?

1

u/V0RT3X_L33T_ 3d ago

Yes it does count as a valid injection, I have test cases covering all of this

1

u/dawedev 3d ago

Nice, that was the case I'd expect to trip up a purely syntax-based pass, so good to hear it's covered by tests.

1

u/DimensionMindless336 20h ago

The "fail the build" part is the tricky bit, and it's not the analysis that gets you -- it's making the plugin reliable enough that people don't mute it.

Two things that bit me shipping a SwiftSyntax-based build plugin:

  1. These plugins run sandboxed by default. On a locked-down CI runner, or inside an agent's sandbox, the plugin process can't write its trust/cache dirs and it fails in a misleading way -- you get downstream "not available" errors or type-check timeouts instead of a clean "plugin crashed". Building with OTHER_SWIFT_FLAGS='$(inherited) -disable-sandbox' (don't drop the $(inherited)) cleared it up for me. Worth a troubleshooting note before someone concludes the analyzer itself is flaky.
  2. Keep "plugin didn't run" separate from "found a real violation." If an infra hiccup turns into a red build, the team adds an exception within a week and the tool is effectively dead. Make the infra failure a warning; only actual missing-injection detections should fail the build.

One more for the README: concurrent agents or worktrees sharing one DerivedData will stomp the clang module cache, and the resulting nonsense errors look exactly like your analyzer's fault. One DerivedData per worktree. 90ms on 431 files is genuinely impressive -- just don't let a CI hiccup make people blame the tool.

1

u/[deleted] 3d ago

[removed] — view removed comment

1

u/AutoModerator 3d ago

Hey /u/FahadAppMaster, your content has been removed because Reddit has marked your account as having a low Contributor Quality Score. This may result from, but is not limited to, activities such as spamming the same links across multiple subreddits, submitting posts or comments that receive a high number of downvotes, a lack of recent account activity, or having an unverified account.

Please be assured that this action is not a reflection of your participation in our subreddit. This is simply an automated filter in place to reduce spam.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.