Skip to content

cli: add --watch-cleanup to remove output file on exit - #2788

Open
25hash wants to merge 1 commit into
d2lang:masterfrom
25hash:master
Open

cli: add --watch-cleanup to remove output file on exit#2788
25hash wants to merge 1 commit into
d2lang:masterfrom
25hash:master

Conversation

@25hash

@25hash 25hash commented Jul 30, 2026

Copy link
Copy Markdown

Summary

This PR adds a --watch-cleanup flag for d2 --watch.

When the flag is enabled, exiting watch mode with Ctrl-C removes the generated output file. Without the flag, watch mode keeps the current behavior and leaves the output in place.

Example:

d2 --watch --watch-cleanup input.d2 out.svg

Motivation

When using d2 --watch, the generated .svg is often just a temporary preview artifact. Today it remains on disk after watch mode exits, which can leave behind files the user did not intend to keep.

This change makes cleanup explicit and opt-in:

  • d2 --watch ... keeps the output
  • d2 --watch --watch-cleanup ... removes the output on exit

Changes

  • add --watch-cleanup CLI flag
  • wire the flag into watch-mode shutdown
  • print a clearer terminal message in watch mode:
    • press Ctrl-C to quit
    • press Ctrl-C to quit and remove ... when cleanup is enabled
  • add an e2e test covering Ctrl-C + cleanup behavior
  • update the README with a short usage note

Behavior

Default behavior

d2 --watch input.d2 out.svg
  • watch mode exits on Ctrl-C
  • out.svg remains on disk

Cleanup behavior

d2 --watch --watch-cleanup input.d2 out.svg
  • watch mode exits on Ctrl-C
  • out.svg is removed on exit

Notes

This is intentionally opt-in so existing workflows do not change.

Testing

Ran:

go test ./e2etests-cli -run TestCLI_E2E/watch-cleanup -count=1
go test ./e2etests-cli -run 'TestCLI_E2E/watch-' -count=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant