Skip to content

Commit 0eb973d

Browse files
Do not warn user when using a redirected config that came from a config with multiple environments (#11848)
* do not warn user when using a redirected config that came from a config with multiple environments This reverts these lines https://github.com/cloudflare/workers-sdk/pull/10706/files#diff-c5b22ef21f285ccf53ba4a990d6fba5e40050aee0dbe5a0b6737852164771d06L174-L182 which were added as a refactor but cause the warning to be emitted incorrectly. * add changeset * refactor vitest helpers to be imports * remove check that is not really needed and fails on windows * fix up for Windows
1 parent 0f8d69d commit 0eb973d

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

.changeset/social-dogs-sip.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix incorrect warning about multiple environments when using redirected config
6+
7+
Previously, when using a redirected config (via `configPath` in another config file) that originated from a config with multiple environments, wrangler would incorrectly warn about missing environment specification. This fix ensures the warning is only shown when the actual config being used has multiple environments defined, not when the original config did.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default {
22
async fetch(request, env) {
3-
return new Response("Generated: " + env.generated ?? false);
3+
return new Response("Generated: " + (env.generated ?? false));
44
},
55
};

fixtures/redirected-config-worker/tests/index.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { execSync } from "child_process";
1+
import { execSync, spawnSync } from "child_process";
22
import { resolve } from "path";
33
import { fetch } from "undici";
44
import { describe, expect, it, onTestFinished } from "vitest";
@@ -97,7 +97,30 @@ describe("'wrangler dev', when reading redirected config,", () => {
9797
const response = await fetch(`http://${ip}:${port}/`);
9898
const text = await response.text();
9999
expect(response.status).toBe(200);
100-
expect(text).toMatchInlineSnapshot(`"Generated: undefined"`);
100+
expect(text).toMatchInlineSnapshot(`"Generated: false"`);
101+
});
102+
});
103+
104+
describe("'wrangler deploy', when reading redirected config,", () => {
105+
it("uses the generated config", async () => {
106+
build("prod");
107+
const output = spawnSync("pnpm", ["wrangler", "deploy", "--dry-run"], {
108+
cwd: basePath,
109+
stdio: "pipe",
110+
shell: true,
111+
encoding: "utf-8",
112+
});
113+
expect(output.stdout).toContain(`Using redirected Wrangler configuration.`);
114+
expect(output.stdout.replace(/\\/g, "/")).toContain(
115+
` - Configuration being used: "build/wrangler.json"`
116+
);
117+
expect(output.stdout).toContain(
118+
` - Original user's configuration: "wrangler.jsonc"`
119+
);
120+
expect(output.stdout.replace(/\\/g, "/")).toContain(
121+
` - Deploy configuration file: ".wrangler/deploy/config.json"`
122+
);
123+
expect(output.stderr).toMatchInlineSnapshot(`""`);
101124
});
102125
});
103126

fixtures/redirected-config-worker/tools/build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const config = {
77
name: "redirected-config-worker",
88
compatibility_date: "2024-12-01",
99
main: "index.js",
10+
definedEnvironments: ["prod", "staging"],
1011
targetEnvironment: process.env.CLOUDFLARE_ENV,
1112
vars: { generated: process.env.CLOUDFLARE_ENV ?? "none" },
1213
};

packages/wrangler/src/core/register-yargs-command.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
UserError,
66
} from "@cloudflare/workers-utils";
77
import chalk from "chalk";
8+
import { experimental_readRawConfig } from "../../../workers-utils/src";
89
import { fetchResult } from "../cfetch";
910
import { createCloudflareClient } from "../cfetch/internal";
1011
import { readConfig } from "../config";
11-
import { hasDefinedEnvironments } from "../environments";
1212
import { run } from "../experimental-flags";
1313
import { logger } from "../logger";
1414
import { writeOutput } from "../output";
@@ -176,8 +176,15 @@ function createHandler(def: CommandDefinition, commandName: string) {
176176
: defaultWranglerConfig;
177177

178178
if (def.behaviour?.warnIfMultipleEnvsConfiguredButNoneSpecified) {
179-
if (!("env" in args)) {
180-
if (hasDefinedEnvironments(config)) {
179+
if (!("env" in args) && config.configPath) {
180+
const { rawConfig } = experimental_readRawConfig(
181+
{
182+
config: config.configPath,
183+
},
184+
{ hideWarnings: true }
185+
);
186+
const availableEnvs = Object.keys(rawConfig.env ?? {});
187+
if (availableEnvs.length > 0) {
181188
logger.warn(
182189
dedent`
183190
Multiple environments are defined in the Wrangler configuration file, but no target environment was specified for the ${commandName.replace(/^wrangler\s+/, "")} command.

0 commit comments

Comments
 (0)