govim / govim

govim is a Go development plugin for Vim8, written in Go
BSD 3-Clause "New" or "Revised" License
870 stars 62 forks source link

cmd/govim: breaks jump-to-line from commandline #589

Open Merovius opened 4 years ago

Merovius commented 4 years ago

What version of Vim/Gvim are you using?

Vim/Gvim version
$ vim --version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Oct 22 2019 11:41:03)
Included patches: 1-2198
Modified by 
Compiled by 
Huge version with GTK3 GUI.  Features included (+) or not (-):
+acl               -farsi             -mouse_sysmouse    -tag_any_white
+arabic            +file_in_path      +mouse_urxvt       -tcl
+autocmd           +find_in_path      +mouse_xterm       +termguicolors
+autochdir         +float             +multi_byte        +terminal
-autoservername    +folding           +multi_lang        +terminfo
+balloon_eval      -footer            -mzscheme          +termresponse
+balloon_eval_term +fork()            +netbeans_intg     +textobjects
+browse            +gettext           +num64             +textprop
++builtin_terms    -hangul_input      +packages          +timers
+byte_offset       +iconv             +path_extra        +title
+channel           +insert_expand     +perl/dyn          +toolbar
+cindent           +job               +persistent_undo   +user_commands
+clientserver      +jumplist          +postscript        +vartabs
+clipboard         +keymap            +printer           +vertsplit
+cmdline_compl     +lambda            +profile           +virtualedit
+cmdline_hist      +langmap           -python            +visual
+cmdline_info      +libcall           +python3/dyn       +visualextra
+comments          +linebreak         +quickfix          +viminfo
+conceal           +lispindent        +reltime           +vreplace
+cryptv            +listcmds          +rightleft         +wildignore
+cscope            +localmap          +ruby/dyn          +wildmenu
+cursorbind        +lua/dyn           +scrollbind        +windows
+cursorshape       +menu              +signs             +writebackup
+dialog_con_gui    +mksession         +smartindent       +X11
+diff              +modify_fname      -sound             -xfontset
+digraphs          +mouse             +spell             +xim
+dnd               +mouseshape        +startuptime       +xpm
-ebcdic            +mouse_dec         +statusline        +xsmp_interact
+emacs_tags        +mouse_gpm         -sun_workshop      +xterm_clipboard
+eval              -mouse_jsbterm     +syntax            -xterm_save
+ex_extra          +mouse_netterm     +tag_binary        
+extra_search      +mouse_sgr         -tag_old_static    
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "/etc/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/etc"
 f-b for $VIMRUNTIME: "/usr/share/vim/vim81"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/fribidi -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/uuid -I/usr/include/gio-unix-2.0 -I/usr/include/libdrm -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include -pthread    -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       
Linking: gcc   -L. -Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector-strong -rdynamic -Wl,-export-dynamic -Wl,--enable-new-dtags -Wl,-z,relro -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld  -Wl,-z,relro  -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -lpython3.7m -lcrypt -lpthread -ldl  -lutil -lm  -L/usr/local/lib -Wl,--as-needed -o vim   -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0  -lSM -lICE -lXpm -lXt -lX11 -lSM -lICE  -lm  -lselinux  -lncurses -lacl -lattr -lgpm -ldl   -Wl,--enable-new-dtags -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -fstack-protector-strong -L/usr/local/lib  -L/usr/lib64/perl5/CORE -lperl -lpthread -lresolv -ldl -lm -lcrypt -lutil -lc   

What version of Go are you using (go version)?

$ go version
go version go1.13 linux/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mero/.cache/go-build"
GOENV="/home/mero/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mero"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/mero/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/mero/go/pkg/tool/linux_amd64"
GCCGO="/usr/bin/gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/mero/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build053874096=/tmp/go-build -gno-record-gcc-switches"

What version/commit of govim are you using?

806674b9d799bebda41122ce143c234e961bbc97

What is the output of :messages in Vim?

Messages maintainer: Bram Moolenaar                                                                                             
Installing govim and gopls
"cmd/link/internal/ld/lib.go" 2563L, 72448C

Do the Vim channel, govim or gopls log files show anything interesting?

gopls.txt govim.txt vim_channel.txt

How did you install govim?

Vim 8 packages

What did you do?

$ mkdir ~/vimbackup
$ mv ~/.vim ~/.vimrc ~/vimbackup
$ cd ~/go/src # contains checkout of Go
$ vim cmd/link/internal/ld/lib.go +123
$ mkdir -p ~/.vim/pack/plugins/start
$ git clone https://github.com/govim/govim.git ~/.vim/pack/plugins/start/govim
$ vim cmd/link/internal/ld/lib.go +234

What did you expect to see?

vim first opening lib.go on line 123, then on line 234

What did you see instead?

vim first opening lib.go on line 123, then on line 123 again. i.e. vim opens a go-file on the last opened position (or 0), instead of the one provided on the command-line, once govim was installed.

myitcv commented 4 years ago

Hmm strange because the following works for me:

cd $(mktemp -d)
export HOME=$PWD
git clone https://github.com/govim/govim.git ~/.vim/pack/plugins/start/govim
cp ~/.vim/pack/plugins/start/govim/cmd/govim/config/minimal.vimrc .vimrc
git clone --single-branch https://github.com/golang/go.git
cd go/src/
git checkout 6f7b96f6cb5f5c156cf4e69b1fa1ec0e57b01677

Followed by:

vim cmd/link/internal/ld/lib.go +123

and then

vim cmd/link/internal/ld/lib.go +234

The only difference I notice is that your repro is missing a minimal .vimrc?

Merovius commented 4 years ago

Hmmmmm, so, this might be more confusing. Your steps also break for me. Same symptoms: File opens at last opened location, or line 1. If I remove govim, it works fine.

Now I'm wondering whether this has anything to do with vim-configs that ship with Fedora (had issues with that before, can't find a way to reliably disable them)… But it still is also definitely interacting with something govim is doing :)

Merovius commented 4 years ago

Okay, so I just noticed that this doesn't even just affect .go files. This also reproduces for me:

cd $(mktemp -d)
export HOME=$PWD
git clone https://github.com/govim/govim.git ~/.vim/pack/plugins/start/govim
cp ~/.vim/pack/plugins/start/govim/cmd/govim/config/minimal.vimrc .vimrc
vim .vimrc +3

Should open at line 3 - opens at line 1 instead. rm -rf ~/.vim/pack/plugins/start/govim makes it work.

Merovius commented 4 years ago

Okay, using a clean fedora image works to reproduce the issue:

mero@vetinari ~$ sudo docker run -it fedora:latest /bin/bash
[root@f0d3889f286b ~]# dnf install git vim golang
[root@f0d3889f286b ~]# cd $(mktemp -d)
[root@f0d3889f286b ~]# export HOME=$PWD
[root@f0d3889f286b ~]# git clone https://github.com/govim/govim.git ~/.vim/pack/plugins/start/govim
[root@f0d3889f286b ~]# cp ~/.vim/pack/plugins/start/govim/cmd/govim/config/minimal.vimrc .vimrc
[root@f0d3889f286b ~]# vim .vimrc +3
myitcv commented 4 years ago

Definitely confirmed as being something to do with the Fedora-shipped Vim files:

If you:

sudo docker run -it fedora:latest /bin/bash

then:

dnf install -y git vim golang
cd $(mktemp -d)
export HOME=$PWD
git clone https://github.com/govim/govim.git ~/.vim/pack/plugins/start/govim
cp ~/.vim/pack/plugins/start/govim/cmd/govim/config/minimal.vimrc .vimrc
vim -u .vimrc .vimrc +3

everything works.

If you run:

vim -V15/tmp/debug.vim .vimrc +3

you will find what appears to be the culprit in the log:

sourcing "/etc/vimrc"

If you delete /etc/vimrc then try:

vim .vimrc +3

everything works as expected.

myitcv commented 4 years ago

Thanks for the repro BTW - made debugging that significantly easier.

myitcv commented 4 years ago

For the record, the issue here is the following lines from /etc/vimrc:

" Only do this part when compiled with support for autocommands
if has("autocmd")
  augroup fedora
  autocmd!
  " In text files, always limit the width of text to 78 characters
  " autocmd BufRead *.txt set tw=78
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal! g'\"" |
  \ endif
  " don't write swapfile on most commonly used directories for NFS mounts or USB sticks
  autocmd BufNewFile,BufReadPre /media/*,/run/media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
  " start with spec file template
  " 1724126 - do not open new file with .spec suffix with spec file template
  " apparently there are other file types with .spec suffix, so disable the
  " template
  " autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
  augroup END
endif

It's unclear to my why the Fedora packaging folks want to enforce such a default behaviour on restoring cursor position, but I think that's a question best taken up with them.

The least invasive way of working around this without influencing the other Fedora defaults is to add the following at the top of your .vimrc:

au! fedora BufReadPost
Merovius commented 4 years ago

Hm… I'd like to ask you to reopen. Because 1. I already remove all fedora and redhat (and just to be sure just all) autocommands at the top of my .vimrc, precisely because I don't like their defaults, but 2. it turns out I actually have that specific setting in my .vimrc, because I like the behavior in general :)

Without govim, while it does open a file on the last used location when just opening it, giving a specific location on the command line still overwrites it. But govim somehow messes with this, by always using the last location. I suggest trying that out in the docker container (without removing the fedora defaults):

dnf install vim golang git
git clone https://github.com/govim/govim.git ~/.vim/pack/plugins/start/govim  
cp ~/.vim/pack/plugins/start/govim/cmd/govim/config/minimal.vimrc ~/.vimrc
rm -rf ~/.vim/pack/plugins/start/govim # yeah, I know, I'm going minimum effort here to get a well-known text file
vim ~/.vimrc +13 # opens on line 13
vim ~/.vimrc # opens on line 13 again
vim ~/.vimrc +23 # opens on line 23
git clone https://github.com/govim/govim.git ~/.vim/pack/plugins/start/govim
vim ~/.vimrc # opens on line 23
vim ~/.vimrc +42 # still line 23, should be 42

IMO there is still a bug with govim here.

[edit] sorry, couple edits to the series of commands, been sloopy [/edit]

myitcv commented 4 years ago

it turns out I actually have that specific setting in my .vimrc, because I like the behavior in general :)

Ah, well that changes everything!

Re-opening and will try and take a look what's going on. But my wild guess would be that it's something to do with multiple autocommands interacting badly with each other.

myitcv commented 4 years ago

Frustratingly, whilst trying to reproduce this with logging via:

vi -V9/tmp/vim.debug .vimrc +5

the problem does not reproduce!