diff --git a/.changeset/wise-pandas-warn.md b/.changeset/wise-pandas-warn.md new file mode 100644 index 00000000000..9a640af4ab4 --- /dev/null +++ b/.changeset/wise-pandas-warn.md @@ -0,0 +1,8 @@ +--- +"@cloudflare/workers-utils": patch +"wrangler": patch +--- + +Warn when `CLOUDFLARE_COMPLIANCE_REGION` overrides a conflicting configured compliance region + +Wrangler now explains that the environment variable takes precedence and continues using its value instead of rejecting the command. diff --git a/packages/workers-utils/src/environment-variables/misc-variables.ts b/packages/workers-utils/src/environment-variables/misc-variables.ts index e533bff9852..be1ef3dac89 100644 --- a/packages/workers-utils/src/environment-variables/misc-variables.ts +++ b/packages/workers-utils/src/environment-variables/misc-variables.ts @@ -1,6 +1,4 @@ import path from "node:path"; -import { dedent } from "ts-dedent"; -import { UserError } from "../errors"; import { getGlobalConfigPath } from "../global-wrangler-config-path"; import { getBooleanEnvironmentVariableFactory, @@ -101,20 +99,6 @@ export const getCloudflareComplianceRegion = ( complianceConfig: ComplianceConfig ) => { const complianceRegionFromEnv = getCloudflareComplianceRegionFromEnv(); - if ( - complianceRegionFromEnv !== undefined && - complianceConfig?.compliance_region !== undefined && - complianceRegionFromEnv !== complianceConfig.compliance_region - ) { - throw new UserError( - dedent` - The compliance region has been set to different values in two places: - - \`CLOUDFLARE_COMPLIANCE_REGION\` environment variable: \`${complianceRegionFromEnv}\` - - \`compliance_region\` configuration property: \`${complianceConfig.compliance_region}\` - `, - { telemetryMessage: false } - ); - } return ( complianceRegionFromEnv || complianceConfig?.compliance_region || "public" ); diff --git a/packages/wrangler/src/__tests__/deploy/environments.test.ts b/packages/wrangler/src/__tests__/deploy/environments.test.ts index f049de04a07..3621a1ae6a0 100644 --- a/packages/wrangler/src/__tests__/deploy/environments.test.ts +++ b/packages/wrangler/src/__tests__/deploy/environments.test.ts @@ -451,19 +451,23 @@ describe("deploy", () => { await runWrangler("deploy ./index.js"); }); - it("should error if the region is set in both env var and configured, and they conflict", async ({ + it("should warn and use the env var if it conflicts with the configured region", async ({ expect, }) => { vi.stubEnv("CLOUDFLARE_COMPLIANCE_REGION", "public"); writeWranglerConfig({ compliance_region: "fedramp_high" }); writeWorkerSource(); + mockUploadWorkerRequest({ + expectedBaseUrl: "api.cloudflare.com", + }); + mockSubDomainRequest(); + mockGetWorkerSubdomain({ enabled: true }); - await expect(runWrangler("deploy ./index.js")).rejects - .toThrowErrorMatchingInlineSnapshot(` - [Error: The compliance region has been set to different values in two places: - - \`CLOUDFLARE_COMPLIANCE_REGION\` environment variable: \`public\` - - \`compliance_region\` configuration property: \`fedramp_high\`] - `); + await runWrangler("deploy ./index.js"); + + expect(std.warn).toContain( + 'The compliance region was resolved to "public" from the `CLOUDFLARE_COMPLIANCE_REGION` environment variable, which takes precedence over the configured value "fedramp_high".' + ); }); it("should not error if the region is set in both env var and configured, and they are the same", async () => { diff --git a/packages/wrangler/src/config/index.ts b/packages/wrangler/src/config/index.ts index 72e94aea161..41c508a4f61 100644 --- a/packages/wrangler/src/config/index.ts +++ b/packages/wrangler/src/config/index.ts @@ -4,6 +4,7 @@ import { configFileName, experimental_readRawConfig, FatalError, + getCloudflareComplianceRegion, isPagesConfig, normalizeAndValidateConfig, UserError, @@ -68,6 +69,25 @@ async function logWarningsWithUpgradeHint( } } +function logWarningsIfComplianceRegionIsOverridden( + config: Config, + hideWarnings: boolean | undefined +): void { + const configuredRegion = config.compliance_region; + if (hideWarnings || configuredRegion === undefined) { + return; + } + + const resolvedRegion = getCloudflareComplianceRegion(config); + if (resolvedRegion === configuredRegion) { + return; + } + + logger.once.warn( + `The compliance region was resolved to "${resolvedRegion}" from the \`CLOUDFLARE_COMPLIANCE_REGION\` environment variable, which takes precedence over the configured value "${configuredRegion}".` + ); +} + /** * Carries the validated `Config` alongside the * watcher dependency set and the normalised type-generation settings. @@ -123,6 +143,7 @@ export async function readNewConfig( ); void logWarningsWithUpgradeHint(diagnostics, options.hideWarnings); + logWarningsIfComplianceRegionIsOverridden(config, options.hideWarnings); if (diagnostics.hasErrors()) { throw new UserError(diagnostics.renderErrors(), { telemetryMessage: "new-config worker validation failed", @@ -175,6 +196,7 @@ export function readConfig( ); void logWarningsWithUpgradeHint(diagnostics, options?.hideWarnings); + logWarningsIfComplianceRegionIsOverridden(config, options.hideWarnings); if (diagnostics.hasErrors()) { throw new UserError(diagnostics.renderErrors(), { telemetryMessage: "config wrangler validation failed", @@ -238,6 +260,7 @@ export function readPagesConfig( ); void logWarningsWithUpgradeHint(diagnostics, options.hideWarnings); + logWarningsIfComplianceRegionIsOverridden(config, options.hideWarnings); if (diagnostics.hasErrors()) { throw new UserError(diagnostics.renderErrors(), { telemetryMessage: "config pages validation failed",