Skip to content

fix(deploy): set Email Routing routes to a Worker after the upload - #6

Merged
Refzlund merged 3 commits into
nextfrom
fix/email-routing-after-upload
Dec 1, 2026
Merged

Refzlund merged 3 commits into
nextfrom
fix/email-routing-after-upload

Conversation

@Refzlund

@Refzlund Refzlund commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Why

devflare deploy --prod of an app whose zones[domain].emailRouting routes mail to a Worker — for example catchAll: { worker: '<its own worker>' } — failed on its first deploy into an account where that Worker did not exist yet:

Cloudflare PUT /zones/<zone>/email/routing/rules/catch_all failed (2016): Workers Script Info not found

Zone provisioning ran during deploy preparation, before Wrangler uploaded the script, so a route to the app's own Worker could never be set on a first deploy.

What changes

  • Worker-targeted Email Routing runs after the upload. Every rule and catch-all whose action is worker is now set after the upload is proven, and after a percentage rollout's traffic split. Everything else stays in preparation, in its old order: Email Sending, enabling routing, destination addresses, forward and drop routes, DNS, event subscriptions.
  • It reconciles as before. Rules match by address, the catch-all by enabled state and actions. Nothing is deleted, rule priority still comes from the full declared list, and a second deploy is a no-op.
  • A failed upload skips the deferred routes. The failure message names the Worker routes still waiting.
  • A failure after a good upload exits 1. The message says the Worker was uploaded, names the route and its target Worker, and tells a failed read from a failed write. It suggests a re-run only where one could help.
  • Every exit after a good upload names the routes it did not set: a failed verification, a failed rollout, an unresolvable account. A reused live version under DEVFLARE_REQUIRE_FRESH_PRODUCTION_DEPLOYMENT sets the routes before refusing, so the refusal can no longer block a route for good.
  • Cloudflare error 2016 is retried, on writes only: three more attempts, 3 s apart. DEVFLARE_EMAIL_ROUTE_RETRY_DELAY_MS overrides the wait. How long Email Routing takes to see a new Worker is not measured, so the docs only describe the retry.
  • --dry-run lists the deferred routes as "after the upload" and still makes no write.
  • Preview deploys still do no zone work.

Checks

  • tests/unit/config/deploy-zones.test.ts, tests/unit/cli/deploy-after-upload.test.ts and tests/integration/cli/deploy-email-routing-after-upload.test.ts: 69 pass, 0 fail. tsgo --noEmit is clean.
  • Mutants, each restored afterwards; every one fails a test:
    • the step moved ahead of the upload-evidence check, or ahead of the rollout;
    • the step dropped from the reused-version branch;
    • the early exits no longer naming unset routes;
    • the retry removed, or applied to every error;
    • a catch-all read tagged as a write;
    • the other-Worker branch removed from the message.
  • An independent review returned no must-fix findings and five should-fix ones; all five are fixed in the second commit.

🤖 Generated with Claude Code

Refzlund and others added 2 commits December 1, 2026 02:05
A rule or catch-all whose action is `worker` was reconciled in deploy
preparation, before Wrangler uploaded the script. Cloudflare refuses such a
route while the Worker does not exist (`2016: Workers Script Info not
found`), so an app routing mail to its own Worker failed on its first deploy
into an account.

Preparation (`provisionZoneResources`) now skips every `worker` route and
hands back `provisionAfterUpload`, bound to the same account and API. The
deploy command runs it once the upload is proven (and, for a percentage
rollout, after the traffic split), so a failed upload never reaches it. A
failure there exits 1 with a message saying the Worker WAS uploaded, which
route failed and why, and what this run had already set. Everything else
(sending onboarding, enabling routing, destinations, forward/drop rules and
catch-alls, DNS) keeps its place and order in preparation.

Deferral covers every `worker` action, not only self-targeted ones: one
predicate, deterministic, and harmless for a Worker that already exists. A
version-only upload (percentage rollout) runs the step too, because the
script exists after it and preparation has already reconciled the zone's
other routes. Previews still have no zone work at all. `--dry-run` runs the
step against the stubbed API and lists its would-creates as "after the
upload". Rule priority stays the index in the whole declared list.

Rule and catch-all reads and writes now throw `EmailRouteError`, naming the
route and keeping Cloudflare's error as the cause.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review follow-up to 1c84635.

- Order: the step runs only after the upload is proven. New command-flow
  cases cover a Wrangler exit 0 with no upload evidence (no catch-all write)
  and a percentage rollout (split first, then the route; a failed split
  skips it).
- Messages: `EmailRouteError` now carries the operation (read/write) and the
  target Worker. A failed read says nothing about the route changed; a failed
  write names the Worker it hands mail to; a route to ANOTHER Worker says to
  deploy that one first instead of suggesting a re-run that cannot fix it.
- Early exits: a reused live version runs the step before a required-fresh
  deployment is refused (adding a worker rule never changes the upload, so
  that refusal used to block the route forever). The failed-upload,
  unproven-upload, unresolved-account, failed-verification and failed-rollout
  exits now name the Worker routes left unset (`describeWorkerEmailRoutes`).
- Propagation: a write answered with 2016 is retried three times, 3 s apart
  (`DEVFLARE_EMAIL_ROUTE_RETRY_DELAY_MS` overrides the delay; tests inject a
  sleep). Nothing else is retried. How long Email Routing actually takes to
  see a new Worker is not measured; the changeset and docs say only what the
  code does.
- The after-upload result field is now `afterUpload: { routes, provision }`.
- Dropped the "waiting costs nothing" claim: a route to a misspelled Worker
  now fails after the Worker ships.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fc5de95

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
devflare Patch

Not sure what this means? Click here to learn what changesets are.

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

…wn file, under the size ceiling

The after-upload deferral and the 2016 retry took deploy-zones.test.ts from 950 to 1281 lines, past
the 1000-line ceiling source-file-size.test.ts enforces. The two describe blocks for the routes set
after the upload move to deploy-zones-after-upload.test.ts, and the mock zone API and the two pass
runners both files use move to tests/helpers/zone-provision-api.ts. 54 tests before, 54 after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions
github-actions Bot had a problem deploying to testing branch preview / fix/email-routing-after-upload December 1, 2026 01:36 Failure
@Refzlund
Refzlund merged commit 4d09453 into next Dec 1, 2026
7 of 8 checks passed

This branch had an error being deployed

1 failed and 1 active deployments
documentation branch preview / fix/email-routing-after-upload — fc5de950 Deployed Dec 1, 2026 by github-actions[bot]
testing branch preview / fix/email-routing-after-upload — fc5de950 Deployed Dec 1, 2026 by github-actions[bot]
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