From ba2cb969120d6c9c5263bab7625944efe4eaed0b Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:26:40 +0100 Subject: [PATCH 1/4] fix(deploy): use production context when deploying with --prod `netlify deploy --prod` and `--prod-if-unlocked` resolved the CLI config and deployed function env vars with the `dev` context, so `[context.production]` overrides were ignored (e.g. `--no-build --prod` deployed the base publish directory). An explicit `--context` or the `CONTEXT` env var still take precedence. Fixes #5875 Co-Authored-By: Claude Opus 5.5 --- docs/commands/deploy.md | 2 +- eslint-suppressions.json | 2 +- src/commands/base-command.ts | 26 ++++++++++++++--- src/commands/deploy/deploy.ts | 2 +- src/commands/deploy/index.ts | 2 +- tests/unit/commands/deploy/context.test.ts | 34 ++++++++++++++++++++++ 6 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 tests/unit/commands/deploy/context.test.ts diff --git a/docs/commands/deploy.md b/docs/commands/deploy.md index 573bdadc8f0..4618a19e4a6 100644 --- a/docs/commands/deploy.md +++ b/docs/commands/deploy.md @@ -30,7 +30,7 @@ netlify deploy - `alias` (*string*) - Specifies the alias for deployment, the string at the beginning of the deploy subdomain. Useful for creating predictable deployment URLs. Avoid setting an alias string to the same value as a deployed branch. `alias` doesn’t create a branch deploy and can’t be used in conjunction with the branch subdomain feature. Maximum 37 characters. - `allow-anonymous` (*boolean*) - If not logged in, deploy anonymously and create a claimable site instead of requiring authentication -- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: dev) +- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: production with --prod or --prod-if-unlocked, otherwise dev) - `created-via` (*string*) - Specify the source of the deploy (e.g., "cli", "drop") - `dir` (*string*) - Specify a folder to deploy - `env` (*string*) - Set an environment variable for this deploy only. Applies to deployed functions only. Can be specified multiple times. diff --git a/eslint-suppressions.json b/eslint-suppressions.json index 73ba6024678..a4f2f79293f 100644 --- a/eslint-suppressions.json +++ b/eslint-suppressions.json @@ -45,7 +45,7 @@ "count": 7 }, "@typescript-eslint/no-unsafe-assignment": { - "count": 13 + "count": 12 }, "@typescript-eslint/no-unsafe-call": { "count": 2 diff --git a/src/commands/base-command.ts b/src/commands/base-command.ts index c2294613d13..89b364442ee 100644 --- a/src/commands/base-command.ts +++ b/src/commands/base-command.ts @@ -48,6 +48,7 @@ import { isInteractive } from '../utils/scripted-commands.js' import { identify, reportError, setCommandForErrorReporting, track } from '../utils/telemetry/index.js' import type { NetlifyOptions } from './types.js' import type { CachedConfig } from '../lib/build.js' +import type { DeployOptionValues } from './deploy/option_values.js' import type { MinimalAccount } from '../utils/types.js' type Analytics = { @@ -867,7 +868,7 @@ export default class BaseCommand extends Command { packagePath: packagePath, repositoryRoot: repositoryRoot, cwd: cwd, - context: flags.context || process.env.CONTEXT || this.getDefaultContext(), + context: this.getContext(), debug: flags.debug, siteId: this.siteId, token: token, @@ -912,13 +913,30 @@ export default class BaseCommand extends Command { return join(this.workspacePackage || '', '.netlify', ...paths) } + /** + * Returns the context config and env vars are resolved with: `--context`, + * then the `CONTEXT` env var, then the command's default context. + */ + getContext(): string { + return this.opts<{ context?: string }>().context || process.env.CONTEXT || this.getDefaultContext() + } + /** * Returns the context that should be used in case one hasn't been explicitly - * set. The default context is `dev` most of the time, but some commands may - * wish to override that. + * set. The default context is `dev` most of the time, but `serve` and + * production deploys use `production`. */ getDefaultContext(): 'production' | 'dev' { - return this.name() === 'serve' ? 'production' : 'dev' + if (this.name() === 'serve') { + return 'production' + } + if (this.name() === 'deploy') { + const { prod, prodIfUnlocked } = this.opts>>() + if (prod || prodIfUnlocked) { + return 'production' + } + } + return 'dev' } /** diff --git a/src/commands/deploy/deploy.ts b/src/commands/deploy/deploy.ts index 3b1973f38f8..8b5b8884f4a 100644 --- a/src/commands/deploy/deploy.ts +++ b/src/commands/deploy/deploy.ts @@ -1042,7 +1042,7 @@ const prepAndRunDeploy = async ({ const siteEnv = await getEnvelopeEnv({ api, - context: options.context, + context: command.netlify.cachedConfig.context, env: command.netlify.cachedConfig.env, raw: true, scope: 'functions', diff --git a/src/commands/deploy/index.ts b/src/commands/deploy/index.ts index f55fdb05da1..247191138f4 100644 --- a/src/commands/deploy/index.ts +++ b/src/commands/deploy/index.ts @@ -91,7 +91,7 @@ For detailed configuration options, see the Netlify documentation.`, ) .option( '--context ', - 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: dev)', + 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: production with --prod or --prod-if-unlocked, otherwise dev)', normalizeContext, ) .option( diff --git a/tests/unit/commands/deploy/context.test.ts b/tests/unit/commands/deploy/context.test.ts new file mode 100644 index 00000000000..485756ec3b5 --- /dev/null +++ b/tests/unit/commands/deploy/context.test.ts @@ -0,0 +1,34 @@ +import { afterEach, describe, expect, test, vi } from 'vitest' + +import BaseCommand from '../../../../src/commands/base-command.js' +import { createDeployCommand } from '../../../../src/commands/deploy/index.js' + +const parseDeploy = (args: string[]) => { + const deploy = createDeployCommand(new BaseCommand('netlify')) + deploy.parseOptions(args) + return deploy +} + +describe('deploy context', () => { + afterEach(() => { + vi.unstubAllEnvs() + }) + + test.each([ + { args: [], expected: 'dev' }, + { args: ['--draft'], expected: 'dev' }, + { args: ['--prod'], expected: 'production' }, + { args: ['--prod-if-unlocked'], expected: 'production' }, + { args: ['--prod', '--context', 'deploy-preview'], expected: 'deploy-preview' }, + ])('$args resolves to $expected', ({ args, expected }) => { + vi.stubEnv('CONTEXT', '') + + expect(parseDeploy(args).getContext()).toBe(expected) + }) + + test('the CONTEXT env var wins over --prod', () => { + vi.stubEnv('CONTEXT', 'branch-deploy') + + expect(parseDeploy(['--prod']).getContext()).toBe('branch-deploy') + }) +}) From 7f210a01f8c4c2c9ed8782851945d6f98010bdf5 Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:31:46 +0100 Subject: [PATCH 2/4] test(deploy): cover deploy context resolution and clarify --context default Co-Authored-By: Claude Opus 5.5 --- docs/commands/deploy.md | 2 +- src/commands/base-command.ts | 3 +- src/commands/deploy/index.ts | 2 +- .../commands/deploy/deploy.test.ts | 30 +++++++++++++++++++ tests/unit/commands/deploy/context.test.ts | 7 +++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/docs/commands/deploy.md b/docs/commands/deploy.md index 4618a19e4a6..0d30a2fc9a5 100644 --- a/docs/commands/deploy.md +++ b/docs/commands/deploy.md @@ -30,7 +30,7 @@ netlify deploy - `alias` (*string*) - Specifies the alias for deployment, the string at the beginning of the deploy subdomain. Useful for creating predictable deployment URLs. Avoid setting an alias string to the same value as a deployed branch. `alias` doesn’t create a branch deploy and can’t be used in conjunction with the branch subdomain feature. Maximum 37 characters. - `allow-anonymous` (*boolean*) - If not logged in, deploy anonymously and create a claimable site instead of requiring authentication -- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: production with --prod or --prod-if-unlocked, otherwise dev) +- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT, otherwise production; draft deploys resolve project config and function env vars with dev) - `created-via` (*string*) - Specify the source of the deploy (e.g., "cli", "drop") - `dir` (*string*) - Specify a folder to deploy - `env` (*string*) - Set an environment variable for this deploy only. Applies to deployed functions only. Can be specified multiple times. diff --git a/src/commands/base-command.ts b/src/commands/base-command.ts index 89b364442ee..f5529345483 100644 --- a/src/commands/base-command.ts +++ b/src/commands/base-command.ts @@ -48,7 +48,6 @@ import { isInteractive } from '../utils/scripted-commands.js' import { identify, reportError, setCommandForErrorReporting, track } from '../utils/telemetry/index.js' import type { NetlifyOptions } from './types.js' import type { CachedConfig } from '../lib/build.js' -import type { DeployOptionValues } from './deploy/option_values.js' import type { MinimalAccount } from '../utils/types.js' type Analytics = { @@ -931,7 +930,7 @@ export default class BaseCommand extends Command { return 'production' } if (this.name() === 'deploy') { - const { prod, prodIfUnlocked } = this.opts>>() + const { prod, prodIfUnlocked } = this.opts<{ prod?: boolean; prodIfUnlocked?: boolean }>() if (prod || prodIfUnlocked) { return 'production' } diff --git a/src/commands/deploy/index.ts b/src/commands/deploy/index.ts index 247191138f4..f4d2aabc8f6 100644 --- a/src/commands/deploy/index.ts +++ b/src/commands/deploy/index.ts @@ -91,7 +91,7 @@ For detailed configuration options, see the Netlify documentation.`, ) .option( '--context ', - 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: production with --prod or --prod-if-unlocked, otherwise dev)', + 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT, otherwise production; draft deploys resolve project config and function env vars with dev)', normalizeContext, ) .option( diff --git a/tests/integration/commands/deploy/deploy.test.ts b/tests/integration/commands/deploy/deploy.test.ts index 8fc0905ee1b..b9fdf081b7e 100644 --- a/tests/integration/commands/deploy/deploy.test.ts +++ b/tests/integration/commands/deploy/deploy.test.ts @@ -133,6 +133,36 @@ describe.concurrent('deploy command', () => { }) }) + test.for([ + { args: ['--prod'], published: 'production.html', skipped: 'draft.html' }, + { args: [], published: 'draft.html', skipped: 'production.html' }, + ])('should resolve config with the deploy context ($args)', async ({ args, published, skipped }, t) => { + await withMockDeploy(async (mockApi, deployState) => { + await withSiteBuilder(t, async (builder) => { + builder + .withContentFile({ path: 'public/draft.html', content: 'draft' }) + .withContentFile({ path: 'public-production/production.html', content: 'production' }) + .withNetlifyToml({ + config: { + build: { publish: 'public' }, + context: { production: { publish: 'public-production' } }, + }, + }) + + await builder.build() + + await callCli( + ['deploy', '--json', '--no-build', ...args], + getCLIOptions({ apiUrl: mockApi.apiUrl, builder, env: { CONTEXT: '' } }), + ) + + const files = Object.keys(deployState.getDeployBody()?.files ?? {}) + expect(files).toContain(published) + expect(files).not.toContain(skipped) + }) + }) + }) + test('should deploy Edge Functions when directory exists after running a build', async (t) => { await withMockDeploy(async (mockApi, deployState) => { await withSiteBuilder(t, async (builder) => { diff --git a/tests/unit/commands/deploy/context.test.ts b/tests/unit/commands/deploy/context.test.ts index 485756ec3b5..76f7bee2f9a 100644 --- a/tests/unit/commands/deploy/context.test.ts +++ b/tests/unit/commands/deploy/context.test.ts @@ -20,6 +20,7 @@ describe('deploy context', () => { { args: ['--prod'], expected: 'production' }, { args: ['--prod-if-unlocked'], expected: 'production' }, { args: ['--prod', '--context', 'deploy-preview'], expected: 'deploy-preview' }, + { args: ['--prod', '--context', 'dev'], expected: 'dev' }, ])('$args resolves to $expected', ({ args, expected }) => { vi.stubEnv('CONTEXT', '') @@ -31,4 +32,10 @@ describe('deploy context', () => { expect(parseDeploy(['--prod']).getContext()).toBe('branch-deploy') }) + + test('--context wins over the CONTEXT env var', () => { + vi.stubEnv('CONTEXT', 'branch-deploy') + + expect(parseDeploy(['--prod', '--context', 'deploy-preview']).getContext()).toBe('deploy-preview') + }) }) From 275c59ddabcccdb0bdcdf2e262ec4a328b439613 Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:36:52 +0100 Subject: [PATCH 3/4] docs(deploy): describe --context default per flag Co-Authored-By: Claude Opus 5.5 --- docs/commands/deploy.md | 2 +- src/commands/base-command.ts | 4 ---- src/commands/deploy/index.ts | 2 +- tests/unit/commands/deploy/context.test.ts | 4 ++-- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/docs/commands/deploy.md b/docs/commands/deploy.md index 0d30a2fc9a5..d2545bf71d3 100644 --- a/docs/commands/deploy.md +++ b/docs/commands/deploy.md @@ -30,7 +30,7 @@ netlify deploy - `alias` (*string*) - Specifies the alias for deployment, the string at the beginning of the deploy subdomain. Useful for creating predictable deployment URLs. Avoid setting an alias string to the same value as a deployed branch. `alias` doesn’t create a branch deploy and can’t be used in conjunction with the branch subdomain feature. Maximum 37 characters. - `allow-anonymous` (*boolean*) - If not logged in, deploy anonymously and create a claimable site instead of requiring authentication -- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT, otherwise production; draft deploys resolve project config and function env vars with dev) +- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT if set; otherwise production, but project config and function env vars use dev without --prod or --prod-if-unlocked) - `created-via` (*string*) - Specify the source of the deploy (e.g., "cli", "drop") - `dir` (*string*) - Specify a folder to deploy - `env` (*string*) - Set an environment variable for this deploy only. Applies to deployed functions only. Can be specified multiple times. diff --git a/src/commands/base-command.ts b/src/commands/base-command.ts index f5529345483..e1385e6f0c7 100644 --- a/src/commands/base-command.ts +++ b/src/commands/base-command.ts @@ -912,10 +912,6 @@ export default class BaseCommand extends Command { return join(this.workspacePackage || '', '.netlify', ...paths) } - /** - * Returns the context config and env vars are resolved with: `--context`, - * then the `CONTEXT` env var, then the command's default context. - */ getContext(): string { return this.opts<{ context?: string }>().context || process.env.CONTEXT || this.getDefaultContext() } diff --git a/src/commands/deploy/index.ts b/src/commands/deploy/index.ts index f4d2aabc8f6..b2c8e99cca5 100644 --- a/src/commands/deploy/index.ts +++ b/src/commands/deploy/index.ts @@ -91,7 +91,7 @@ For detailed configuration options, see the Netlify documentation.`, ) .option( '--context ', - 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT, otherwise production; draft deploys resolve project config and function env vars with dev)', + 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT if set; otherwise production, but project config and function env vars use dev without --prod or --prod-if-unlocked)', normalizeContext, ) .option( diff --git a/tests/unit/commands/deploy/context.test.ts b/tests/unit/commands/deploy/context.test.ts index 76f7bee2f9a..8e5df501256 100644 --- a/tests/unit/commands/deploy/context.test.ts +++ b/tests/unit/commands/deploy/context.test.ts @@ -27,10 +27,10 @@ describe('deploy context', () => { expect(parseDeploy(args).getContext()).toBe(expected) }) - test('the CONTEXT env var wins over --prod', () => { + test.each([{ args: [] }, { args: ['--prod'] }])('the CONTEXT env var wins over the default for $args', ({ args }) => { vi.stubEnv('CONTEXT', 'branch-deploy') - expect(parseDeploy(['--prod']).getContext()).toBe('branch-deploy') + expect(parseDeploy(args).getContext()).toBe('branch-deploy') }) test('--context wins over the CONTEXT env var', () => { From 641cbe5b725beca0f09891f974aae9ad5bb2bfe9 Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:39:43 +0100 Subject: [PATCH 4/4] docs(deploy): describe the --context default the build uses Co-Authored-By: Claude Opus 5.5 --- docs/commands/deploy.md | 2 +- src/commands/base-command.ts | 4 ++-- src/commands/deploy/index.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/commands/deploy.md b/docs/commands/deploy.md index d2545bf71d3..a1aaddb5d02 100644 --- a/docs/commands/deploy.md +++ b/docs/commands/deploy.md @@ -30,7 +30,7 @@ netlify deploy - `alias` (*string*) - Specifies the alias for deployment, the string at the beginning of the deploy subdomain. Useful for creating predictable deployment URLs. Avoid setting an alias string to the same value as a deployed branch. `alias` doesn’t create a branch deploy and can’t be used in conjunction with the branch subdomain feature. Maximum 37 characters. - `allow-anonymous` (*boolean*) - If not logged in, deploy anonymously and create a claimable site instead of requiring authentication -- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT if set; otherwise production, but project config and function env vars use dev without --prod or --prod-if-unlocked) +- `context` (*string*) - Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT or "production") - `created-via` (*string*) - Specify the source of the deploy (e.g., "cli", "drop") - `dir` (*string*) - Specify a folder to deploy - `env` (*string*) - Set an environment variable for this deploy only. Applies to deployed functions only. Can be specified multiple times. diff --git a/src/commands/base-command.ts b/src/commands/base-command.ts index e1385e6f0c7..09c9c368526 100644 --- a/src/commands/base-command.ts +++ b/src/commands/base-command.ts @@ -918,8 +918,8 @@ export default class BaseCommand extends Command { /** * Returns the context that should be used in case one hasn't been explicitly - * set. The default context is `dev` most of the time, but `serve` and - * production deploys use `production`. + * set. The default context is `dev` most of the time, but some commands may + * wish to override that. */ getDefaultContext(): 'production' | 'dev' { if (this.name() === 'serve') { diff --git a/src/commands/deploy/index.ts b/src/commands/deploy/index.ts index b2c8e99cca5..e41e30291f9 100644 --- a/src/commands/deploy/index.ts +++ b/src/commands/deploy/index.ts @@ -91,7 +91,7 @@ For detailed configuration options, see the Netlify documentation.`, ) .option( '--context ', - 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT if set; otherwise production, but project config and function env vars use dev without --prod or --prod-if-unlocked)', + 'Specify a deploy context for environment variables read during the build ("production", "deploy-preview", "branch-deploy", "dev") or `branch:your-branch` where `your-branch` is the name of a branch (default: value of CONTEXT or "production")', normalizeContext, ) .option(