CARGO-WATCH(1)
๐ NAME ๐
Okay so bestie, welcome to the CARGO-WATCH(1) manual page ๐. The name is cargo-watch, and it watches over your Cargo project's source. It's giving โyour personal build-watch gremlinโ energy, and honestly? Iconic. It ate and left no crumbs. โจ
๐ฆ SYNOPSIS ๐ฆ
Here's the invocation vibe, copied exactly so we don't mess with the syntax:
cargo watch [options] [--exec | -x cargo subcommand]... [--shell | -s
command]... [-- command...] cargo watch [--version | -V] cargo watch
[--help | -h]
โจ DESCRIPTION โจ
Okay so lowkey the whole point is: it watches over your project's source for changes, and runs Cargo commands when they occur. Highkey a vibe. No cap: it's your โsomething changed, do the thingโ bestie. ๐ฅ
๐ง COMMANDS ๐ง
Commands are main character energy here, so let's meet them.
๐
cargo subcommand
Cargo subcommand to run when watched files are modified, and at startup, unless --postpone is specified.
This is essentially the same as <command> but with cargo prepended, except that in addition, cargo-specific options are applied, such as --features.
๐ซถ command
Command to run when watched files are modified, and at startup, unless --postpone is specified.
All commands passed are joined together with &&.
Cargo commands (-x) are always executed before shell commands (-s).
If a trailing command is given with -- <command>, it supersedes all other commands.
๐ OPTIONS ๐
Okay bestie, here's the whole flag menu โ no skips, periodt. iykyk. Not me being obsessed with a flag list, but here we are. ๐
-h,--helpโ Display this message.-V,--versionโ Display version information.--debugโ Show debug output.--whyโ Show paths that changed.-q,--quietโ Suppress output fromcargo-watchitself.--skip-local-depsโ Don't try to find local dependencies of the current crate and watch their working directories. Only watch the current directory.-w,--watchwatch... โ Watch specific file(s) or folder(s). Disables finding and watching local dependencies. By default, the workspace directories of your project and all local dependencies are watched, except for thetarget/and.git/folders. Your.ignoreand.gitignorefiles are used to filter paths.-i,--ignorepattern... โ Ignore a glob/gitignore-style pattern. On Windows, patterns given to-ihave forward slashes (/) automatically converted to backward ones (\) to ease command portability.--no-vcs-ignoresโ Don't use.gitignorefiles.--no-dot-ignoresโ Don't use.ignorefiles.--ignore-nothingโ Ignore nothing, not eventarget/and.git/.-c,--clearโ Clear the screen before each run.-E,--env-varkey=value โ Inject an environment variable.--env-filepath โ Inject environment variables from a dotenv-style file.-Bvalue โ InjectRUST_BACKTRACE=<value>into the environment. This is useful when panics are handled and you want a backtrace to be printed: usually you'd have to addRUST_BACKTRACE=1yourself, this shortens it to-B1!-Lvalue โ InjectRUST_LOG=<value>into the environment.--experimental--env-changesโ Unstable option which will setWATCHEXEC_*environment variables on the command to gather information on the files changes cargo watch saw. Do not rely on this, may disappear or change without notice. Note that the files listed are not guaranteed to be the full list of files changed, nor indeed to even have been changed at all, and that this may vary between platforms. In general, finding the list of files that changed is best done by other tools such asfind, to list out all the files with a modification date greater than the last known execution.-C,--workdirpath โ Change the working directory to the path given. The default is the crate root, as found bycargo locate-project.--postponeโ Postpone first run until a file changes.--pollโ Force use of polling for file changes.--no-process-groupโ By default, Cargo Watch uses a process group to run the command(s). This disables that behaviour.-d,--delaydelay โ File updates debounce delay in seconds (default: 0.5).--no-restartโ Don't restart command while it's still running.--watch-when-idleโ Ignore events emitted while the commands run.--featuresfeatures โ List of features passed to cargo invocations. The features will be passed to every supported cargo subcommand. For example, with this command:$ cargo watch --features foo,bar -x clean -x build -x docBothbuildanddocwill be run with thefooandbarfeatures, butcleanwon't.--use-shelluse-shell โ Use a different shell. E.g.--use-shell=bash. On Windows, try--use-shell=powershell. The special valuenonewill use no shell, but will only work with a trailing command, not-xor-s.
๐ EXAMPLES ๐
Okay, here's the vibe check: run cargo check on file save:
$ cargo watch
Run tests only:
$ cargo watch -x test
Run check then tests:
$ cargo watch -x check -x test
Run run with arguments:
$ cargo watch -x ยดrun -- --some-argยด
Run an arbitrary command:
$ cargo watch -- echo Hello world
Run with features passed to cargo:
$ cargo watch --features ยดfoo,barยด
๐ธ DETAILS ๐ธ
๐ฆ Ignore files ๐ฆ
This is where the ignore lore lives.
.gitignore files are used by default to ignore paths to watch and trigger runs. To stop honouring them, pass --no-vcs-ignores.
.ignore files in the same syntax are also used by default. This file can be used to specify files that should be ignored by cargo watch but checked into git, without constantly adding --ignore abc options on the command-line. Do note that .ignore files may also be used by other programs, like ripgrep(1). To stop honouring these, pass --no-dot-ignores.
Cargo watch also has an internal list of default ignores on top of those specified in files, like target/ and .git/ and various other common types (logs, editor swap files, lockfiles, etc).
To skip literally all ignores, use the --ignore-nothing flag.
๐ง Ignore syntax ๐ง
See the glob::Pattern docs0 https://doc.rust-lang.org/glob/glob/struct.Pattern.html for a more detailed specification of the glob matching syntax used for --ignore.
On Windows, patterns should be specified with Windows-style (\\) separators. Unix-style separators (/) would not match Windows paths, which could be confusing and give the appearance of commandline ignores not working. For convenience / in commandline ignores are automatically translated to \\ when running on Windows, but one should still try to write the correct patterns for the platform, as there may be more subtle differences.
๐ BUGS ๐
Bugs happen, bestie โ fr fr.
Please open an issue1 https://github.com/watchexec/cargo-watch/issues, or look through the existing ones.
If you want more verbose output, try running with the --debug flag. Note that this will also enable debug mode for watchexec. When filing an issue, make sure to include a log with --debug enabled so problems can be diagnosed, as well as your --version and OS.
If your issue is a watchexec (our main upstream) issue, open it there2 https://github.com/watchexec/watchexec/issues directly. If you're not sure, feel free to open it on the cargo-watch issue tracker, but if it is a watchexec issue, it will be transferred over anyway.
๐ KNOWN BUGS ๐
In 7.8.0, the --workdir option changes the directory before any other options are processed, so e.g. --watch paths may not work as expected. This is fixed upstream in Watchexec library 2.
In 8.x, the -B, -L, -E, --env-var, --env-file options inject environment variables into the main cargo-watch process, which may be unsound in some situations and may also affect cargo-watch itself. This is fixed upstream in Watchexec library 2.
๐ง TROUBLESHOOTING ๐ง
If something's acting up, start here.
Always start by checking your version with cargo watch --version and, if necessary, upgrading to the latest one by checking the website linked above.
๐ญ RLS is slow while using cargo watch, or vice versa, or itยดs waiting for the project lock a lot ๐ญ
Cargo builds (and checks, and clippy, and tests because the tests have to be built) take out a lock on the project so two cargo instances don't run at the same time.
However, Rust Analyzer is much better at this, so use that instead of RLS.
๐ File updates seems to never trigger ๐
Try using --poll to force the polling fallback.
If that still doesn't work, and you're using an editor that does "safe saving", like IntelliJ / PyCharm, you may have to disable "safe saving" as that may prevent file notifications from being generated properly.
Also try using the --why option to see if the paths you expect are changing.
๐ It runs repeatedly without touching anything ๐
That can happen when watching files that are modified by the command you're running.
If you're only running compiles or checks (i.e. any command that only affects the target/ folder) and you're using -w, you might be confusing the target-folder-ignorer. Check your options and paths.
You can also use the --watch-when-idle flag to ignore any event that happens while the command is running. This will become the default in 8.0.
๐ธ It runs repeatedly only touching ignored files ๐ธ
Make sure the files you ignored are the only ones being touched. Use the --why option to see exactly which files were modified and triggered the restart. Some programs and libraries create temporary files that may not match a simple ignore pattern.
As above, you can also use the --watch-when-idle flag to help.
๐ I donยดt have colour in my cargo output / for cargo test ๐
This sometimes happens on some terminal configurations or for test harnesses. A quick workaround (instead of going down the rabbit hole of debugging your console settings) is to pass --color=always to the command. E.g.
$ cargo watch -x ยดcheck --color=alwaysยด
For test (and bench) commands, you'll need to pass the flag to the underlying program instead of cargo:
$ cargo watch -x ยดtest -- --color=alwaysยด
๐ AUTHOR ๐
Written and maintained by Fรฉlix Saparelli.
Project homepage:
https://watchexec.github.io/#cargo-watch
Public domain.
๐ Sign-off: that's the CARGO-WATCH(1) manual page, January 2024, and it's public domain. Bye bestie, go slay those builds! ๐
๐ฆ where this page comes from
- alpine 3.24: cargo-watch-doc 8.5.3-r0 ยท CC0-1.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms