Fix panic when loading invalid Compose file#2006
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRefactors CLI loader construction by introducing Changes
Sequence Diagram(s)sequenceDiagram
participant CLI as "CLI Command"
participant Loader as "Session Loader"
participant StackMgr as "Stack Manager / stacks.GetFallbackStack"
participant FS as "Compose Files / FS"
CLI->>Loader: configureLoaderForCommand(cmd) -> newSessionLoaderOptionsForCommand
Loader->>Loader: build session.LoaderOptions (ProjectName, ComposeFilePaths)
Loader->>FS: attempt LoadProject / resolve ProjectWorkingDir
alt compose file missing or invalid
FS-->>Loader: return types.ErrComposeFileNotFound / validation error
Loader->>StackMgr: call GetFallbackStack(defaults)
StackMgr-->>Loader: return fallback params or error
else compose ok
FS-->>Loader: project loaded
end
Loader->>StackMgr: newStackManagerForLoader(loader) (may receive nil sm)
StackMgr-->>CLI: stack manager or error
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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.11.4)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" Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/cmd/cli/command/session.go (1)
63-93:⚠️ Potential issue | 🟡 MinorGuard the provider-flag lookup.
Line 70 assumes the
providerflag is always registered, butcmd.Flag("provider")returnsnilwhen absent. Calling.Changedonnilcauses a panic. The test at line 135-139 creates a barecobra.Commandwithout registeringprovider, and the test case withprojectName: "myproject"(line 87) will trigger this path.Suggested fix
if projectName != "" { + providerFlag := cmd.Flag("provider") var maybeProvider client.ProviderID - if maybeProvider.Set(projectName) == nil && !cmd.Flag("provider").Changed { + if maybeProvider.Set(projectName) == nil && (providerFlag == nil || !providerFlag.Changed) { // using -p with a provider name instead of -P term.Warnf("Project name %q looks like a provider name; did you mean to use -P=%s instead of -p?", projectName, projectName) doubleCheckProjectName(projectName)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/cmd/cli/command/session.go` around lines 63 - 93, newSessionLoaderOptionsForCommand calls cmd.Flag("provider").Changed without guarding for nil; fetch the flag once (e.g. f := cmd.Flag("provider")), compute a safe "not changed" boolean as (f == nil || !f.Changed) and use that in the existing condition that calls maybeProvider.Set(projectName) == nil so you don't dereference a nil flag; update the branch in newSessionLoaderOptionsForCommand (and keep the existing calls to maybeProvider.Set, term.Warnf and doubleCheckProjectName) to use this guarded check.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/cmd/cli/command/session_test.go`:
- Around line 98-103: The test currently does a shallow copy with oldGlobal :=
global which doesn't protect nested state (GlobalConfig.Stack and its
Variables); instead, save the specific fields you modify (e.g., save oldProvider
:= global.Stack.Provider and if you mutate variables save a deep copy of
global.Stack.Variables or save oldVariables := append([]Type(nil),
global.Stack.Variables...) as appropriate) and restore those specific fields in
the t.Cleanup closure (restore global.Stack.Provider and restore
global.Stack.Variables) rather than restoring the top-level global variable;
update the session_test.go setup that sets global.Stack.Provider = "defang" to
use this targeted save/restore approach.
In `@src/pkg/session/session.go`:
- Around line 77-80: When sl.sm is nil the code calls
stacks.GetFallbackStack(sl.opts.Default) which currently forces the returned
stack's Name to DefaultBeta and can drop an explicit --stack value; preserve an
explicit stack name by checking sl.opts.Default (the CLI-provided stack name)
and, if non-empty, ensure the returned stack has its Name set to sl.opts.Default
(either by calling a fallback helper that accepts a name or by updating the Name
field on the result of stacks.GetFallbackStack); modify the logic around sl.sm,
stacks.GetFallbackStack, and sl.opts.Default so an explicitly provided stack
name is not overwritten.
---
Outside diff comments:
In `@src/cmd/cli/command/session.go`:
- Around line 63-93: newSessionLoaderOptionsForCommand calls
cmd.Flag("provider").Changed without guarding for nil; fetch the flag once (e.g.
f := cmd.Flag("provider")), compute a safe "not changed" boolean as (f == nil ||
!f.Changed) and use that in the existing condition that calls
maybeProvider.Set(projectName) == nil so you don't dereference a nil flag;
update the branch in newSessionLoaderOptionsForCommand (and keep the existing
calls to maybeProvider.Set, term.Warnf and doubleCheckProjectName) to use this
guarded check.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9cf79f56-93a6-4593-abef-5c9a9e29cfb6
⛔ Files ignored due to path filters (1)
src/go.sumis excluded by!**/*.sum
📒 Files selected for processing (13)
pkgs/defang/cli.nixsrc/cmd/cli/command/commands.gosrc/cmd/cli/command/compose.gosrc/cmd/cli/command/deployments.gosrc/cmd/cli/command/estimate.gosrc/cmd/cli/command/session.gosrc/cmd/cli/command/session_test.gosrc/cmd/cli/command/stack.gosrc/cmd/cli/command/testdata/invalid-compose/compose.yamlsrc/go.modsrc/pkg/agent/fs.gosrc/pkg/session/session.gosrc/pkg/stacks/manager.go
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Description
After "debug with AI"-prompt, code would continue without session and panic.
Linked Issues
Fixes #2007
Checklist
Summary by CodeRabbit
Bug Fixes
Refactor
Chores
Tests