Skip to content

Commit 51fea7c

Browse files
fix: disallow setting account_id in named service environments (#814)
Much like #641, we don't want to allow setting account_id with named service environments. This is so that we use the same account_id for multiple environments, and have them group together in the dashboard.
1 parent 8c2c7b7 commit 51fea7c

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

.changeset/little-roses-invite.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: disallow setting account_id in named service environments
6+
7+
Much like https://github.com/cloudflare/wrangler2/pull/641, we don't want to allow setting account_id with named service environments. This is so that we use the same account_id for multiple environments, and have them group together in the dashboard.

packages/wrangler/src/__tests__/configuration.test.ts

+55
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,61 @@ describe("normalizeAndValidateConfig()", () => {
17631763
});
17641764
});
17651765

1766+
it("should error if named environment contains a `account_id` field, even if there is no top-level name", () => {
1767+
const rawConfig: RawConfig = {
1768+
legacy_env: false,
1769+
env: {
1770+
DEV: {
1771+
account_id: "some_account_id",
1772+
},
1773+
},
1774+
};
1775+
1776+
const { config, diagnostics } = normalizeAndValidateConfig(
1777+
rawConfig,
1778+
undefined,
1779+
{ env: "DEV" }
1780+
);
1781+
1782+
expect(config.account_id).toBeUndefined();
1783+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1784+
"Processing wrangler configuration:
1785+
1786+
- \\"env.DEV\\" environment configuration
1787+
- The \\"account_id\\" field is not allowed in named service environments.
1788+
Please remove the field from this environment."
1789+
`);
1790+
expect(diagnostics.hasWarnings()).toBe(false);
1791+
});
1792+
1793+
it("should error if top-level config and a named environment both contain a `account_id` field", () => {
1794+
const rawConfig: RawConfig = {
1795+
account_id: "ACCOUNT_ID",
1796+
legacy_env: false,
1797+
env: {
1798+
DEV: {
1799+
account_id: "ENV_ACCOUNT_ID",
1800+
},
1801+
},
1802+
};
1803+
1804+
const { config, diagnostics } = normalizeAndValidateConfig(
1805+
rawConfig,
1806+
undefined,
1807+
{ env: "DEV" }
1808+
);
1809+
1810+
expect(config.account_id).toEqual("ACCOUNT_ID");
1811+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
1812+
"Processing wrangler configuration:
1813+
1814+
- \\"env.DEV\\" environment configuration
1815+
- The \\"account_id\\" field is not allowed in named service environments.
1816+
Please remove the field from this environment."
1817+
`);
1818+
expect(diagnostics.hasWarnings()).toBe(false);
1819+
});
1820+
17661821
it("should warn for non-inherited fields that are missing in environments", () => {
17671822
const vars: RawConfig["vars"] = {
17681823
FOO: "foo",

packages/wrangler/src/config/validation-helpers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function inheritableInLegacyEnvironments<K extends keyof Environment>(
8282
rawEnv: RawEnvironment,
8383
field: K,
8484
validate: ValidatorFn,
85-
transformFn: TransformFn<Environment[K]>,
85+
transformFn: TransformFn<Environment[K]> = (v) => v,
8686
defaultValue: Environment[K]
8787
): Environment[K] {
8888
return topLevelEnv === undefined || isLegacyEnv === true

packages/wrangler/src/config/validation.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,14 @@ function normalizeAndValidateEnvironment(
659659

660660
const environment: Environment = {
661661
// Inherited fields
662-
account_id: inheritable(
662+
account_id: inheritableInLegacyEnvironments(
663663
diagnostics,
664+
isLegacyEnv,
664665
topLevelEnv,
665666
rawEnv,
666667
"account_id",
667668
isString,
669+
undefined,
668670
undefined
669671
),
670672
compatibility_date: inheritable(

0 commit comments

Comments
 (0)