From 4b476b4e26c96778e3bd195d6c618e48e2387caa Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Sun, 2 Nov 2025 13:16:44 +0530 Subject: [PATCH 1/2] feat(cli): add detailed non-prod build environment check for Convex deploy (#28) --- npm-packages/convex/src/cli/deploy.ts | 15 +++++---------- npm-packages/convex/src/cli/lib/envvars.ts | 15 +++++++++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/npm-packages/convex/src/cli/deploy.ts b/npm-packages/convex/src/cli/deploy.ts index 254ce76ef..7c3472c72 100644 --- a/npm-packages/convex/src/cli/deploy.ts +++ b/npm-packages/convex/src/cli/deploy.ts @@ -80,20 +80,15 @@ Same format as .env.local or .env files, and overrides them.`, const ctx = await oneoffContext(cmdOptions); const deploymentSelection = await getDeploymentSelection(ctx, cmdOptions); + + const nonProdReason = isNonProdBuildEnvironment(); if ( - cmdOptions.checkBuildEnvironment === "enable" && - isNonProdBuildEnvironment() && - deploymentSelection.kind === "existingDeployment" && - deploymentSelection.deploymentToActOn.source === "deployKey" && - deploymentSelection.deploymentToActOn.deploymentFields?.deploymentType === - "prod" - ) { + cmdOptions.checkBuildEnvironment === "enable" && nonProdReason &&deploymentSelection.kind === "existingDeployment" && deploymentSelection.deploymentToActOn.source === "deployKey" && deploymentSelection.deploymentToActOn.deploymentFields?.deploymentType === "prod" + ){ await ctx.crash({ exitCode: 1, errorType: "invalid filesystem data", - printedMessage: `Detected a non-production build environment and "${CONVEX_DEPLOY_KEY_ENV_VAR_NAME}" for a production Convex deployment.\n - This is probably unintentional. - `, + printedMessage: `${nonProdReason}\n\nDetected a non-production build environment and "${CONVEX_DEPLOY_KEY_ENV_VAR_NAME}" for a production Convex deployment.\nThis is probably unintentional.` }); } diff --git a/npm-packages/convex/src/cli/lib/envvars.ts b/npm-packages/convex/src/cli/lib/envvars.ts index 2eac3e2bd..c44501e7e 100644 --- a/npm-packages/convex/src/cli/lib/envvars.ts +++ b/npm-packages/convex/src/cli/lib/envvars.ts @@ -340,14 +340,21 @@ export function gitBranchFromEnvironment(): string | null { return null; } -export function isNonProdBuildEnvironment(): boolean { +// ------------------------------------------ +// Improved: Return detailed reason for non-production builds +// ------------------------------------------ +export function isNonProdBuildEnvironment(): string | null { if (process.env.VERCEL) { // https://vercel.com/docs/projects/environment-variables/system-environment-variables - return process.env.VERCEL_ENV !== "production"; + if (process.env.VERCEL_ENV !== "production") { + return `Detected VERCEL because the VERCEL env var is set, but VERCEL_ENV is "${process.env.VERCEL_ENV}" instead of "production".`; + } } if (process.env.NETLIFY) { // https://docs.netlify.com/configure-builds/environment-variables/ - return process.env.CONTEXT !== "production"; + if (process.env.CONTEXT !== "production") { + return `Detected NETLIFY because the NETLIFY env var is set, but CONTEXT is "${process.env.CONTEXT}" instead of "production".`; + } } - return false; + return null; // everything looks production-safe } From 91052ef2ee3ef763e6c006b4fe02201828b10334 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Mon, 3 Nov 2025 11:17:38 +0530 Subject: [PATCH 2/2] chore: fix prettier and build issues --- npm-packages/convex/src/cli/deploy.ts | 13 +++++++++---- npm-packages/convex/src/cli/lib/envvars.ts | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/npm-packages/convex/src/cli/deploy.ts b/npm-packages/convex/src/cli/deploy.ts index 7c3472c72..5a26bd4c4 100644 --- a/npm-packages/convex/src/cli/deploy.ts +++ b/npm-packages/convex/src/cli/deploy.ts @@ -80,15 +80,20 @@ Same format as .env.local or .env files, and overrides them.`, const ctx = await oneoffContext(cmdOptions); const deploymentSelection = await getDeploymentSelection(ctx, cmdOptions); - + const nonProdReason = isNonProdBuildEnvironment(); if ( - cmdOptions.checkBuildEnvironment === "enable" && nonProdReason &&deploymentSelection.kind === "existingDeployment" && deploymentSelection.deploymentToActOn.source === "deployKey" && deploymentSelection.deploymentToActOn.deploymentFields?.deploymentType === "prod" - ){ + cmdOptions.checkBuildEnvironment === "enable" && + nonProdReason && + deploymentSelection.kind === "existingDeployment" && + deploymentSelection.deploymentToActOn.source === "deployKey" && + deploymentSelection.deploymentToActOn.deploymentFields?.deploymentType === + "prod" + ) { await ctx.crash({ exitCode: 1, errorType: "invalid filesystem data", - printedMessage: `${nonProdReason}\n\nDetected a non-production build environment and "${CONVEX_DEPLOY_KEY_ENV_VAR_NAME}" for a production Convex deployment.\nThis is probably unintentional.` + printedMessage: `${nonProdReason}\n\nDetected a non-production build environment and "${CONVEX_DEPLOY_KEY_ENV_VAR_NAME}" for a production Convex deployment.\nThis is probably unintentional.`, }); } diff --git a/npm-packages/convex/src/cli/lib/envvars.ts b/npm-packages/convex/src/cli/lib/envvars.ts index c44501e7e..7f922907f 100644 --- a/npm-packages/convex/src/cli/lib/envvars.ts +++ b/npm-packages/convex/src/cli/lib/envvars.ts @@ -340,9 +340,9 @@ export function gitBranchFromEnvironment(): string | null { return null; } -// ------------------------------------------ +// ------------------------------------------------------------- // Improved: Return detailed reason for non-production builds -// ------------------------------------------ +// ------------------------------------------------------------- export function isNonProdBuildEnvironment(): string | null { if (process.env.VERCEL) { // https://vercel.com/docs/projects/environment-variables/system-environment-variables