Skip to content

Worktree path does not expand #4708

Description

@iLynxcat

Describe the bug
When creating a new worktree and entering its path (i.e. ~/path/to/worktree or ../worktree-name), those path segments (~, ..) do not expand because they are normally expanded automatically by the terminal (afaik) and instead a directory at /path/to/repository/~/path/to/worktree is created.

To Reproduce
Steps to reproduce the behavior:

  1. Open a repository by running "lazygit" when pwd is a git repository
  2. Activate Worktrees tab by typing 2 then ]
  3. Choose "New worktree" by typing n
  4. Choose "Create worktree from ref"
  5. Choose a base ref (I recommend using your current checked out ref)
  6. Enter a worktree path that begins with ".." or "~", and submit
  7. Enter a new branch name (I recommend test/worktree-creation)
  8. Take note of the new Worktree -> Path relative to the Worktree -> Path when using the arrow key select your previous checked-out worktree

Expected behavior
A clear and concise description of what you expected to happen.

I expect ".." and "~" to expand as they would if I ran the command in my terminal directly, and the worktree to be created accordingly

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

A worktree existing at path /Users/lynx/Developer/OBFUSCATED_PATH/~/path/to/worktree Finder window navigated to a project directory, with a subdirectory '~' followed by path/to/worktree, containing the worktree files directly.

Version info:
Run lazygit --version and paste the result here
commit=, build date=, build source=Homebrew, version=0.53.0, os=darwin, arch=arm64, git version=2.39.5 (Apple Git-154)
Run git --version and paste the result here
git version 2.39.5 (Apple Git-154)

Additional context
Add any other context about the problem here.

None to share

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

Activity

  1. ChrisMcD1 commented on Aug 12, 2025

    @ChrisMcD1
    Contributor

    I was able to reproduce this on WSL with ~ at the start of the path, but unable to reproduce when using .. at the start of the path. That worked perfectly fine for me and produced the expected output.

    Image

    We don't do anything weird with the Path argument:


    Arg(opts.Path, opts.Base)

    So it seems the problem comes down to the way that Go's os/exec behaves:

    Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells. The package behaves more like C's "exec" family of functions.

    https://pkg.go.dev/os/exec

    We could add a patch similar to the following to make ~ in particular work. Not sure if that's too hacky or not. There seems to have once been a pretty popular repo that did much of the same (until it was archived and os.UserHomeDir was created, which took away much of its core functionality) https://github.com/mitchellh/go-homedir/tree/main

    diff --git a/pkg/gui/controllers/helpers/worktree_helper.go b/pkg/gui/controllers/helpers/worktree_helper.go
    index a54adcc49..f0dd51861 100644
    --- a/pkg/gui/controllers/helpers/worktree_helper.go
    +++ b/pkg/gui/controllers/helpers/worktree_helper.go
    @@ -2,6 +2,8 @@ package helpers
    
     import (
            "errors"
    +       "os"
    +       "path/filepath"
            "strings"
    
            "github.com/jesseduffield/gocui"
    @@ -114,7 +116,18 @@ func (self *WorktreeHelper) NewWorktreeCheckout(base string, canCheckoutBase boo
            self.c.Prompt(types.PromptOpts{
                    Title: self.c.Tr.NewWorktreePath,
                    HandleConfirm: func(path string) error {
    -                       opts.Path = path
    +                       if len(path) == 0 {
    +                               return errors.New("Path cannot be empty!")
    +                       }
    +                       if path[0] != '~' {
    +                               opts.Path = path
    +                       } else {
    +                               homeDir, err := os.UserHomeDir()
    +                               if err != nil {
    +                                       return err
    +                               }
    +                               opts.Path = filepath.Join(homeDir, path[1:])
    +                       }
    
                            if detached {
                                    return f()
    
  2. stefanhaller commented on Aug 12, 2025

    @stefanhaller
    Collaborator

    So it seems the problem comes down to the way that Go's os/exec behaves:

    This has nothing to do with os/exec, which is not involved here.

    We could add a patch similar to the following to make ~ in particular work. Not sure if that's too hacky or not.

    The patch seems reasonable to me, except that I would extract the logic to a helper function (maybe somewhere in pkg/utils/).

  3. ChrisMcD1 commented on Aug 13, 2025

    @ChrisMcD1
    Contributor

    So it seems the problem comes down to the way that Go's os/exec behaves:

    This has nothing to do with os/exec, which is not involved here.

    I might be reading this wrong, but as I see it, the opts.Path goes here

    cmdArgs := NewGitCmd("worktree").Arg("add").
    ArgIf(opts.Detach, "--detach").
    ArgIf(opts.Branch != "", "-b", opts.Branch).
    Arg(opts.Path, opts.Base)
    return self.cmd.New(cmdArgs.ToArgv()).Run()

    which then goes to here

    func (self *CmdObjBuilder) New(args []string) *CmdObj {
    cmdObj := self.NewWithEnviron(args, os.Environ())

    which then is calling os.exec here:

    func (self *CmdObjBuilder) NewWithEnviron(args []string, env []string) *CmdObj {
    cmd := exec.Command(args[0], args[1:]...)

    So the "problem" is that os.exec does not do the shell interpolation of ~ like the reporter expects. (I concede that it might not be considered a "problem" since this is intended behavior)

  4. stefanhaller commented on Aug 13, 2025

    @stefanhaller
    Collaborator

    @ChrisMcD1 You are right. For some reason I was thinking this os/exec comment would only apply to the executable being called, but of course it applies to command arguments in the same way.

  5. stefanhaller commented on Jun 30, 2026

    @stefanhaller
    Collaborator

    This is included in #5741, along with a lot of other improvements to the whole create worktree flow.

  6. iLynxcat commented on Jun 30, 2026

    @iLynxcat
    Author

    yay! cheers for resolving this, glad to see it -- and still happily using lazygit every day :)

  7. added a commit that references this issue on Jul 3, 2026
    7458275
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

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions