Skip to content

Commit

Permalink
Add support for ~/.config/lazygit without setting XDG_CONFIG_HOME
Browse files Browse the repository at this point in the history
Currently lazygit looks for its config file in `XDG_CONFIG_HOME` if it's
available, but if not it falls back to the defaults defined by the
[xdg](https://github.com/adrg/xdg) package. Unfortunately the defaults
the package falls back to isn't what CLI applications commonly fall back
to on macOS. Specifically, it looks in `~/Library/Application Support`
instead of `~/.config`.

This updates the app config logic to:

- Look for `~/.config/lazygit` first if `XDG_CONFIG_HOME` is not set
  and we're on macOS.
- Fallback to the existing `xdg` package location if the configuration
  file exists there.
- Default to `~/.config/lazygit/config.yml` if `XDG_CONFIG_HOME` is not
  set, we're on macOS, and there is no existing configuration file.

This change did feel a bit like having to thread a needle and I didn't
see any existing tests for this behavior (which is reasonable, since it's
complicated and OS dependent) so I did test a few variations of the
configuration locally by building with this change included and running
a `brew` installed lazygit.

It seemed to work properly, falling back to the existing location when
`XDG_CONFIG_HOME` isn't set, using `~/.config/lazygit` when `config.yml`
is present, and creating `~/.config/lazygit/config.yml` when it's not.

I think this should resolve jesseduffield#1341
  • Loading branch information
BlakeWilliams committed Oct 12, 2024
1 parent d11e11d commit 73469eb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
37 changes: 36 additions & 1 deletion pkg/config/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,34 @@ func findConfigFile(filename string) (exists bool, path string) {
return true, legacyConfigPath
}

// if on macOs, default to looking in ~/.config/lazygit first since that's
// the most common location for CLI apps to store configuration when
// `XDG_CONFIG_HOME` is not set. The `xdg` package uses `~/Library/Application\ Support`
// as the default directory for macOS, so we have to handle this case manually.
if overrideXdgConfigHome && os.Getenv("XDG_CONFIG_HOME") == "" {
homeConfigFilePath, err := findHomeConfigFile(filename)
if err == nil {
return true, homeConfigFilePath
}
}

// look for lazygit/filename in XDG_CONFIG_HOME and XDG_CONFIG_DIRS
configFilepath, err := xdg.SearchConfigFile(filepath.Join("lazygit", filename))
if err == nil {
return true, configFilepath
}

return false, filepath.Join(xdg.ConfigHome, "lazygit", filename)
configHome := xdg.ConfigHome
// If we're on macOS and XDG_CONFIG_HOME is not set, we use
// ~/.config/lazygit instead of the default `~/Library/Application\ Support`
// directory the `xdg` package uses.
if overrideXdgConfigHome && os.Getenv("XDG_CONFIG_HOME") == "" {
if homeDir, err := os.UserHomeDir(); err == nil {
return false, filepath.Join(homeDir, ".config", "lazygit", filename)
}
}

return false, filepath.Join(configHome, "lazygit", filename)
}

var ConfigFilename = "config.yml"
Expand Down Expand Up @@ -494,3 +515,17 @@ func LogPath() (string, error) {

return stateFilePath("development.log")
}

func findHomeConfigFile(filename string) (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}

path := filepath.Join(home, ".config", "lazygit", filename)
if _, err = os.Stat(path); err != nil {
return "", err
}

return path, nil
}
5 changes: 5 additions & 0 deletions pkg/config/xdg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !darwin

package config

const overrideXdgConfigHome = false
5 changes: 5 additions & 0 deletions pkg/config/xdg_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build darwin

package config

const overrideXdgConfigHome = true

0 comments on commit 73469eb

Please sign in to comment.