Skip to content

Nil-pointer panic (SIGSEGV) when switching to a bare/worktree repo via the recent-repos menu #5736

Closed

Description

@gitmpr

Nil-pointer panic (SIGSEGV) when switching to a bare/worktree repo via the recent-repos menu

Describe the bug

When a bare repo that uses the git worktree ".git-file pointer" layout (a directory containing .git → gitdir: ./.bare, with the actual bare repo in .bare/) is present in the recent-repos list, selecting it from the in-app recent-repos menu (<c-r>) crashes lazygit with a nil-pointer SIGSEGV.

This is the same root failure as the existing "open lazygit directly in a bare repo" reports (#5469, #5681, #3100), but a different and worse failure mode: those go through the startup path and surface a handled error ("must be run in a work tree") and exit cleanly. The in-session switch path has no such handling, so the post-switch error-recovery Refresh dereferences a nil git command and the whole process panics.

(Code references below are pinned to the v0.61.1 tag, matching the panic traces.)

Root cause

Switching repos goes through ReposHelper.DispatchSwitchTo:

  1. os.Chdir(path) into the bare-repo directory — succeeds.

  2. commands.VerifyInGitRepo (pkg/commands/git.go:177) is just git rev-parse --git-dir, which succeeds for a bare repo, so the switch is allowed to proceed.

  3. onNewRepo runs, which calls GetRepoPaths → git rev-parse --path-format=absolute --show-toplevel --absolute-git-dir --git-common-dir --is-bare-repository .... This fails for a bare repo with fatal: this operation must be run in a work tree (pkg/gui/gui.go:323 → pkg/commands/git.go:67).

  4. That failure is surfaced as an error popup. The popup's recovery path (NewGui.func2, pkg/gui/gui.go:750) calls Refresh.

  5. But the new repo's git command was never constructed (step 3 failed), so self.c.Git() is nil, and the refresh helpers dereference it:

    → panic: runtime error: invalid memory address or nil pointer dereference.

Why the .git-pointer (git-worktree) layout is required to hit this: lazygit's "is this a repo?" checks are os.Stat(filepath.Join(dir, ".git")) (isDirectoryAGitRepository, pkg/app/app.go:165; newRecentReposList, pkg/gui/recent_repos_panel.go:36). A bare repo laid out as a directory with a .git file pointing at ./.bare passes that check, so it is listed and selectable in the recent-repos menu, even though git rev-parse --is-bare-repository is true. A plain git init --bare x.git directory has no .git entry, fails the check, and is filtered out, so it never reaches this path. updateRecentRepoList, recent_repos_panel.go:11 refuses to add bare repos, but nothing re-validates an entry already in the list before switching into it (e.g. one added by an older lazygit version, or by external tooling).

The menu also displays the entry as Branch unknown, because getCurrentBranch, repos_helper.go:74-94 reads the .git pointer and then tries to read ./.bare/HEAD relative to the wrong working directory.

To Reproduce

Isolated repro (uses throwaway XDG_* dirs so it doesn't touch a real recent-repos list):

export XDG_CONFIG_HOME=/tmp/lgrepro/config
export XDG_STATE_HOME=/tmp/lgrepro/state
rm -rf /tmp/lgrepro && mkdir -p "$XDG_CONFIG_HOME/lazygit" "$XDG_STATE_HOME/lazygit"

# bare-worktree repo with the ".git -> ./.bare" pointer
mkdir -p /tmp/lgrepro/barewt && cd /tmp/lgrepro/barewt
git init -q --bare .bare
printf 'gitdir: ./.bare\n' > .git

# a normal repo to launch lazygit in
mkdir -p /tmp/lgrepro/normal && cd /tmp/lgrepro/normal && git init -q

# seed lazygit's recent-repos list with the bare dir
cat > "$XDG_STATE_HOME/lazygit/state.yml" <<'YML'
recentrepos:
    - /tmp/lgrepro/normal
    - /tmp/lgrepro/barewt
YML

cd /tmp/lgrepro/normal
lazygit
# press <c-r> to open recent repositories, select the "/tmp/lgrepro/barewt"
# entry (shown as "Branch unknown"), press <enter>  ->  SIGSEGV panic

For contrast, the openRecentRepo startup path does NOT panic — it shows the handled error instead. Put /tmp/lgrepro/barewt first in recentrepos, then cd to a non-repo dir and run lazygit, answering n to "create a new repository?". You get:

*errors.errorString Error getting repo paths: '... --is-bare-repository ...' failed: fatal: this operation must be run in a work tree
        pkg/gui/gui.go:323

Actual behavior

Crash (the in-session switch path):

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV ...]
...helpers.(*RefreshHelper).refreshStatus
        pkg/gui/controllers/helpers/refresh_helper.go:756
...helpers.(*RefreshHelper).Refresh.func2
        pkg/gui/controllers/helpers/refresh_helper.go:219
...popup.(*PopupHandler).ErrorHandler
        pkg/gui/popup/popup_handler.go:96
...
panic: runtime error: invalid memory address or nil pointer dereference
...helpers.(*RefreshHelper).refreshStashEntries
        pkg/gui/controllers/helpers/refresh_helper.go:739

Expected behavior

Switching to a bare repo from the recent-repos menu should be handled the same way as launching directly in one: a graceful error/skip (and ideally proper bare-repo + worktree support, cf. #4330), never a nil-pointer panic that crashes the application. At minimum, DispatchSwitchTo should detect the bare repo (or the failed GetRepoPaths) and abort the switch cleanly instead of running a Refresh against a nil git command.

A note on the specific trigger: this is the ".git-file at the repo root" variant of a git worktree setup, where the repo root holds a .git file pointing at a sibling bare repo (gitdir: ./.bare) instead of the bare repo living at the root directly. That pointer-file convention is documented across several git-worktree guides — e.g. git-wt and this blog post, as well as other write-ups. The .git file is added so editors and other tools that expect a .git entry at the repo root keep working from the root of a bare-worktree checkout. Bare-repo-plus-worktree setups are an advanced (less mainstream) git feature overall, but within that space this .git → .bare layout is an established convention — so it's not a one-off. Either way, lazygit handling it gracefully seems clearly preferable to a nil-pointer panic that crashes the application.

Version info

commit=, build date=, build source=nix, version=0.61.1, os=linux, arch=amd64, git version=2.53.0

(Installed via the nixpkgs lazygit package, build source nix.)

The relevant code (DispatchSwitchTo gated only by VerifyInGitRepo; the nil-deref refresh helpers; the os.Stat(dir/.git) repo checks) is unchanged on the latest release v0.62.2 and on tip-of-master (commit b31e57254), so it should reproduce there too.

Related issues

Activity

  1. stefanhaller commented on Aug 8, 2026

    @stefanhaller
    Collaborator

    @gitmpr I can reproduce the issue by manually adding such a path to my state.yml, as you do in your reproduction recipe; but I don't see how such a path can end up in the menu through normal lazygit usage. Whatever, it is fixed by #5910, which now shows an error popup instead of crashing.

  2. added a commit that references this issue on Aug 8, 2026
    bf5829a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions