watchexec/cargo-watch

Support for selecting workspace package

Gricha opened this issue · 2 comments

Hey! I have a workspace that contains ~5 different services, I run all of them on cargo watch. Up until 8.2.1 each command had specified directories to watch explicitly and if dependencies changed I tried to keep it relatively up to date manually.

With new release and support for local dependencies detection, I was hoping I could move away from this.
One caveat I have is that my services currently rely on being started from the root of workspace. I used to do this:
(cd .. && cargo watch -w path/to/specific/dep --why -- cargo run -p my-service)

In the new version I'm in kind of a pickle since cargo watch relies on working directory being of a package I want to watch, but cargo run that follows must run in the root (or generally binary needs to be started with CWD being root of workspace).
In a sense this is what I'd like to achieve.
# ran from specific package
cargo watch --why -- (cd .. && cargo run -p foo)
or
# ran from root
cargo watch -p foo --why -- cargo run -p foo
To my understanding of docs, the second one is not supported. The first one seems not to work, I couldn't figure out proper syntax that would let subshell slide.

My proposal for long term is to support specifying observed package via parameter. I am looking for advices for short term though. I'm attaching a minimal project that makes it easier to test with. The project has binary foo, and libraries a, b, c. Foo depends on a, a depends on b, c is unused. Touching particular files in the libraries helps test watch invocations of the workspace dependent on how the command was started.
temp-watch.zip

For anything but the most basic shell shenanigans you'll want to quote the entire thing, otherwise your shell parses and runs subshells etc before even passing to cargo watch.

You're right -p isn't supported at the moment, though as the flags are already reserved, they don't throw "unknown option!" errors, a bit unfortunate. Your current workaround with manual -w should keep working for the root case. -p support is tracked in #52 though of course a lot has changed since that was first filed.

Ahh.. Great hint @passcod , this lead me to running this command:
cargo watch --why -- sh -c "(cd .. && cargo run -p foo)" which works perfectly. Thank you!