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

`wrangler d1 migrations list --remote` ignores `account_id` from the config file

Open spiflicate opened this issue 1 month ago • 3 comments

What versions & operating system are you using?

wrangler 4.151.0 (also 4.134.0), Node 22, macOS 15 (Darwin 24.6.0, arm64)

Please provide a link to a minimal reproduction

No repo needed. Any project whose wrangler.jsonc sets account_id and has a D1 binding, run by a user whose login can see more than one account:

{
  "name": "example",
  "account_id": "<account A>",
  "d1_databases": [
    { "binding": "DB", "database_name": "example-db", "database_id": "<id>", "migrations_dir": "migrations" }
  ]
}
wrangler d1 migrations list example-db --remote   # in a non-interactive shell

Describe the Bug

wrangler d1 migrations list --remote ignores account_id from the config file. With more than one account on the login, it fails in a non-interactive shell (or prompts in an interactive one) even though the config names the account.

The handler authenticates with an empty config instead of the loaded one:

https://github.com/cloudflare/workers-sdk/blob/4a5377fc228dd94aa5df7545c8185cdd98be733e/packages/wrangler/src/d1/migrations/list.ts#L53-L55

if (remote) {
	await requireAuth({});
}

getActiveAccountId() only reads config.account_id, so passing {} means the account from wrangler.jsonc is never considered. wrangler d1 migrations apply --remote on the same project works, as do other commands that pass config. Passing config here (await requireAuth(config);) should fix it.

Expected: the command uses account_id from the config, as apply does. Workaround: set CLOUDFLARE_ACCOUNT_ID.

Please provide any relevant error logs

✘ [ERROR] More than one account available but unable to select one in non-interactive mode.

  Please set the appropriate `account_id` in your Wrangler configuration file or assign it to the `CLOUDFLARE_ACCOUNT_ID` environment variable.
  Available accounts are (`<name>`: `<account_id>`):
    `<account A>`: `<id>`
    `<account B>`: `<id>`

spiflicate avatar Oct 15 '26 16:10 spiflicate

Automated triage

Issue assessment

  • Actionability: ✅ Actionable.
  • Type: Bug.
  • Reproduction: Automatic attempt requested.
  • Labels:
    • bug (high)
    • package:wrangler (high)
    • product:d1 (high)
    • auth (medium)
Actionability and missing information

wrangler d1 migrations list --remote calls requireAuth with an empty config, so account_id from the config file is ignored and multi-account logins fail in non-interactive mode; apply and other commands pass the config and work. Reported on wrangler 4.151.0/4.134.0, Node 22, macOS 15.

Reproduction reason: The report gives a minimal config and command and identifies the empty-config auth call, describing a current behavior defect that can be verified by running the command with a multi-account login in non-interactive mode.

Reproduction

  • Result: ✅ Reproduced.
  • Summary: Reproduced with wrangler 4.151.0: d1 migrations list --remote in a non-interactive shell ignores the config's account_id when the login sees 2 accounts and errors "More than one account available..." (exit 1); with CLOUDFLARE_ACCOUNT_ID set the same command succeeds.
Evidence and attempted steps

Steps attempted:

  1. Cloned workers-sdk at commit 4a5377fc (wrangler 4.151.0, the exact commit/version cited in the issue) as setup reference; confirmed the reported code path exists at HEAD.
    • Command: git clone --depth 1 https://github.com/cloudflare/workers-sdk /workspace/workers-sdk
  2. Built the fixture: wrangler.jsonc with account_id (Example Account A) + D1 binding + migrations dir; pinned wrangler 4.151.0 exactly and installed with pnpm.
    • Command: cd /workspace/run/project && pnpm install
  3. Simulated the issue's prerequisites without real credentials: placeholder OAuth token file in isolated $XDG_CONFIG_HOME, and a local mock Cloudflare API (2 accounts) via CLOUDFLARE_API_BASE_URL override; ran the reported command non-interactively with CLOUDFLARE_ACCOUNT_ID unset.
    • Command: env -u CLOUDFLARE_ACCOUNT_ID XDG_CONFIG_HOME=/workspace/run/home/.config CLOUDFLARE_AUTH_USE_KEYRING=false WRANGLER_SEND_METRICS=false CLOUDFLARE_API_BASE_URL=http://127.0.0.1:8799/client/v4 pnpm exec wrangler d1 migrations list example-db --remote < /dev/null
  4. Baseline run to distinguish the bug from a broken setup: identical command and mock environment with the issue's documented workaround CLOUDFLARE_ACCOUNT_ID set; expected success per the issue.
    • Command: env CLOUDFLARE_ACCOUNT_ID=fca9dbd8e26f4f9cbd7d5a1b3e4a5c6d XDG_CONFIG_HOME=/workspace/run/home/.config CLOUDFLARE_AUTH_USE_KEYRING=false WRANGLER_SEND_METRICS=false CLOUDFLARE_API_BASE_URL=http://127.0.0.1:8799/client/v4 pnpm exec wrangler d1 migrations list example-db --remote < /dev/null

Expected behavior: wrangler d1 migrations list example-db --remote should resolve the account from account_id in the wrangler.jsonc config file (as d1 migrations apply --remote does) and list unapplied migrations without prompting or erroring, even when the logged-in user can see more than one account and the shell is non-interactive.

Observed behavior: Exit code 1 with the exact reported error: [ERROR] More than one account available but unable to select one in non-interactive mode. / Please set the appropriate account_id in your Wrangler configuration file or assign it to the CLOUDFLARE_ACCOUNT_ID environment variable. / Available accounts are: Example Account A: fca9dbd8e26f4f9cbd7d5a1b3e4a5c6d, Example Account B: a1b2c3d4e5f60718293a4b5c6d7e8f90 — despite account_id being set in wrangler.jsonc. The API request log shows the command fetched GET /accounts and GET /memberships (i.e. it resolved the account from the API instead of the config). Baseline: with CLOUDFLARE_ACCOUNT_ID set (the issue's documented workaround), the identical command succeeds (exit 0, "No migrations to apply!") with no account-listing calls.

Artifact: Download reproduction archive (Cloudflare Access required).

Treat archive contents as untrusted. Inspect them before running anything.


This summary was generated by an LLM and may contain errors. Please verify its findings.

Triage bot commands and options

Repository collaborators with write or admin access can trigger actions by commenting on this issue:

  • @workers-devprod reproduce: Attempt to reproduce the issue using its description and recent comments.

workers-devprod avatar Oct 15 '26 16:10 workers-devprod

Hi, I'd like to fix this if that's OK. As the triage notes, d1MigrationsListCommand calls requireAuth({}) in its --remote branch, so getActiveAccountId() never sees account_id from the config. The fix is requireAuth(config), which matches how the other D1 commands authenticate.

I have it ready with two tests in src/__tests__/d1/migrate.test.ts under list. They run without CLOUDFLARE_ACCOUNT_ID (the existing list tests set it, which is why this wasn't caught) and with two accounts on the login. Without account_id in config, the command still errors in non-interactive mode. With account_id: "nx01", it lists the migrations, and every D1 query goes to nx01. The new test fails on main with the error from this issue. A wrangler patch changeset is included.

Happy to open the PR whenever you're ready.

idhanth avatar Oct 15 '26 21:10 idhanth

@idhanth sorry for the late reply

If you'd like to open a PR to fix this issue that would be very welcomes 🙂 🙏

dario-piotrowicz avatar Oct 28 '26 18:10 dario-piotrowicz