Skip to content

Comments

warn about conflicting provider env var#1825

Closed
jordanstephens wants to merge 1 commit intomainfrom
jordan/conflicting-provider-env-warning
Closed

warn about conflicting provider env var#1825
jordanstephens wants to merge 1 commit intomainfrom
jordan/conflicting-provider-env-warning

Conversation

@jordanstephens
Copy link
Member

@jordanstephens jordanstephens commented Jan 20, 2026

Description

Lio reported:

I set DEFANG_PROVIDER to gcp and do defang ls . I pick stack beta (only choice), I get warning that DEFANG_PROVIDER is set in two places but env gets used (which is gcp) but then it proceeds to show aws stuff
For example:

$ DEFANG_PROVIDER=gcp defang ls
This project was deployed with an implicit Stack called ‘beta’ before Stacks were introduced.
   To update your existing deployment, select the ‘beta’ Stack.
Creating a new Stack will result in a separate deployment instance.
   To learn more about Stacks, visit: https://docs.defang.io/docs/concepts/stacks
To skip this prompt, run this command with --stack=<stack_name>
Select a stack
? stack beta (deployed Jan 16 2026)
! The variable “DEFANG_PROVIDER” is set in both the stack and the environment. The value from the environment will be used.
 * Using the “beta” stack on aws from interactive selection
# ...

So this warning is wrong. We actually ignore the environment variable in this case. DEFANG_PROVIDER is the only stack variable which can not be overridden by the environment, so this PR updates this warning logic to handleDEFANG_PROVIDER specifically.

Linked Issues

Checklist

  • I have performed a self-review of my code
  • I have added appropriate tests
  • I have updated the Defang CLI docs and/or README to reflect my changes, if necessary

Summary by CodeRabbit

  • Bug Fixes
    • Improved warning messages when stack configuration values conflict with environment settings. Messages now provide clearer guidance about which value takes precedence, with specialized messaging for certain configuration variables.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 20, 2026

📝 Walkthrough

Walkthrough

The PR refines the warning message logic in LoadStackEnv when stack and environment variables conflict during loading with overload disabled, introducing specialized messaging for the DEFANG_PROVIDER variable versus generic messaging for other variables.

Changes

Cohort / File(s) Change Summary
LoadStackEnv conflict messaging
src/pkg/stacks/stacks.go
Specialized warning messages for environment-stack variable conflicts when overload is false; DEFANG_PROVIDER receives distinct messaging, other variables use generic conflict text

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • lionello

Poem

🐰 A provider's voice grew clear and true,
With special words when conflicts brew,
DEFANG echoes, distinct and bright,
While others share the warning light,
Stack wins the day, environment yields,
Harmony restored in config fields! 🌟

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly summarizes the main change: adding a warning about the conflicting DEFANG_PROVIDER environment variable when it conflicts with stack values.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.5.0)

level=warning msg="[linters_context] running gomodguard failed: unable to read module file go.mod: current working directory must have a go.mod file: if you are not using go modules it is suggested to disable this linter"
level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/pkg/stacks/stacks.go`:
- Around line 229-237: The warning for DEFANG_PROVIDER contradicts behavior
because when overload is false we currently warn that the stack value will be
used but do not call os.Setenv; change the logic so DEFANG_PROVIDER is always
written into the environment regardless of overload. Specifically, in the block
that checks currentEnv[key], ensure that if key == "DEFANG_PROVIDER" you call
os.Setenv(key, value) (or treat it as if (_, ok := currentEnv[key]; !ok ||
overload) is true) so the stack value overrides the existing env; adjust the
term.Warnf message if needed to reflect that DEFANG_PROVIDER is being forced,
and keep references to currentEnv, overload, os.Setenv, term.Warnf, and the
"DEFANG_PROVIDER" constant to locate the code.

Comment on lines 229 to 237
if envValue, ok := currentEnv[key]; ok && envValue != value && !overload {
term.Warnf("The variable %q is set in both the stack and the environment. The value from the environment will be used.\n", key)
if key == "DEFANG_PROVIDER" {
term.Warnf("The variable DEFANG_PROVIDER is set to %q in the environment, but the stack specifies %q. The value from the stack will be used.\n", envValue, value)
} else {
term.Warnf("The variable %q is set to %q in the environment, but the stack specifies %q. The value from the environment will be used.\n", key, envValue, value)
}
}
if _, ok := currentEnv[key]; !ok || overload {
err := os.Setenv(key, value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Warning contradicts behavior for DEFANG_PROVIDER; env is still left untouched.

When overload is false and DEFANG_PROVIDER is already set, the warning says the stack value will be used, but the code path does not call os.Setenv, so downstream code reading os.Getenv("DEFANG_PROVIDER") will still see the environment value. This is exactly the mismatch the PR aims to fix. Consider forcing an override for DEFANG_PROVIDER even when overload is false.

🔧 Proposed fix
-		if _, ok := currentEnv[key]; !ok || overload {
+		shouldOverride := overload || key == "DEFANG_PROVIDER"
+		if _, ok := currentEnv[key]; !ok || shouldOverride {
 			err := os.Setenv(key, value)
 			if err != nil {
 				return fmt.Errorf("could not set env var %q: %w", key, err)
 			}
 		}
🤖 Prompt for AI Agents
In `@src/pkg/stacks/stacks.go` around lines 229 - 237, The warning for
DEFANG_PROVIDER contradicts behavior because when overload is false we currently
warn that the stack value will be used but do not call os.Setenv; change the
logic so DEFANG_PROVIDER is always written into the environment regardless of
overload. Specifically, in the block that checks currentEnv[key], ensure that if
key == "DEFANG_PROVIDER" you call os.Setenv(key, value) (or treat it as if (_,
ok := currentEnv[key]; !ok || overload) is true) so the stack value overrides
the existing env; adjust the term.Warnf message if needed to reflect that
DEFANG_PROVIDER is being forced, and keep references to currentEnv, overload,
os.Setenv, term.Warnf, and the "DEFANG_PROVIDER" constant to locate the code.

@jordanstephens
Copy link
Member Author

After thinking about this more, I'm closing this PR and will instead fix this in #1820. See cd9a96f (#1820)

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.

Stacks warning: using env var when it's not using it

1 participant