Skip to content

Incorrect custom command configuration is handled poorly #4256

Open
@brandondong

Description

@brandondong

Describe the bug
I was trying to set up a custom command for the first time.

customCommands:
  - key: '<c-m>'
    context: 'localBranches'
    command: 'git merge master'

Putting that in my config and launching lazygit on Windows causes it to just immediately terminate with no error message and a strange artifact at the edge of the window.
Image

Only later did I learn that ctrl-m was not supported: docs/keybindings/Custom_Keybindings.md

log.Fatalf("Unrecognized key %s for keybinding. For permitted values see %s", strings.ToLower(key), constants.Links.Docs.CustomKeybindings)

log.Fatal while the TUI has already started appears to cause this issue. I tried different shells and terminals on Windows to the same result. On WSL, it's better but multiline log.Fatal's still do not render quite correctly
Image
(This log.Fatal happens when providing an invalid custom command context)

I think the ideal behavior would be for the custom command key/context validation to happen in

if err := base.Validate(); err != nil {

Custom command misconfigurations would then behave consistently with how other user config errors are (gracefully) handled:

  1. If a config misconfiguration happens on startup, then lazygit cleanly prints the error and exits before starting the TUI.
  2. If a config misconfiguration happens during user config reload, an error popup will be shown and the old valid config will still be used instead of immediately crashing.
  3. If a config misconfiguration happens during repository switch, an error popup will be shown and the switch will be prevented instead of immediately crashing.

Actually making that fix is a bit of a refactor though...

Also, I noticed there are 3 disagreeing sources for what is a valid context value.

  1. The docs: https://github.com/jesseduffield/lazygit/blob/master/docs/Custom_Command_Keybindings.md#contexts
  2. What is actually allowed:
    ctx, ok := self.contextForContextKey(types.ContextKey(context))
    (basically any context key, for example context: 'statusSpacer1' is accepted...)
  3. The context keys listed in the log fatal error message:
    allContextKeyStrings := lo.Map(context.AllContextKeys, func(key types.ContextKey, _ int) string {
    Almost the same as 2 but missing some of the more recently added context keys.

To Reproduce
Steps to reproduce the behavior:

Add

customCommands:
  - key: '<c-m>'
    context: 'localBranches'
    command: 'git merge master'

or

customCommands:
  - key: '<c-n>'
    context: 'localBranches2'
    command: 'git merge master'

to your config and then launch lazygit

Expected behavior
See above

Screenshots
If applicable, add screenshots to help explain your problem.

Version info:
master branch

Additional context
Add any other context about the problem here.

Note: please try updating to the latest version or manually building the latest master to see if the issue still occurs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions