Skip to content

Commit 2e90efc

Browse files
fix: ensure that non-inherited fields are not removed when using an inferred named environment (#7478)
It is an error for the the user to provide an environment name that doesn't match any of the named environments in the Wrangler configuration. But if there are no named environments defined at all in the Wrangler configuration, we special case the top-level environment as though it was a named environment. Previously, when this happens, we would remove all the nonInheritable fields from the configuration (essentially all the bindings) leaving an incorrect configuration. Now we correctly generate a flattened named environment that has the nonInheritable fields, plus correctly applies any transformFn on inheritable fields.
1 parent ee98fd4 commit 2e90efc

File tree

4 files changed

+90
-63
lines changed

4 files changed

+90
-63
lines changed

.changeset/khaki-steaks-sniff.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: ensure that non-inherited fields are not removed when using an inferred named environment
6+
7+
It is an error for the the user to provide an environment name that doesn't match any of the named environments in the Wrangler configuration.
8+
But if there are no named environments defined at all in the Wrangler configuration, we special case the top-level environment as though it was a named environment.
9+
Previously, when this happens, we would remove all the nonInheritable fields from the configuration (essentially all the bindings) leaving an incorrect configuration.
10+
Now we correctly generate a flattened named environment that has the nonInheritable fields, plus correctly applies any transformFn on inheritable fields.

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

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3854,22 +3854,35 @@ describe("normalizeAndValidateConfig()", () => {
38543854

38553855
describe("named environments", () => {
38563856
it("should warn if we specify an environment but there are no named environments", () => {
3857-
const rawConfig: RawConfig = {};
3858-
const { diagnostics } = normalizeAndValidateConfig(rawConfig, undefined, {
3859-
env: "DEV",
3860-
});
3857+
const rawConfig: RawConfig = {
3858+
name: "my-worker",
3859+
kv_namespaces: [{ binding: "KV", id: "xxxx-xxxx-xxxx-xxxx" }],
3860+
};
3861+
const { diagnostics, config } = normalizeAndValidateConfig(
3862+
rawConfig,
3863+
undefined,
3864+
{
3865+
env: "dev",
3866+
}
3867+
);
3868+
expect(config).toEqual(
3869+
expect.objectContaining({
3870+
name: "my-worker-dev",
3871+
kv_namespaces: [{ binding: "KV", id: "xxxx-xxxx-xxxx-xxxx" }],
3872+
})
3873+
);
38613874
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
38623875
"Processing wrangler configuration:
38633876
"
38643877
`);
38653878
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
38663879
"Processing wrangler configuration:
3867-
- No environment found in configuration with name \\"DEV\\".
3868-
Before using \`--env=DEV\` there should be an equivalent environment section in the configuration.
3880+
- No environment found in configuration with name \\"dev\\".
3881+
Before using \`--env=dev\` there should be an equivalent environment section in the configuration.
38693882
38703883
Consider adding an environment configuration section to the Wrangler configuration file:
38713884
\`\`\`
3872-
[env.DEV]
3885+
[env.dev]
38733886
\`\`\`
38743887
"
38753888
`);
@@ -4111,70 +4124,70 @@ describe("normalizeAndValidateConfig()", () => {
41114124
Please remove the field from this environment."
41124125
`);
41134126
});
4114-
});
41154127

4116-
it("should error if named environment contains a `account_id` field, even if there is no top-level name", () => {
4117-
const rawConfig: RawConfig = {
4118-
legacy_env: false,
4119-
env: {
4120-
DEV: {
4121-
account_id: "some_account_id",
4128+
it("should error if named environment contains a `account_id` field, even if there is no top-level name", () => {
4129+
const rawConfig: RawConfig = {
4130+
legacy_env: false,
4131+
env: {
4132+
DEV: {
4133+
account_id: "some_account_id",
4134+
},
41224135
},
4123-
},
4124-
};
4136+
};
41254137

4126-
const { config, diagnostics } = normalizeAndValidateConfig(
4127-
rawConfig,
4128-
undefined,
4129-
{ env: "DEV" }
4130-
);
4138+
const { config, diagnostics } = normalizeAndValidateConfig(
4139+
rawConfig,
4140+
undefined,
4141+
{ env: "DEV" }
4142+
);
41314143

4132-
expect(diagnostics.hasWarnings()).toBe(true);
4133-
expect(config.account_id).toBeUndefined();
4134-
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
4135-
"Processing wrangler configuration:
4136-
- Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
4137-
`);
4138-
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
4139-
"Processing wrangler configuration:
4144+
expect(diagnostics.hasWarnings()).toBe(true);
4145+
expect(config.account_id).toBeUndefined();
4146+
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
4147+
"Processing wrangler configuration:
4148+
- Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
4149+
`);
4150+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
4151+
"Processing wrangler configuration:
41404152
4141-
- \\"env.DEV\\" environment configuration
4142-
- The \\"account_id\\" field is not allowed in named service environments.
4143-
Please remove the field from this environment."
4144-
`);
4145-
});
4153+
- \\"env.DEV\\" environment configuration
4154+
- The \\"account_id\\" field is not allowed in named service environments.
4155+
Please remove the field from this environment."
4156+
`);
4157+
});
41464158

4147-
it("should error if top-level config and a named environment both contain a `account_id` field", () => {
4148-
const rawConfig: RawConfig = {
4149-
account_id: "ACCOUNT_ID",
4150-
legacy_env: false,
4151-
env: {
4152-
DEV: {
4153-
account_id: "ENV_ACCOUNT_ID",
4159+
it("should error if top-level config and a named environment both contain a `account_id` field", () => {
4160+
const rawConfig: RawConfig = {
4161+
account_id: "ACCOUNT_ID",
4162+
legacy_env: false,
4163+
env: {
4164+
DEV: {
4165+
account_id: "ENV_ACCOUNT_ID",
4166+
},
41544167
},
4155-
},
4156-
};
4168+
};
41574169

4158-
const { config, diagnostics } = normalizeAndValidateConfig(
4159-
rawConfig,
4160-
undefined,
4161-
{ env: "DEV" }
4162-
);
4170+
const { config, diagnostics } = normalizeAndValidateConfig(
4171+
rawConfig,
4172+
undefined,
4173+
{ env: "DEV" }
4174+
);
41634175

4164-
expect(config.account_id).toEqual("ACCOUNT_ID");
4165-
expect(diagnostics.hasErrors()).toBe(true);
4166-
expect(diagnostics.hasWarnings()).toBe(true);
4167-
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
4168-
"Processing wrangler configuration:
4169-
- Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
4170-
`);
4171-
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
4172-
"Processing wrangler configuration:
4176+
expect(config.account_id).toEqual("ACCOUNT_ID");
4177+
expect(diagnostics.hasErrors()).toBe(true);
4178+
expect(diagnostics.hasWarnings()).toBe(true);
4179+
expect(diagnostics.renderWarnings()).toMatchInlineSnapshot(`
4180+
"Processing wrangler configuration:
4181+
- Experimental: Service environments are in beta, and their behaviour is guaranteed to change in the future. DO NOT USE IN PRODUCTION."
4182+
`);
4183+
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
4184+
"Processing wrangler configuration:
41734185
4174-
- \\"env.DEV\\" environment configuration
4175-
- The \\"account_id\\" field is not allowed in named service environments.
4176-
Please remove the field from this environment."
4177-
`);
4186+
- \\"env.DEV\\" environment configuration
4187+
- The \\"account_id\\" field is not allowed in named service environments.
4188+
Please remove the field from this environment."
4189+
`);
4190+
});
41784191
});
41794192

41804193
it("should warn for non-inherited fields that are missing in environments", () => {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ export function inheritable<K extends keyof Environment>(
6969
): Environment[K] {
7070
validate(diagnostics, field, rawEnv[field], topLevelEnv);
7171
return (
72-
(rawEnv[field] as Environment[K]) ??
72+
// `rawEnv === topLevelEnv` is a special case where the user has provided an environment name
73+
// but that named environment is not actually defined in the configuration.
74+
// In that case we have reused the topLevelEnv as the rawEnv,
75+
// and so we need to process the `transformFn()` anyway rather than just using the field in the `rawEnv`.
76+
(rawEnv !== topLevelEnv ? (rawEnv[field] as Environment[K]) : undefined) ??
7377
transformFn(topLevelEnv?.[field]) ??
7478
defaultValue
7579
);

packages/wrangler/src/config/validation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export function normalizeAndValidateConfig(
224224
activeEnv = normalizeAndValidateEnvironment(
225225
envDiagnostics,
226226
configPath,
227-
{},
227+
topLevelEnv, // in this case reuse the topLevelEnv to ensure that nonInherited fields are not removed
228228
isDispatchNamespace,
229229
envName,
230230
topLevelEnv,

0 commit comments

Comments
 (0)