Skip to content

--local commands read a different state directory than cf dev, and local list commands return [] #224

Closed

Description

@risnake

Summary of the issue

After cf dev, the --local API commands can't see the data the dev server wrote, and the local list commands can't help you find it.

  1. Different default state directories. In a cf init project, cf dev persists to ./.cloudflare/state. --local commands default to ~/.config/cloudflare/state (per --help), even when run from the project directory. So without --persist-to they read an empty store and return [] with exit 0, which looks like "no data" instead of "wrong store".
  2. Local list commands return [] even with the right state directory. With --persist-to ./.cloudflare/state, cf kv namespaces list --local and cf d1 list --local both return []. The running dev server's Local Explorer lists the namespace (CACHE-demo) and database (DB-demo), and cf kv keys list --namespace-id CACHE-demo / cf d1 raw DB-demo work once you know those IDs. The only way to find the IDs from the CLI is to guess the <BINDING>-<worker> pattern.
Command (after cf dev wrote one KV key and one D1 row) Result
cf kv keys list --namespace-id CACHE-demo --local []
cf kv keys list --namespace-id CACHE-demo --local --persist-to .cloudflare/state [{"name":"hits"}]
cf kv namespaces list --local --persist-to .cloudflare/state []
cf d1 list --local --persist-to .cloudflare/state []
GET /cdn-cgi/local/explorer/api/storage/kv/namespaces (dev server running) [{"id":"CACHE-demo","title":"CACHE"}]
GET /cdn-cgi/local/explorer/api/d1/database (dev server running) [{"name":"DB","uuid":"DB-demo",...}]

Expected behaviour

  • Inside a project, --local commands use the same state directory as cf dev (or at least say which directory they read).
  • kv namespaces list --local and d1 list --local list the namespaces and databases in that state, using the same IDs the Local Explorer and the other --local commands accept. If listing isn't supported in cold state, return an error instead of []. The README says "Commands with no local explorer equivalent return an error".

Version of cf used

1.0.0-beta.12

System information and package versions

System:
  OS: macOS 27.0
  CPU: (18) arm64 Apple M5 Pro
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 26.8.2
  npm: 11.19.1
npmPackages:
  @cloudflare/vite-plugin: beta => 2.0.0-beta.sha-52b0dc0e9
  cf: ^1.0.0-beta.12 => 1.0.0-beta.12

Reproduction (optional)

cf init demo --package-manager npm && cd demo

cloudflare.config.ts, env:

CACHE: bindings.kv({}),
DB: bindings.d1({}),

src/index.ts:

export default {
	async fetch(req, env) {
		const n = Number((await env.CACHE.get("hits")) ?? 0) + 1;
		await env.CACHE.put("hits", String(n));
		await env.DB.exec("CREATE TABLE IF NOT EXISTS visits (id INTEGER PRIMARY KEY, at TEXT)");
		await env.DB.prepare("INSERT INTO visits (at) VALUES (?)").bind(new Date().toISOString()).run();
		return new Response(String(n));
	},
} satisfies ExportedHandler<Env>;
cf dev            # then curl http://localhost:5173 once, then stop it
cf kv keys list --namespace-id CACHE-demo --local                              # []
cf kv keys list --namespace-id CACHE-demo --local --persist-to .cloudflare/state  # [{"name":"hits"}]
cf kv namespaces list --local --persist-to .cloudflare/state                   # []
cf d1 list --local --persist-to .cloudflare/state                              # []

Relevant error logs (optional)

None. Every command exits 0. Related: #22 notes that --local and wrangler dev also default to different directories.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions