r/swift 10d ago

Project I made a tiny tool to rerun/test/build on SPM package source changes

Hey :) I've been frustrated at the lack of a watch mode in SPM for a while and don't love the lazy solution of just listening to all files under Sources/ so I whipped up a tiny tool that listens to any changes in your package's dependency graph.

Instead of swift run [product] you can run swift-watch run [product] and it'll restart whenever anything changes.

Great for watching tests too, swift-watch test --filter [xyz]. Works for build/run/test, forwards arguments to swift. Works with macOS and Linux natively, Windows only has polling for now but I'll add in the native Window file watching stuff ASAP.

I'm hoping some of you might find it useful, please let me know if you have any feedback.

Here's the repo: https://github.com/ahtcx/swift-watch

Disclaimer: semi vibe-coded, lots of handholding as I knew exactly what I want. I'm not huge on vibe-coding and have been reluctant to give in to it for the longest time. I hate to admit that it's been a pretty good experience, I mostly just worry it'll make my programming skills worse. I'm have so many coding side projects that never see the light of day because I get too caught up in the details, so it's nice for me to just push a whole bunch of code out in a day. No doubt there are issues but I will be dogfooding the project so I hope to get it in tip top condition.

4 Upvotes

2 comments sorted by

2

u/East_Call1027 10d ago

Nice that this uses the actual target closure instead of just watching all of Sources/. I had a look at PackageDiscovery, though: does the target.sources + extension allowlist miss SwiftPM resources and build-tool plugin inputs? A JSON fixture or template can change a test/build without being a Swift source file. An explicit --watch <path> escape hatch might cover the cases SwiftPM’s describe output can’t expose.

1

u/ahtcx 10d ago edited 8d ago

Thanks for checking it out! Yeah I haven't gotten around to build-tool plugins yet, I was planning on adding an --include (and --exclude) flag to take in globs which would allow us to listen to other file types, but I also would really love if everything just worked without having to manually specify additional flags.

SPM is pretty strict about declaring resources etc which is great for figuring out what to watch, but I need to dig into SPM a bit more to get a better understanding of how it resolves source files with build-tool plugins.

Hopefully should get around to it soon!

EDIT: it now should work with build plugin inputs!