Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/pages-project-commands-env-account-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

Let `CLOUDFLARE_ACCOUNT_ID` override the cached account id in `wrangler pages project` commands

`pages project list`, `pages project create` and `pages project delete` passed the internal Pages cache (`pages.json`) straight to account selection, which treats `account_id` as user-authored configuration and therefore ranks it above `CLOUDFLARE_ACCOUNT_ID`. In a multi-account setup a stale cached account won silently, so the command targeted the wrong account and failed with `Authentication error [code: 10000]`.

These three commands now overlay the environment account id on top of the cache before resolving auth, which is what `pages deploy`, `pages deployment list`, `pages deployment delete`, `pages download config` and `pages secret` already do. The cache is still used as a fallback when the environment variable is unset.
11 changes: 6 additions & 5 deletions packages/wrangler/src/__tests__/pages/project-create.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { afterEach, describe, it, vi } from "vitest";
import { saveToConfigCache } from "../../config-cache";
import { PAGES_CONFIG_CACHE_FILENAME } from "../../pages/constants";
import { endEventLoop } from "../helpers/end-event-loop";
import { mockAccountId, mockApiToken } from "./../helpers/mock-account-id";
import { mockConsoleMethods } from "./../helpers/mock-console";
import { msw } from "./../helpers/msw";
import { runWrangler } from "./../helpers/run-wrangler";
import type { PagesConfigCache } from "../../pages/types";

describe("pages project create", () => {
const std = mockConsoleMethods();
Expand Down Expand Up @@ -186,11 +189,9 @@ describe("pages project create", () => {
{ once: true }
)
);
vi.mock("getConfigCache", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signoff: joe345-str/joeydonner1979@gmail.com

return {
account_id: "original-account-id",
project_name: "an-existing-project",
};
saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
account_id: "original-account-id",
project_name: "an-existing-project",
});
vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "new-account-id");
await runWrangler(
Expand Down
11 changes: 6 additions & 5 deletions packages/wrangler/src/__tests__/pages/project-delete.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, describe, it, vi } from "vitest";
import { saveToConfigCache } from "../../config-cache";
import { PAGES_CONFIG_CACHE_FILENAME } from "../../pages/constants";
import { endEventLoop } from "../helpers/end-event-loop";
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
import { mockConsoleMethods } from "../helpers/mock-console";
import { clearDialogs, mockConfirm } from "../helpers/mock-dialogs";
import { useMockIsTTY } from "../helpers/mock-istty";
import { msw } from "../helpers/msw";
import { runWrangler } from "../helpers/run-wrangler";
import type { PagesConfigCache } from "../../pages/types";

describe("pages project delete", () => {
const std = mockConsoleMethods();
Expand Down Expand Up @@ -151,11 +154,9 @@ describe("pages project delete", () => {
text: `Are you sure you want to delete "an-existing-project"? This action cannot be undone.`,
result: true,
});
vi.mock("getConfigCache", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signoff: joe345-str/joeydonner1979@gmail.com

return {
account_id: "original-account-id",
project_name: "an-existing-project",
};
saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
account_id: "original-account-id",
project_name: "an-existing-project",
});
vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "new-account-id");
await runWrangler("pages project delete an-existing-project");
Expand Down
12 changes: 6 additions & 6 deletions packages/wrangler/src/__tests__/pages/project-list.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
import { http, HttpResponse } from "msw";
import { afterEach, describe, it, vi } from "vitest";
import { saveToConfigCache } from "../../config-cache";
import { PAGES_CONFIG_CACHE_FILENAME } from "../../pages/constants";
import { endEventLoop } from "../helpers/end-event-loop";
import { mockConsoleMethods } from "../helpers/mock-console";
import { mockAccountId, mockApiToken } from "./../helpers/mock-account-id";
import { msw } from "./../helpers/msw";
import { runWrangler } from "./../helpers/run-wrangler";
import type { Project } from "./../../pages/types";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signoff: joe345-str/joeydonner1979@gmail.com

import type { PagesConfigCache, Project } from "./../../pages/types";
import type { ExpectStatic } from "vitest";

describe("pages project list", () => {
Expand Down Expand Up @@ -83,11 +85,9 @@ describe("pages project list", () => {
it("should override cached accountId with CLOUDFLARE_ACCOUNT_ID environmental variable if provided", async ({
expect,
}) => {
vi.mock("getConfigCache", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signoff: joe345-str/joeydonner1979@gmail.com

return {
account_id: "original-account-id",
project_name: "an-existing-project",
};
saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
account_id: "original-account-id",
project_name: "an-existing-project",
});
vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "new-account-id");
const requests = mockProjectListRequest(expect, [], "new-account-id");
Expand Down
19 changes: 16 additions & 3 deletions packages/wrangler/src/pages/projects.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execSync } from "node:child_process";
import { getCloudflareAccountIdFromEnv } from "@cloudflare/workers-auth";
import {
COMPLIANCE_REGION_CONFIG_PUBLIC,
UserError,
Expand Down Expand Up @@ -42,7 +43,11 @@ export const pagesProjectListCommand = createCommand({
PAGES_CONFIG_CACHE_FILENAME
);

const accountId = await requireAuth(config);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signoff: joe345-str/joeydonner1979@gmail.com

const envAccountId = getCloudflareAccountIdFromEnv();
const accountId = await requireAuth({
...config,
...(envAccountId ? { account_id: envAccountId } : {}),
});

const projects: Array<Project> = await listProjects({ accountId });

Expand Down Expand Up @@ -148,7 +153,11 @@ export const pagesProjectCreateCommand = createCommand({
const config = getConfigCache<PagesConfigCache>(
PAGES_CONFIG_CACHE_FILENAME
);
const accountId = await requireAuth(config);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signoff: joe345-str/joeydonner1979@gmail.com

const envAccountId = getCloudflareAccountIdFromEnv();
const accountId = await requireAuth({
...config,
...(envAccountId ? { account_id: envAccountId } : {}),
});

// When run by an AI agent, delegate new static Pages projects to a Workers
// static-assets deploy of the current directory. Accounts that already
Expand Down Expand Up @@ -303,7 +312,11 @@ export const pagesProjectDeleteCommand = createCommand({
const config = getConfigCache<PagesConfigCache>(
PAGES_CONFIG_CACHE_FILENAME
);
const accountId = await requireAuth(config);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signoff: joe345-str/joeydonner1979@gmail.com

const envAccountId = getCloudflareAccountIdFromEnv();
const accountId = await requireAuth({
...config,
...(envAccountId ? { account_id: envAccountId } : {}),
});

const confirmed =
args.yes ||
Expand Down