lazygit icon indicating copy to clipboard operation
lazygit copied to clipboard

Request: avoid writing to `LAZYGIT_NEW_DIR_FILE` if not changing directory

Open baodrate opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe.

Currently, lazygit writes to the file specified by $LAZYGIT_NEW_DIR_FILE whether it is designated to change directories or stay in the current one. In the latter case lazygit writes the CWD to the file. The problem is that the wrapper function suggested in the README always executes cd if $LAZYGIT_NEW_DIR_FILE exists and this is problematic because that can have side-effects. Particularly, in zsh the chpwd hooks will trigger whether the directory that is changed to is the same as the current one or not. (one of the things I use chpwd hooks for is to set various environment variables. an undesired cd can overwrite these)

Describe the solution you'd like

Currently, lazygit writes the CWD to $LAZYGIT_NEW_DIR_FILE if gui.RetainOriginalDir is true:

https://github.com/jesseduffield/lazygit/blob/41071c37035a5944f77b4f0da5f07ec3b52fef0e/pkg/gui/gui.go#L637-L646

why not simply avoid doing anything? The file only needs to be touched if we want to indicate to the user that a change of directory is desired.

Describe alternatives you've considered

Currently, my lg() wrapper function looks like (w/ zsh-specific syntax):

function lg {
  emulate -L zsh
  (){
    local -x LAZYGIT_NEW_DIR LAZYGIT_NEW_DIR_FILE=$1
    if lazygit ${@:2}; then
      [[ -f $LAZYGIT_NEW_DIR_FILE ]] &&
        LAZYGIT_NEW_DIR=$(<$LAZYGIT_NEW_DIR_FILE) &&
        [[ $LAZYGIT_NEW_DIR:A == $PWD:A ]]
        cd $LAZYGIT_NEW_DIR
    fi
  } =() $@
}

which works, but notice the :A, which in zsh-language resolves the path using realpath, because the path lazygit writes seems to be an absolute path, and might or might not have symlinks, so it is necessary to fully resolve both $PWD and $LAZYGIT_NEW_DIR to match.

This seems to work decently but is kind of annoying.

Additional context

when writing to $LAZYGIT_NEW_DIR_FILE, lazygit uses os.Getwd() which says:

Getwd returns a rooted path name corresponding to the current directory. If the current directory can be reached via multiple paths (due to symbolic links), Getwd may return any one of them.

Testing on my system shows that (w/ ~/foobar linked to ~/var/deeply/nested/path) if switching from a different project to ~/foobar/proj001, lazygit shows the proper symlinked path in the interface (i.e. the recent repository menu shows proj0001 /home/qubidt/foobar/proj001 ), the path it writes to $LAZYGIT_NEW_DIR_FILE will be /home/qubidt/var/deeply/nested/path/proj001.

This is also undesirable and perhaps should be a separate ticket.

baodrate avatar Jun 28 '22 19:06 baodrate