cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.75k stars 733 forks source link

🐛 BUG: Creating KV with --preview flag can't be used #3621

Open dinkopehar opened 1 year ago

dinkopehar commented 1 year ago

Which Cloudflare product(s) does this pertain to?

KV

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

3.1.1 Wrangler ,

What version of Node are you using?

18.16.1

What operating system are you using?

WSL Ubuntu

Describe the Bug

Hello.

I'm using SvelteKit to bootstrap my app with Cloudflare Pages. I opened issue regarding why platform is not available . I wasn't sure how to test locally with bindings, such as KV, R2 etc. Since SvelteKit generated _worker.js, I used wrangler dev path/to/_worker.js to run it.

In my wrangler.toml, I have set:

name = "someapp"
main = ".svelte-kit/cloudflare/_worker.js"
compatibility_date = "2023-07-10"

kv_namespaces = [
    { binding = "HELLO", id = "5933xxxxx..." }
]

When I try to run it like this (KV "HELLO" exists), it states:

wrangler 3.1.1 (update available 3.2.0)
-----------------------------------------------------
wrangler dev now uses local mode by default, powered by 🔥 Miniflare and 👷 workerd.
To run an edge preview session for your Worker, use wrangler dev --remote

✘ [ERROR] In development, you should use a separate kv namespace than the one you'd use in production. Please create a new kv namespace with "wrangler kv:namespace create <name> --preview" and add its id as preview_id to the kv_namespace "TODO" in your wrangler.toml

But when I create preview KV as suggested and add it to wrangler.toml, I get:

wrangler dev .svelte-kit/cloudflare/_worker.js
 ⛅️ wrangler 3.1.1 (update available 3.2.0)
-----------------------------------------------------
wrangler dev now uses local mode by default, powered by 🔥 Miniflare and 👷 workerd.
To run an edge preview session for your Worker, use wrangler dev --remote

✘ [ERROR] Processing wrangler.toml configuration:

    - "kv_namespaces[0]" bindings should have a string "id" field but got
  {"binding":"TODO","preview_id":"c71962c1xxxx..."}.

This is what KV with preview outputs:

wrangler kv:namespace create TODO --preview
 ⛅️ wrangler 3.1.1 (update available 3.2.0)
-----------------------------------------------------
🌀 Creating namespace with title "stanovi-TODO_preview"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
{ binding = "TODO", preview_id = "c71962cxxxx..." }

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

JacobMGEvans commented 1 year ago

Thank you for reporting this,

I am going to work on this as well as other commands around products that previously required preview tokens when in development local. The current Miniflare + Workerd have made this requirement obsolete in development local scenarios and shouldn't be erroring.

related internal ticket for same issue with R2: DEVX-773

Cc: @admah

rab-dev commented 1 year ago

A workaround may be to add the namespace as both id and preview_id.

{ binding = "TODO", id = "c71962cxxxx...", preview_id = "c71962cxxxx...", }

Can @dinko-pehar confirm ?

I had the same problem and adding both id and preview_id enables wrangler to run my app although I have not yet tested extensively to confirm correct behaviour.

Edit:

See https://github.com/cloudflare/workers-sdk/issues/3763#issuecomment-1688337394

Explains wrangler code can run:

  1. local (the default)
  2. --remote on the edge
  3. --preview using preview store

So be aware id = preview_id is a hack and running local with --remote or --preview may affect your production KV store.