jesseduffield / jesseduffield/lazygit

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

Open
#2,018 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
82.4k
Forks
3k
Avg merge
2d 18h
Merged PRs (30d)
19

Description

**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](https://zsh.sourceforge.io/Doc/Release/Functions.html#Hook-Functions) 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](https://pkg.go.dev/os#Getwd):

> 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.