upbound / up

The @upbound CLI
Apache License 2.0
49 stars 42 forks source link

`up ctx` panics when new context matches current context #521

Open RedbackThomson opened 1 month ago

RedbackThomson commented 1 month ago

What happened?

Caught panic:

runtime error: invalid memory address or nil pointer dereference

Restoring terminal...

goroutine 1 [running]:
runtime/debug.Stack()
    runtime/debug/stack.go:24 +0x64
runtime/debug.PrintStack()
    runtime/debug/stack.go:16 +0x1c
github.com/charmbracelet/bubbletea.(*Program).Run.func1()
    github.com/charmbracelet/bubbletea@v0.25.0/tea.go:478 +0x88
panic({0x1078f4ce0?, 0x109a241b0?})
    runtime/panic.go:770 +0x124
github.com/upbound/up/cmd/up/ctx.(*fileWriter).Write(0x140006fcc80, 0x14000a35ec0)
    github.com/upbound/up/cmd/up/ctx/writer.go:81 +0x98
github.com/upbound/up/cmd/up/ctx.(*Space).Accept(0x14000dc20c0, 0x14000906ff0?, {0x107e09398, 0x140006fcc80})
    github.com/upbound/up/cmd/up/ctx/actions.go:40 +0x11c
github.com/upbound/up/cmd/up/ctx.(*Space).Items.func2({0x2e, {0x0, 0x0, 0x0, 0x0, 0x1, 0x0, {0x106a20e2b, 0x4}, {0x106a23e3a, ...}, ...}, ...})
    github.com/upbound/up/cmd/up/ctx/navigation.go:253 +0x54
github.com/upbound/up/cmd/up/ctx.model.Update({0x2e, {0x0, 0x0, 0x0, 0x0, 0x1, 0x0, {0x106a20e2b, 0x4}, {0x106a23e3a, ...}, ...}, ...}, ...)
    github.com/upbound/up/cmd/up/ctx/list.go:231 +0x1320
github.com/charmbracelet/bubbletea.(*Program).eventLoop(0x140007eb5f0, {0x107e252b0?, 0x1400092c008?}, 0x140012b45a0)
    github.com/charmbracelet/bubbletea@v0.25.0/tea.go:411 +0x4d8
github.com/charmbracelet/bubbletea.(*Program).Run(0x140007eb5f0)
    github.com/charmbracelet/bubbletea@v0.25.0/tea.go:543 +0x6c8
github.com/upbound/up/cmd/up/ctx.(*Cmd).RunInteractive(0x14000c7b258, {0x107e37300, 0x140011a6730}, 0x14000c1e200, 0x14000fffe60?, {0x107e18d48, 0x109b161a0})
    github.com/upbound/up/cmd/up/ctx/cmd.go:346 +0x3b4
github.com/upbound/up/cmd/up/ctx.(*Cmd).Run(0x14000c7b258, {0x107e37300, 0x140011a6730}, 0x14000c1e200, 0x14000fffe60)
    github.com/upbound/up/cmd/up/ctx/cmd.go:119 +0x13c
reflect.Value.call({0x107aa4980?, 0x14000c7b258?, 0x1400110f858?}, {0x106a20d23, 0x4}, {0x14001060720, 0x3, 0x1046ba808?})
    reflect/value.go:596 +0x970
reflect.Value.Call({0x107aa4980?, 0x14000c7b258?, 0x1400110f958?}, {0x14001060720?, 0x107df4060?, 0x104507de0?})
    reflect/value.go:380 +0x94
github.com/alecthomas/kong.callFunction({0x107aa4980?, 0x14000c7b258?, 0x18?}, 0x14001094450)
    github.com/alecthomas/kong@v0.8.0/callbacks.go:98 +0x390
github.com/alecthomas/kong.callMethod({0x106a1eff9, 0x3}, {0x107baade0?, 0x14000c7b258?, 0x3?}, {0x107aa4980?, 0x14000c7b258?, 0x140011a6230?}, 0x107a04820?)
    github.com/alecthomas/kong@v0.8.0/callbacks.go:132 +0x54
github.com/alecthomas/kong.(*Context).RunNode(0x14000c1e200, 0x14000402780, {0x0, 0x0, 0x107e64c20?})
    github.com/alecthomas/kong@v0.8.0/context.go:762 +0x64c
github.com/alecthomas/kong.(*Context).Run(0x14000f9a660?, {0x0?, 0x140011a6730?, 0x107689280?})
    github.com/alecthomas/kong@v0.8.0/context.go:787 +0x138
main.main()
    github.com/upbound/up/cmd/up/main.go:193 +0x588
up: error: ctx.Cmd.Run(): unexpected model type: <nil>

The panic resolves to this line, which seems to happen because we aren't checking for nil on ctpConf. Following activateContext, it appears to be possible that we're returning a nil for the configuration if the new context is deep equal to the current context. We should add a nil check to that variable and safely return.

How can we reproduce it?

I'm not even sure... @cwilhit found it and he isn't sure why either.

What environment did it happen in?