[CFX-6414] fix(dotenv): make --if-needed skip only when .env is complete - #750
Conversation
|
🎫 Jira: |
|
/approve-smoke-tests |
|
🔐 Fork PR smoke tests triggered by @ajalon1 What happens next:
|
Previously, shouldSkipSetup used ValidateEnvironment which checked both .env file values and OS environment variables. This caused the wizard to be skipped when core variables (DATAROBOT_API_TOKEN, DATAROBOT_ENDPOINT) were set via environment but missing from .env, leaving .env incomplete. Change shouldSkipSetup to use ValidateEnvironmentFileOnly, which ignores OS environment variables for the skip decision. This ensures the wizard runs whenever .env itself is missing required variables. - Add ValidateEnvironmentFileOnly and GatherUserPromptsFromFile - Refactor promptsWithValues to accept includeEnv flag - Add regression test for env-var-only core variables Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
ac8d5d7 to
49a3a5b
Compare
|
🔐 Fork smoke tests started by maintainer ⏳ Security scans passed. Running smoke tests... Commit: |
|
✅ All smoke tests passed! (Fork PR) ✅ Security Scan: success |
Code OwnershipCli Maintainers
Review requested from the teams above. Labels will be removed automatically upon approval. |
There was a problem hiding this comment.
Pull request overview
Updates dotenv validation so --if-needed checks .env independently of OS environment variables.
Changes:
- Adds file-only validation and prompt gathering.
- Refactors prompt value resolution.
- Adds regression coverage for OS-only core variables.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
internal/envbuilder/validator.go |
Adds file-only validation and value-resolution modes. |
internal/envbuilder/validator_test.go |
Updates tests for the new resolver parameter. |
internal/envbuilder/builder.go |
Adds file-only prompt gathering. |
cmd/dotenv/if_needed_test.go |
Tests missing core variables supplied by OS environment. |
cmd/dotenv/cmd.go |
Uses file-only validation for skip decisions. |
Suppressed comments (1)
internal/envbuilder/validator.go:126
- The new file-only mode still depends on the OS environment in the Pulumi special case below. If
PULUMI_CONFIG_PASSPHRASEexists both in the OS and in drconfig, the lookup suppresses loading the Viper value;resolvePromptValuethen ignores the OS value, so validation fails solely because that environment variable is present. In file-only mode, allow the Viper fallback regardless ofos.LookupEnv.
func promptsWithValues(prompts []UserPrompt, variables Variables, includeEnv bool) []UserPrompt {
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
|
||
| // Gather all user prompts from the repository | ||
| userPrompts, err := GatherUserPrompts(repoRoot, variables) | ||
| userPrompts, err := gatherUserPrompts(repoRoot, variables, includeEnv) |
There was a problem hiding this comment.
Going to pull this back and work on this bit tomorrow.
Copilot review identified that in file-only mode, if PULUMI_CONFIG_PASSPHRASE was set in OS env but not in .env, validation would fail incorrectly. The first loop skipped viper load due to os.LookupEnv, but resolvePromptValue ignored the OS value. Fix: Extract resolvePulumiPassphrase helper that respects includeEnv flag. In file-only mode, always check viper config regardless of OS env. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
ValidateEnvironmentFileOnly still synthesized values: gatherUserPrompts applied YAML defaults and ApplyGeneratedValues, and Valid() accepted non-empty commented entries. A required variable satisfied only by a default, a generated secret, or a commented-out .env line could therefore make shouldSkipSetup return true, leaving .env incomplete and skipping the wizard. Add a synthesize flag to gatherUserPrompts/validateEnvironment/ promptsWithValues/resolvePromptValue. ValidateEnvironmentFileOnly now runs in presence-only mode (includeEnv=false, synthesize=false): - YAML defaults are not applied - ApplyGeneratedValues is skipped - commented .env entries count as absent Also remove the unused GatherUserPromptsFromFile public wrapper; the includeEnv distinction stays private to the package. The PULUMI_CONFIG_PASSPHRASE viper-config value remains an intentional non-file exception (resolvePulumiPassphrase) and is documented as such. Regression tests cover the three synthesis leaks (default, generated secret, commented entry) plus a positive control. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
370a933 to
9c3b77c
Compare
|
/approve-smoke-tests |
|
🔐 Fork PR smoke tests triggered by @ajalon1 What happens next:
|
|
🔐 Fork smoke tests started by maintainer ⏳ Security scans passed. Running smoke tests... Commit: |
|
✅ All smoke tests passed! (Fork PR) ✅ Security Scan: success |
RATIONALE
dr dotenv setup --if-neededwas skipping the wizard when.envexisted but was incomplete, leaving.envincomplete and causing downstream failures (e.g., deployment issues reported in CFX-6414).Two root causes, both in the skip check (
shouldSkipSetup):shouldSkipSetupusedValidateEnvironment, which checks both.envfile values andos.LookupEnv. Environment variables satisfied validation even when.envwas missing core variables, so the wizard never ran and.envwas never populated.gatherUserPromptsstill applied YAMLdefaultvalues, ranApplyGeneratedValues(auto-generating secrets), andValid()accepted non-empty commented entries. A required variable satisfied only by a default, a generated secret, or a commented-out.envline could make the skip check pass, leaving.envincomplete.CHANGES
ValidateEnvironmentFileOnlyto validate.envcontents only, ignoring OS environment variables.shouldSkipSetupto useValidateEnvironmentFileOnlyso--if-neededskips only when.envitself is complete.ValidateEnvironmentFileOnlypresence-only: it no longer applies YAML defaults, does not runApplyGeneratedValues, and treats commented-out.enventries as absent. A required variable now counts as present only if it has an effective, uncommented value in the.envfile.synthesizeflag throughgatherUserPrompts/validateEnvironment/promptsWithValues/resolvePromptValue.GatherUserPromptsandValidateEnvironmentkeepsynthesize=true(unchanged behavior);ValidateEnvironmentFileOnlyusessynthesize=false.GatherUserPromptsFromFilepublic wrapper; theincludeEnv/synthesizedistinction stays private to theenvbuilderpackage.promptsWithValuesto acceptincludeEnv/synthesizeflags and extractedresolvePromptValueto reduce cyclomatic complexity.PULUMI_CONFIG_PASSPHRASEviper-config value remains an intentional non-file exception (resolvePulumiPassphrase), documented as such.BEHAVIOR CHANGE NOTE
dr dotenv setup --if-needednow runs the wizard in three cases where it previously skipped (exit 0): a required var satisfied only by a YAMLdefault, a requiredgenerate: truesecret absent from.env, and a required var commented out in.env. This is a bug fix (the.envwas incomplete), but it is an observable change to a user-facing flag.For automation/CI that runs
dr dotenv setup --if-needednon-interactively, pair it with--yes(orDATAROBOT_CLI_NON_INTERACTIVE=true) so the wizard runs headless and writes the missing values, then exits 0. A script using--if-neededwithout--yesthat previously got a silent skip for one of these cases will now launch the wizard and block on a prompt.dr dotenv validateis unchanged — it still usesValidateEnvironment(synthesize=true), so a var satisfied only by a default still passesvalidate.TESTING
go test ./cmd/dotenv/... ./internal/envbuilder/... -count=1 -race— all pass, includingTestShouldSkipSetup_PresenceOnly(3 leak cases + positive control).task lint— clean across linux/darwin/windows.validateunchanged) — all pass.RELATED
.envgenuinely complete so [CFX-7263] Fail on bad env credentials instead of using the stored profile #751's "complete env pair = authoritative" rule fires on correct data.Note
Medium Risk
Changes when dotenv setup is skipped in automation paths; incorrect skip behavior previously caused deployment failures, so regressions here could reintroduce incomplete
.envfiles or force unexpected wizard runs.Overview
Fixes
dr dotenv setup --if-neededskipping the wizard when required vars were present only in the OS environment, leaving.envincomplete and breaking downstream deploys.shouldSkipSetupnow uses new file-only validation (ValidateEnvironmentFileOnly/GatherUserPromptsFromFile) that ignoresos.LookupEnv, so an incomplete.envstill triggers setup. Prompt value resolution was refactored with anincludeEnvflag andresolvePromptValue, and a regression test covers the env-var-only case.Reviewed by Cursor Bugbot for commit ac8d5d7. Configure here.