Skip to content

Give app entrypoints the platform's bindings on every environment - #253

Merged
tnkuehne merged 2 commits into
mainfrom
fix/entrypoint-env-bindings
Nov 20, 2026
Merged

tnkuehne merged 2 commits into
mainfrom
fix/entrypoint-env-bindings

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Hosted apps that read bindings through this.env or a handler's env lose the platform's facades on the HTTP path. The GitHub template's browser routes (/, /connect/start) fail with Cannot read properties of undefined (reading 'batch'): initialize(this.env.DB) gets undefined. The same app's UserConnection RPC calls read the database without problems, and apps that use import { env } from "cloudflare:workers" are unaffected.

The runtime installs the D1/R2/KV facades, WIDEFLEET_OAUTH and app connections on the importable env when the app loads. In workerd, that export is a proxy for the Worker's base environment (getWorkerEnv()). Locally every entrypoint receives the same object, so all tests passed. In production, Cloudflare's Worker Loader hands the default HTTP entrypoint a different environment object that doesn't have them.

Change

  • withEnvProperties passes apps an environment that carries every property of the importable env.
  • It applies to class entrypoints (ScopedEntrypoint), object handlers, the cron and queue runners, and both workflow runners.
  • An environment that cannot be extended gets a copy instead.
  • The importable env is read by keys and values only. Reading descriptors through its proxy throws for non-configurable bindings such as WIDEFLEET_OAUTH; the managed OAuth runtime test caught that.

Tests

  • env-properties.test.ts covers merging, frozen environments and a workerd-style proxy with a non-configurable binding.
  • Ran pnpm check, all unit tests, and the dynamic, workflow, capture, services, managed OAuth and Cloudflare runtime suites.
  • Locally, the Cloudflare control plane noindex test fails on main as well. deployment.test.ts needs a Docker network, which this machine could not create.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the previous binding-name issue is fixed.

Summary

App entrypoints now receive missing platform bindings from the importable env.

  • App entrypoints now get platform bindings across requests, events, and workflows.

Reviews (2) · Last reviewed commit: "Keep a binding named __proto__ when merg..." · Reviewed by Greptile

The runtime installs D1, R2 and KV facades, managed OAuth and app
connections on the importable env when an app loads. That env is a proxy
for the Worker's base environment. In production, Cloudflare's Worker
Loader hands the default HTTP entrypoint another environment object, so
apps reading `this.env` or a handler's `env` found `DB` undefined there,
while named RPC entrypoints and the importable env worked.

Entrypoints, object handlers, cron, queue and workflow runners now pass
apps an environment that carries every property of the importable env.
An environment that cannot be extended is copied. The importable env is
read only by keys and values, since reading descriptors through its
proxy breaks invariants for non-configurable bindings.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 9c25bee Nov 20 2026, 08:19 PM

Comment thread packages/app-runtime/src/env-merge.ts Resolved
@tnkuehne
tnkuehne merged commit 8b8a4b0 into main Nov 20, 2026
16 checks passed
@tnkuehne
tnkuehne deleted the fix/entrypoint-env-bindings branch November 20, 2026 20:27
tnkuehne added a commit that referenced this pull request Nov 20, 2026
## Problem

Connector pages still fail with `Cannot read properties of undefined
(reading 'batch')` on 0.7.20. #253 did not fix it.

The runtime defines the D1/R2/KV/Workflow facades, `WIDEFLEET_OAUTH` and
app connections once, on the importable `env`, while the app's modules
load. A diagnostic app in production logged what happens next. When the
same isolate serves a later request, such as `/connect/start` after a My
connections status check:

| | at module load | `this.env` in the request | importable `env` in the
request |
|---|---|---|---|
| `DB`, `WIDEFLEET_OAUTH` | present | missing | missing |
| `WIDEFLEET_D1_DB` and other raw bindings | present | present | present
|

Cloudflare hands that request a new environment object. The importable
`env` is a proxy for the current environment, so it follows the new
object. Everything defined at module load is gone. Requests in a fresh
isolate work, which is why single test calls succeeded.

#253 copied the facades from the importable `env`. It lacked them too,
so the copy changed nothing.

## Change

- `bindingRegistry` keeps the facades. They resolve the raw bindings
from the current environment when called, so the same objects serve
every request.
- `withBindings` adds missing properties to the environment an
invocation hands the app, and to the importable `env`. Class
entrypoints, object handlers, cron, queue and both workflow runners use
it.
- Child bundles that are compiled separately import `withBindings` from
`./__widefleet_bindings.js` through the `widefleet:bindings` alias.
- Module load still installs the facades, for apps that read bindings at
the top level.
- Every property is configurable, so reading it through the importable
env's proxy no longer breaks proxy invariants.
- Removes `env-merge.ts` and `env-properties.ts` from #253.

## Tests

- `binding-registry.test.ts` reproduces the production sequence with a
workerd-style proxy: install at load, switch to a new environment
object, then install again on the next request.
- Ran `pnpm check`, unit, dynamic, capture, services and Cloudflare
runtime suites.
- Locally, the control plane `noindex` test fails on `main` as well.


<!-- greptile_comment -->

<!-- greptile_summary -->

<h2><a
href="https://app.greptile.com/api/retrigger?id=114626572&amp;sig=2db3c64dab16b102ede7edb841ce761c7a5bf5b9321ad276bcfe09166dd32c9c"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img
alt="Retrigger"
src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"
align="right"></picture></a><a
href="https://app.greptile.com/-/switch-tenant?tenant=mendra&amp;next=%2Fwidefleet%2Fwidefleet%2Fpull%2F254&amp;utm_source=greptile_review&amp;utm_medium=github&amp;utm_campaign=view_in_greptile"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img
alt="View in Greptile"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"
align="right"></picture></a>Confidence Score: 5/5 <sup><img alt="Tier:
plus" title="Review tier: plus"
src="https://greptile-static-assets.s3.amazonaws.com/badges/effort-plus.svg?v=3"
height="12"></sup></h2>

<!-- greptile-impact-verdict --> The PR appears safe to merge; no
actionable issues were found.

<details open><summary><h3>Summary</h3></summary>

The PR keeps platform bindings in a shared registry and restores missing
bindings before app handlers run.

- Every app request gets its platform bindings.

No actionable issues were found. No tests were run during this review.
</details>

<details open><summary><h3>Diagram</h3></summary>

```mermaid
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["Define platform bindings"] --> B["Shared binding registry"]
  B --> C["Install during module load"]
  D["App invocation"] --> E["withBindings"]
  B --> E
  E --> F["Complete importable env"]
  E --> G["Complete invocation env"]
  G --> H["Call app code"]
  I["Event and workflow modules"] -->|"widefleet:bindings"| E
```
</details>

<!-- greptile_confidence_score:5 -->

<sub>Reviews (1) · Last reviewed commit: ["Add platform bindings to the
environment..."](b450d8a)
· [Reviewed by
Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub>

<!-- /greptile_comment -->
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