Skip to content

Commit 6503ace

Browse files
fix: ensure the correct worker name is published in legacy environments (#719)
When a developer uses `--env` to specify an environment name, the Worker name should be computed from the top-level Worker name and the environment name. When the given environment name does not match those in the wrangler.toml, we error. But if no environments have been specified in the wrangler.toml, at all, then we only log a warning and continue. In this second case, we were reusing the top-level environment, which did not have the correct legacy environment fields set, such as the name. Now we ensure that such an environment is created as needed. See #680 (comment)
1 parent 18d09c7 commit 6503ace

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

.changeset/beige-olives-doubt.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: ensure the correct worker name is published in legacy environments
6+
7+
When a developer uses `--env` to specify an environment name, the Worker name should
8+
be computed from the top-level Worker name and the environment name.
9+
10+
When the given environment name does not match those in the wrangler.toml, we error.
11+
But if no environments have been specified in the wrangler.toml, at all, then we only
12+
log a warning and continue.
13+
14+
In this second case, we were reusing the top-level environment, which did not have the
15+
correct legacy environment fields set, such as the name. Now we ensure that such an
16+
environment is created as needed.
17+
18+
See https://github.com/cloudflare/wrangler2/pull/680#issuecomment-1080407556

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

+53-9
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe("publish", () => {
7272
expect(std.warn).toMatchInlineSnapshot(`""`);
7373
});
7474

75-
it("appends the environment name when provided", async () => {
75+
it("appends the environment name when provided, and there is associated config", async () => {
7676
writeWranglerToml({ env: { "some-env": {} } });
7777
writeWorkerSource();
7878
mockSubDomainRequest();
@@ -90,14 +90,58 @@ describe("publish", () => {
9090
expect(std.warn).toMatchInlineSnapshot(`""`);
9191
});
9292

93-
it("should throw an error w/ helpful message when using --env --name", async () => {
94-
writeWranglerToml({ env: { "some-env": {} } });
93+
it("appends the environment name when provided (with a warning), if there are no configured environments", async () => {
94+
writeWranglerToml({});
9595
writeWorkerSource();
9696
mockSubDomainRequest();
9797
mockUploadWorkerRequest({
9898
env: "some-env",
9999
legacyEnv: true,
100100
});
101+
await runWrangler("publish index.js --env some-env --legacy-env true");
102+
expect(std.out).toMatchInlineSnapshot(`
103+
"Uploaded test-name-some-env (TIMINGS)
104+
Published test-name-some-env (TIMINGS)
105+
test-name-some-env.test-sub-domain.workers.dev"
106+
`);
107+
expect(std.err).toMatchInlineSnapshot(`""`);
108+
expect(std.warn).toMatchInlineSnapshot(`
109+
"Processing wrangler.toml configuration:
110+
- No environment found in configuration with name \\"some-env\\".
111+
Before using \`--env=some-env\` there should be an equivalent environment section in the configuration.
112+
113+
Consider adding an environment configuration section to the wrangler.toml file:
114+
\`\`\`
115+
[env.some-env]
116+
\`\`\`
117+
"
118+
`);
119+
});
120+
121+
it("should throw an error when an environment name when provided, which doesn't match those in the config", async () => {
122+
writeWranglerToml({ env: { "other-env": {} } });
123+
writeWorkerSource();
124+
mockSubDomainRequest();
125+
await expect(
126+
runWrangler("publish index.js --env some-env --legacy-env true")
127+
).rejects.toThrowErrorMatchingInlineSnapshot(`
128+
"Processing wrangler.toml configuration:
129+
- No environment found in configuration with name \\"some-env\\".
130+
Before using \`--env=some-env\` there should be an equivalent environment section in the configuration.
131+
The available configured environment names are: [\\"other-env\\"]
132+
133+
Consider adding an environment configuration section to the wrangler.toml file:
134+
\`\`\`
135+
[env.some-env]
136+
\`\`\`
137+
"
138+
`);
139+
});
140+
141+
it("should throw an error w/ helpful message when using --env --name", async () => {
142+
writeWranglerToml({ env: { "some-env": {} } });
143+
writeWorkerSource();
144+
mockSubDomainRequest();
101145
await runWrangler(
102146
"publish index.js --name voyager --env some-env --legacy-env true"
103147
).catch((err) =>
@@ -2801,12 +2845,12 @@ export default{
28012845

28022846
await expect(runWrangler("publish index.js")).rejects
28032847
.toThrowErrorMatchingInlineSnapshot(`
2804-
"You seem to be trying to use Durable Objects in a Worker written with Service Worker syntax.
2805-
You can use Durable Objects defined in other Workers by specifying a \`script_name\` in your wrangler.toml, where \`script_name\` is the name of the Worker that implements that Durable Object. For example:
2806-
{ name = EXAMPLE_DO_BINDING, class_name = ExampleDurableObject } ==> { name = EXAMPLE_DO_BINDING, class_name = ExampleDurableObject, script_name = example-do-binding-worker }
2807-
Alternatively, migrate your worker to ES Module syntax to implement a Durable Object in this Worker:
2808-
https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/"
2809-
`);
2848+
"You seem to be trying to use Durable Objects in a Worker written with Service Worker syntax.
2849+
You can use Durable Objects defined in other Workers by specifying a \`script_name\` in your wrangler.toml, where \`script_name\` is the name of the Worker that implements that Durable Object. For example:
2850+
{ name = EXAMPLE_DO_BINDING, class_name = ExampleDurableObject } ==> { name = EXAMPLE_DO_BINDING, class_name = ExampleDurableObject, script_name = example-do-binding-worker }
2851+
Alternatively, migrate your worker to ES Module syntax to implement a Durable Object in this Worker:
2852+
https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/"
2853+
`);
28102854
});
28112855
});
28122856

packages/wrangler/src/config/validation.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ export function normalizeAndValidateConfig(
108108

109109
let activeEnv = topLevelEnv;
110110
if (envName !== undefined) {
111+
const envDiagnostics = new Diagnostics(
112+
`"env.${envName}" environment configuration`
113+
);
111114
const rawEnv = rawConfig.env?.[envName];
112115
if (rawEnv !== undefined) {
113-
const envDiagnostics = new Diagnostics(
114-
`"env.${envName}" environment configuration`
115-
);
116116
activeEnv = normalizeAndValidateEnvironment(
117117
envDiagnostics,
118118
configPath,
@@ -124,6 +124,18 @@ export function normalizeAndValidateConfig(
124124
);
125125
diagnostics.addChild(envDiagnostics);
126126
} else {
127+
// An environment was specified, but no configuration for it was found.
128+
// To cover any legacy environment cases, where the `envName` is used,
129+
// Let's create a fake active environment with the specified `envName`.
130+
activeEnv = normalizeAndValidateEnvironment(
131+
envDiagnostics,
132+
configPath,
133+
{},
134+
envName,
135+
topLevelEnv,
136+
isLegacyEnv,
137+
rawConfig
138+
);
127139
const envNames = rawConfig.env
128140
? `The available configured environment names are: ${JSON.stringify(
129141
Object.keys(rawConfig.env)

0 commit comments

Comments
 (0)