workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: `unstable_getMiniflareWorkerOptions` breaks Miniflare when using Workers AI

Open frandiox opened this issue 1 year ago • 3 comments

Which Cloudflare product(s) does this pertain to?

Wrangler

What version(s) of the tool(s) are you using?

3.78.7

What version of Node are you using?

22.9.0

What operating system and version are you using?

Mac Sonoma

Describe the Bug

Observed behavior

When adding an AI binding in wrangler.toml, unstable_getMiniflareWorkerOptions returns the correct wrappedBinding but without the associated external worker. This is because unstable_getMiniflareWorkerOptions is not using the externalWorkers returned by buildMiniflareBindingOptions here so it's not possible to pass it to Miniflare later.

If the result is passed to Miniflare, it fails with the following:

workerd/server/workerd-api.c++:770: error: wrapped binding module can't be resolved (internal modules only); moduleName = miniflare-internal:wrapped:__WRANGLER_EXTERNAL_AI_WORKER

Expected behavior

There should be a way to get the wrangler config in a way that works when passed to Miniflare when using Workers AI. Perhaps it could return externalWorkers or something similar.

Steps to reproduce

  1. Add Workers AI to wrangler.toml:
[ai]
binding = "AI"
  1. Try reading the wrangler.toml with unstable_getMiniflareWorkerOptions and passing the result to Miniflare, or just log it. It will contain wrappedBindings { AI: { scriptName: '__WRANGLER_EXTERNAL_AI_WORKER' } } without the associated __WRANGLER_EXTERNAL_AI_WORKER service binding anywhere.

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

frandiox avatar Sep 23 '24 09:09 frandiox

cc @jacob-ebey perhaps interesting for your new Vite plugin

frandiox avatar Sep 23 '24 09:09 frandiox

Hey! I think unstable_getMiniflareWorkerOptions exists mostly to get the vitest integration working - is this issue because the ai binding isn't working with vitest, or are you using it directly in some way? If the latter, it would be helpful to get more info on what your usecase is :)

emily-shen avatar Oct 14 '24 11:10 emily-shen

Hi 👋
The use case is creating a Miniflare instance in a custom integration (Vite plugin) for local development, but reading the config options from wrangler.toml directly. Something like this.

When using the result of unstable_getMiniflareWorkerOptions to create the Miniflare instance, it is missing the worker associated to the wrapped binding for AI, which makes Miniflare/workerd complain.

I guess this would also affect the Vitest integration if you use AI there? Assuming that integration creates a Miniflare instance.

frandiox avatar Oct 15 '24 02:10 frandiox

this should work now as the external workers are also exported in unstable_getMiniflareWorkerOptions

emily-shen avatar Apr 01 '25 10:04 emily-shen