Skip to content

fix(cloudflare): ignore unchanged env watch events#631

Closed
shawnsw wants to merge 1 commit intodmno-dev:mainfrom
shawnsw:fix/varlock-wrangler-unchanged-env-restarts
Closed

fix(cloudflare): ignore unchanged env watch events#631
shawnsw wants to merge 1 commit intodmno-dev:mainfrom
shawnsw:fix/varlock-wrangler-unchanged-env-restarts

Conversation

@shawnsw
Copy link
Copy Markdown

@shawnsw shawnsw commented Apr 19, 2026

Summary

  • ignore fs.watch() events in varlock-wrangler dev when the resolved env graph is unchanged
  • prevent restart loops caused by spurious env file watch events on macOS
  • keep Wrangler restarts limited to real env changes

Repro

On macOS, varlock-wrangler dev can receive repeated fs.watch() events for env source files even when their contents have not changed.

In our case this showed up under portless as repeated events for apps/api/.env.schema, causing varlock-wrangler to restart Wrangler in a loop.

Root cause

varlock-wrangler dev currently restarts Wrangler on every watched env file event after re-resolving env.

That assumes every watch event represents a semantic env change, which is not always true on macOS.

Fix

Cache the last resolved serialized env graph and compare it after each watch event.

Only restart Wrangler when the newly resolved env graph differs from the previous one.

Why this is safe

This does not change how env is resolved.

It only skips restarts when the resolved env output is identical, so real env changes still trigger the existing restart path.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 19, 2026

⚠️ No Changeset found

Latest commit: a43d97c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@theoephraim
Copy link
Copy Markdown
Member

Thanks!

@theoephraim
Copy link
Copy Markdown
Member

@shawnsw - I am having a hard time recreating the issue locally. Can you give repro steps?

@shawnsw
Copy link
Copy Markdown
Author

shawnsw commented Apr 20, 2026

@theoephraim On macOS, fs.watch() can emit repeated events for watched env files even when the resolved env graph is unchanged. If varlock-wrangler dev restarts on every watch event, it can loop.

That also explains why you may not reproduce it locally: if your machine does not emit those extra events, the loop does not happen.

The fix is harmless: re-resolve env on each watch event, compare the serialized resolved graph, and only restart when it actually changed. Real env changes still restart Wrangler.

Repro:

  1. Run varlock-wrangler dev on macOS.
  2. Watch env inputs like .env.schema / .env.local.
  3. Trigger repeated watch events without a semantic env change. (see script below)
  4. Observe repeated restarts
for i in {1..5}; do
  python3 - <<'PY'
from pathlib import Path
p = Path(".env.schema")
data = p.read_text()
p.write_text(data)
PY
  sleep 1
done

Copilot AI added a commit that referenced this pull request Apr 21, 2026
…unchanged

Builds on PR #631 by @shawnsw which added the debounce + cache equality
check. Adds a 5-second idle threshold: if no restart has happened in the
last 5 seconds, a save event will trigger a restart even when the resolved
env graph is identical. This handles the case where a user saves a file
again after a long idle period and expects the dev server to reload.

Co-authored-by: Shawn <shawn@docnow.au>"

Agent-Logs-Url: https://github.com/dmno-dev/varlock/sessions/785c3bab-96c9-480b-bcab-ec417ac2c52e

Co-authored-by: theoephraim <1158956+theoephraim@users.noreply.github.com>
theoephraim added a commit that referenced this pull request Apr 21, 2026
…anged (#635)

* fix(cloudflare): debounce wrangler restarts and skip when env is unchanged

Agent-Logs-Url: https://github.com/dmno-dev/varlock/sessions/ef8b4df3-0fb0-4ccb-b2e2-c90dac16c7d6

Co-authored-by: theoephraim <1158956+theoephraim@users.noreply.github.com>

* fix(cloudflare): force restart after idle threshold even when env is unchanged

Builds on PR #631 by @shawnsw which added the debounce + cache equality
check. Adds a 5-second idle threshold: if no restart has happened in the
last 5 seconds, a save event will trigger a restart even when the resolved
env graph is identical. This handles the case where a user saves a file
again after a long idle period and expects the dev server to reload.

Co-authored-by: Shawn <shawn@docnow.au>"

Agent-Logs-Url: https://github.com/dmno-dev/varlock/sessions/785c3bab-96c9-480b-bcab-ec417ac2c52e

Co-authored-by: theoephraim <1158956+theoephraim@users.noreply.github.com>

* fix(cloudflare): fix restart loop detection and idle threshold init

Initialize lastRestartAt to Date.now() so the idle threshold guard
works correctly on first file change. Move restartTimeout = undefined
to only the skip/error paths so the exit handler can distinguish
restart-kills from voluntary exits.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: theoephraim <1158956+theoephraim@users.noreply.github.com>
Co-authored-by: Theo Ephraim <theo@dmno.dev>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@theoephraim
Copy link
Copy Markdown
Member

Rolled this into #635 - should be out shortly.

Thanks!!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants