Skip to content

Commit

Permalink
fix: disallow setting account_id in named service environments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
threepointone committed Apr 16, 2022
1 parent d2f5ebb commit c4e9921
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/little-roses-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: disallow setting account_id in named service environments

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.
55 changes: 55 additions & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,61 @@ describe("normalizeAndValidateConfig()", () => {
});
});

it("should error if named environment contains a `account_id` field, even if there is no top-level name", () => {
const rawConfig: RawConfig = {
legacy_env: false,
env: {
DEV: {
account_id: "some_account_id",
},
},
};

const { config, diagnostics } = normalizeAndValidateConfig(
rawConfig,
undefined,
{ env: "DEV" }
);

expect(config.account_id).toBeUndefined();
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- \\"env.DEV\\" environment configuration
- The \\"account_id\\" field is not allowed in named service environments.
Please remove the field from this environment."
`);
expect(diagnostics.hasWarnings()).toBe(false);
});

it("should error if top-level config and a named environment both contain a `account_id` field", () => {
const rawConfig: RawConfig = {
account_id: "ACCOUNT_ID",
legacy_env: false,
env: {
DEV: {
account_id: "ENV_ACCOUNT_ID",
},
},
};

const { config, diagnostics } = normalizeAndValidateConfig(
rawConfig,
undefined,
{ env: "DEV" }
);

expect(config.account_id).toEqual("ACCOUNT_ID");
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- \\"env.DEV\\" environment configuration
- The \\"account_id\\" field is not allowed in named service environments.
Please remove the field from this environment."
`);
expect(diagnostics.hasWarnings()).toBe(false);
});

it("should warn for non-inherited fields that are missing in environments", () => {
const vars: RawConfig["vars"] = {
FOO: "foo",
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/config/validation-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function inheritableInLegacyEnvironments<K extends keyof Environment>(
rawEnv: RawEnvironment,
field: K,
validate: ValidatorFn,
transformFn: TransformFn<Environment[K]>,
transformFn: TransformFn<Environment[K]> = (v) => v,
defaultValue: Environment[K]
): Environment[K] {
return topLevelEnv === undefined || isLegacyEnv === true
Expand Down
4 changes: 3 additions & 1 deletion packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,14 @@ function normalizeAndValidateEnvironment(

const environment: Environment = {
// Inherited fields
account_id: inheritable(
account_id: inheritableInLegacyEnvironments(
diagnostics,
isLegacyEnv,
topLevelEnv,
rawEnv,
"account_id",
isString,
undefined,
undefined
),
compatibility_date: inheritable(
Expand Down

0 comments on commit c4e9921

Please sign in to comment.