Watch SDK
The Watch SDK provides file watching and automatic rebuild functionality. Enable hot-reload development workflows where source file changes automatically trigger rebuilds and process restarts — no manual intervention needed.
Quick start
Watch and rebuild
Add the --watch flag to any build command:
Watch and run
Combine --watch with rbs run to automatically restart your application on changes:
When you save a file, the watcher detects the change, stops the running process, rebuilds, and restarts:
CLI flags
| Flag | Description | Default |
|---|
--watch | Enable watch mode. | false |
--debounce <ms> | Milliseconds to wait after last change before rebuilding. | 300 |
--clear | Clear the screen on each rebuild. | false |
--no-clear | Don’t clear the screen on rebuild. | — |
--extensions <ext> | Override watched file extensions (comma-separated). | Auto-detected |
Configuring watch behavior in rules
Rules can specify their own watch behavior using the ctx.watch API. This lets rule authors define sensible defaults for each language or framework.
ctx.watch.config()
Set watch configuration from inside a rule implementation:
Configuration options
| Option | Type | Default | Description |
|---|
extensions | list[string] | [] (all files) | File extensions to watch (e.g., .py, .ts). |
ignore_patterns | list[string] | [".rbs/", ".git/"] | Glob patterns to ignore. |
extra_watch_dirs | list[string] | [] | Additional directories to watch beyond the target’s sources. |
debounce_ms | int | 300 | Wait time in ms after the last change before triggering a rebuild. |
clear_screen | bool | false | Clear terminal screen before each rebuild. |
signal_reload | bool | false | Send SIGHUP instead of restarting the process (for servers that support hot reload). |
ctx.watch.on_reload()
For servers that support hot reload without a full restart, define a custom reload command:
Language examples
Python
Watch behavior:
- Extensions:
.py
- Ignores:
__pycache__/, *.pyc
- Debounce: 300ms
Node.js / TypeScript
Watch behavior:
- Extensions:
.js, .ts, .jsx, .tsx, .css, .html
- Ignores:
node_modules/, dist/
- Debounce: 200ms
Java / Kotlin (Spring Boot)
Watch behavior:
- Extensions:
.java, .kt, .properties, .yaml
- Ignores:
target/, build/
- Debounce: 500ms
Custom watch configuration
How debouncing works
The watcher uses debouncing to batch rapid file changes into a single rebuild. When multiple files are saved in quick succession (e.g., from an IDE “Save All”), only one rebuild is triggered:
Recommended debounce settings:
| Scenario | Recommended debounce |
|---|
| Fast compile languages (Python, Node.js) | 200–300ms |
| Medium compile (Java, Kotlin) | 300–500ms |
| Slow compile (C/C++, Rust) | 500–1000ms |
| IDE auto-save enabled | 500ms |
Process management
Graceful shutdown
When files change and a process is running, the watcher:
- Sends
SIGTERM to the process group.
- Waits up to 3 seconds for graceful shutdown.
- Sends
SIGKILL if the process is still running.
- Runs the rebuild.
- Starts the new process.
Make sure your application handles SIGTERM gracefully — close database connections, flush buffers, and shut down cleanly.
Signal reload (hot reload)
For servers that support hot reload via SIGHUP, enable signal mode to avoid full restarts:
The watcher sends SIGHUP instead of killing and restarting the process. The server can reload configuration or recompile templates without downtime.
Troubleshooting
Infinite rebuild loop
Symptom: Build triggers, outputs trigger another build, repeat.
Solution: Add output directories to ignore patterns:
Too many open files
Symptom: Error about file descriptor limit.
Solution: Reduce the watched directories or increase the file descriptor limit:
Process not stopping
Symptom: Old process keeps running after rebuild.
Solution: Ensure your application handles SIGTERM and exits cleanly. If your process spawns child processes, make sure they are in the same process group.