Skip to content

fix(deploy): use production context when deploying with --prod - #11372

Open
RealDoc06 wants to merge 4 commits into
netlify:mainfrom
RealDoc06:fix/deploy-prod-build-context
Open

RealDoc06 wants to merge 4 commits into
netlify:mainfrom
RealDoc06:fix/deploy-prod-build-context

Conversation

@RealDoc06

Copy link
Copy Markdown

Summary

Fixes #5875

netlify deploy --prod ignored [context.production] settings because the CLI resolved its config with the dev context. Production deploys now default to production, while --context and CONTEXT still win.

Problem

netlify deploy --prod (and --prod-if-unlocked) resolved the CLI's config with the dev context. As a result, [context.production] overrides in netlify.toml were ignored wherever the CLI used its own config. For example, with this config:

[build]
publish = "public"

[context.production]
publish = "public-production"

netlify deploy --no-build --prod deployed public, not public-production.

Root cause:

  • BaseCommand.getConfig resolves config with --context || CONTEXT || getDefaultContext() (src/commands/base-command.ts). getDefaultContext() returned production only for serve, so --prod never affected it. That config is resolved in the preAction hook, before the deploy action runs.
  • This config drives --no-build deploys (publish and functions dirs, edge functions, headers and redirects), framework detection and cachedConfig.env.
  • The deployed functions config looked up its env with options.context (src/commands/deploy/deploy.ts). That falls back to dev in getEnvelopeEnv unless --context is passed, so AWS_LAMBDA_JS_RUNTIME came from dev.

The build step from the original report (--build --prod logging Context: dev) no longer reproduces on main. Deploy always passes defaultConfig to @netlify/build, so @netlify/config re-resolves config with its own default of CONTEXT || 'production' (@netlify/config/lib/options/main.js). The CLI-side resolution is what was still wrong.

Fix

  • getDefaultContext() returns production for deploy when --prod or --prod-if-unlocked is set. The precedence moves into BaseCommand.getContext(): --context, then CONTEXT, then the default. An explicit --context or the CONTEXT env var still wins.
  • The deployed functions env lookup uses command.netlify.cachedConfig.context, the same context the config was resolved with.
  • The --context help text and docs/commands/deploy.md said the default was dev, which was never true for the build step. Since --context only works with --build, the text now describes the build default, matching netlify build: value of CONTEXT or "production".
  • eslint-suppressions.json drops one no-unsafe-assignment, because the inline flags.context read (any) is now typed.

--prod-if-unlocked

It defaults to production, whether or not the site is locked. Config is resolved before the site and its lock state are known, and the lock only decides whether the deploy is published, not what gets built. This matches Netlify itself: a locked production branch still builds in the production context, it just isn't auto-published. So a locked --prod-if-unlocked deploy uploads the production artifact as a draft.

Behavior notes

  • Draft deploys without CONTEXT are unchanged. The CLI-side config and functions env lookup still use dev, and the build still uses production through @netlify/build. That inconsistency is older than this PR and left out of scope, because changing draft builds would change the env vars existing users get.
  • A draft deploy with CONTEXT set now uses that context for the functions env lookup (previously dev). This matches the config the CLI already resolved from CONTEXT.
  • --remote and --trigger don't read the CLI's resolved context, so they're unaffected.

Testing

  • tests/unit/commands/deploy/context.test.ts parses the real deploy command and checks precedence:
    • draft and --draft resolve to dev
    • --prod and --prod-if-unlocked resolve to production
    • --context wins over --prod
    • CONTEXT wins over the default
    • --context wins over CONTEXT
  • A mock-API integration test in tests/integration/commands/deploy/deploy.test.ts: --no-build --prod publishes [context.production].publish, and a draft publishes the base publish. It fails against main's base-command.ts and needs no Netlify token.
  • A throwaway mock-API repro confirmed the build context and env values for draft, --prod, --prod-if-unlocked and --prod --context deploy-preview, before and after the change.
  • npm run typecheck, npm run lint and npm run format:check pass. The deploy unit tests pass (35), and so do all deploy integration tests under --retry=3 (90). Integration tests that need a real Netlify token were not run.
  • tests/unit/lib/completion/generate-autocompletion.test.ts fails on main under Node 26 (option ordering in the snapshot). This PR doesn't cause it.

For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures we
    can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or
    something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures your code follows our style guide and
    passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

Council review

Rounds: 4

Delegate Round 1 Round 2 Round 3 Round 4
Claude Opus 5.5 9/10 9/10 9.5/10 10/10
Muse Wonder 1.0 8/10 9/10 9/10 9.5/10
GPT-6.2 Sol 8/10 9/10 9/10 10/10
GPT-6.2 Astra 8/10 9/10 9/10 10/10

Created with Claude Opus 5.5 in Claude Code (T3 Code).

🤖 Generated with Claude Code

RealDoc06 and others added 4 commits November 18, 2026 20:26
`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 netlify#5875

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…efault

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RealDoc06
RealDoc06 requested review from a team as code owners November 18, 2026 19:58
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Deployments using --prod or --prod-if-unlocked now resolve the production context for configuration and scoped environment variables.
    • Production deployments now use files from the production context’s publish directory, while deployments without --prod continue using the default publish directory.
  • Documentation
    • Updated the deploy command documentation to describe how the --context default relates to the CONTEXT environment variable.

Deploy context resolution now prioritizes the --context option, then CONTEXT, then the command default. Deploys with --prod or --prod-if-unlocked default to production; other deploys default to dev. The deploy command uses the resolved context for functions environment lookup. Unit and integration tests cover context selection and uploaded files.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: serhalp, ndhoule

Merge Risk: Minimal · up to 641cb

Production deploys now resolve configuration and functions environment settings from the production context by default. Draft deploys keep their existing behavior. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: deploy with --prod now uses the production context. It matches the summary, the code changes to base-command.ts and deploy.ts, and the stated objectives. The fix(deplo…
Description check Passed The description states the problem (netlify deploy --prod ignored [context.production] because the CLI resolved config with the dev context), the fix, the affected files, the --prod-if-unlocked behavi…
Linked Issues check Passed Linked issue #5875 asks that netlify deploy --prod use the production context instead of dev. The reviewed diff implements this for the CLI-side config. In src/commands/base-command.ts, `getDefa…
Out of Scope Changes check Passed All changed files relate to the issue. base-command.ts and deploy.ts contain the core fix. deploy/index.ts and docs/commands/deploy.md update the --context default description to match the n…
Full details: Title check

Explanation

The title names the main change: deploy with --prod now uses the production context. It matches the summary, the code changes to base-command.ts and deploy.ts, and the stated objectives. The fix(deploy) prefix is conventional and the title is specific.

Full details: Description check

Explanation

The description states the problem (netlify deploy --prod ignored [context.production] because the CLI resolved config with the dev context), the fix, the affected files, the --prod-if-unlocked behavior, and the tests. It matches the changeset closely.

Full details: Linked Issues check

Explanation

Linked issue #5875 asks that netlify deploy --prod use the production context instead of dev. The reviewed diff implements this for the CLI-side config. In src/commands/base-command.ts, getDefaultContext() now returns production for deploy when prod or prodIfUnlocked is set. getContext() applies the order --context, then CONTEXT, then the default, and getConfig uses it. In src/commands/deploy/deploy.ts, the scoped functions env lookup now uses command.netlify.cachedConfig.context. The issue's --build --prod symptom is handled by the build step. Deploy passes defaultConfig to @netlify/build, which resolves CONTEXT || 'production' on its own, as stated in the PR. I did not verify that dependency code. The PR also adds unit tests for precedence (tests/unit/commands/deploy/context.test.ts) and an integration test for production versus base publish directories. The issue asks for a code change, and the diff provides it. The help text and docs changes match the new behavior. No unmet coding requirement is found.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the issue. base-command.ts and deploy.ts contain the core fix. deploy/index.ts and docs/commands/deploy.md update the --context default description to match the new behavior. eslint-suppressions.json drops one suppression because the flags.context read is now typed by getContext(). The two test files cover the new precedence rules and the publish-directory behavior. No change was found that is unrelated to #5875.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Linked repositories: Some configured repositories could not be included in this review. Cross-repository analysis may be incomplete. Please check linked-repository access and settings.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bd10e731-62e4-4905-ab05-12d82436b4a1
Commits

Reviewing files that changed from the base of the PR and between c83fcb9 and 641cbe5.

Files selected for processing: 7

  • docs/commands/deploy.md
  • eslint-suppressions.json
  • src/commands/base-command.ts
  • src/commands/deploy/deploy.ts
  • src/commands/deploy/index.ts
  • tests/integration/commands/deploy/deploy.test.ts
  • tests/unit/commands/deploy/context.test.ts
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@RealDoc06

Copy link
Copy Markdown
Author

Note

🤖 Claude Opus 5.5 responding on behalf of RealDoc06

@netlify/pod-experience could someone approve the workflow runs? All checks are waiting on "action required" because this is a first-time contribution.

Locally, typecheck, lint, format, the deploy unit tests (35) and the deploy integration tests (90) pass. The one known failure, the generate-autocompletion snapshot under Node 26, also fails on main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

netlify deploy --build --prod runs with dev context

1 participant