Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in identifier/syntax prevents autocommands to be executed #3631

Open
dunric opened this issue Jan 13, 2024 · 7 comments
Open

Error in identifier/syntax prevents autocommands to be executed #3631

dunric opened this issue Jan 13, 2024 · 7 comments

Comments

@dunric
Copy link

dunric commented Jan 13, 2024

What did you do? (required: The issue will be closed when not provided)

  1. create a new empty project, initialize with go mod init
  2. create a new main.go file with following-like contents:
package main

func main() {
   report()  // function defined in other file
}
  1. create a new file other.go in the same project's root directory:
package main

import "fmt"

// error in keyword fnc ⇒ func
fnc report() {
	fomt.Println("vim-go")   // error in module identifier fomt ⇒ fmt
}
  1. open main.go in gVim. Autocommands defined in .vimrc get executted.
  2. open within the same session other.go with :edit command. Autocommands are not executed.

defined in .vimrc

" This won't get executed when subsequent file contains an invalid syntax
autocmd FileType go setlocal tabstop=4 noexpandtab foldmethod=syntax 

What did you expect to happen?

Autocommands for FileType go will be triggered, despite some syntactic errors, just based on file's extension .go .

What happened instead?

Autocommands were not run, when other.go was opened as the second file.
Curiously, when other.go is opened as the first one, FileType based autocommands are executed, despite the same contents with syntactic errors.

Autocommands are also executed when other.go contents is fixed and valid, even when opened as the second one. Seems it may be related to gopls response which subsequently will prevent autocommands to be triggered.

Configuration (MUST fill this out):

vim-go version:

current, latest commit 5bed70d

vimrc you used to reproduce:

vimrc
set nocompatible
filetype on
filetype plugin on
filetype plugin indent on

autocmd FileType go setlocal tabstop=4 noexpandtab foldmethod=syntax 

Vim version (first three lines from :version):

VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Dec 15 2023 17:06:05)

Go version (go version):

go version go1.21.5 linux/amd64

Go environment

go env Output:
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/dunric/.cache/go-build'
GOENV='/home/dunric/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/dunric/.go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/dunric/.go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/dunric/Projects/Go/foo/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1460767399=/tmp/go-build -gno-record-gcc-switches'

gopls version

gopls version Output:
golang.org/x/tools/gopls (devel)
    golang.org/x/tools/gopls@(devel)

vim-go configuration:

vim-go configuration
g:go_auto_type_info = 1
g:go_term_enabled = 1
g:go_jump_to_error = 0
g:go_doc_popup_window = 0
g:go_get_update = 0
g:go_fold_enable = ['block', 'varconst', 'import']
g:go_auto_sameids = 1
g:go_metalinter_command = 'staticcheck'
g:go_doc_balloon = 0
g:go_doc_url = 'http://localhost:6060'
g:go_term_mode = 'vertical botright split'
g:go_loaded_gosnippets = 1
g:go_fillstruct_mode = 'gopls'
g:go_template_file = '/home/dunric/.vim/templates/skeleton.go'
g:go_template_test_file = '/home/dunric/.vim/templates/skeleton_test.go'
g:go_loaded_install = 1
g:go_updatetime = 800
g:go_doc_keywordprg_enabled = 1
g:go_diagnostics_level = 1
g:go_metalinter_autosave = 1
g:go_gopls_use_placeholders = v:true

filetype detection configuration:

filetype detection
filetype detection:ON  plugin:ON  indent:ON
@bhcleek
Copy link
Collaborator

bhcleek commented Jan 13, 2024

I'm only able to duplicate a problem with the folding when other.go is opened after main.go. The other settings are applied correctly. I suspect that what's going on here is a Vim bug of some sort, not a vim-go bug. Some quick testing seems to support that idea.

The folding is as expected when other.go is opened after main.go when setting foldmethod is done unconditionally (e.g. not as part of an auto command). And interestingly, executing :setlocal foldmethod=syntax folds correctly even when setting foldmethod via the autocommand didn't cause the intended effect.

As far as I can tell, this isn't really a vim-go bug, especially given how the order file opening impacts the results.

@dunric
Copy link
Author

dunric commented Jan 14, 2024

@bhcleek Thank you for investigating the issue, however I beg to differ this issue is of stock Vim and not of vim-go plugin. I've experimented with removing vim-go and using only the rudimentary Go support coming with plain Vim (compiler, ftplugin, indentation & syntax highlight) and the issue didn't manifest. I've altered tabstop and foldmethod FileType plugin autocommand settings and they were correctly applied.

@bhcleek
Copy link
Collaborator

bhcleek commented Jan 14, 2024

I hear you, @dunric. What's throwing me is that the I see tabstop applied correctly in all scenarios. I've also seen foldmethod get applied correctly with vim-go when I edit other.go very quickly after opening main.go. Something is amiss, but it's hard to track down, and I'm not yet sure what's going on.

@bhcleek
Copy link
Collaborator

bhcleek commented Jan 14, 2024

Interestingly, it appears that editing other.go again fixes the folding 🤔 I'm really not sure what to make of this. Vim-go doesn't do anything specific with regard to setting the foldmethod.

@bhcleek
Copy link
Collaborator

bhcleek commented Jan 16, 2024

Given that expandtab seems to be being applied correctly in all cases and that vim-go does not do anything specific with foldmethod, I'm still inclined to believe that this is some kind of Vim bug. Clearly, if it is a Vim bug, it's triggered by conditions that vim-go creates. I'll continue to see if I can narrow it down, but currently I'm stumped.

@bhcleek
Copy link
Collaborator

bhcleek commented Jan 16, 2024

I finally have a replication path that I can use to start tracking down the root cause.

@bhcleek
Copy link
Collaborator

bhcleek commented Feb 25, 2024

Disabling diagnostics highlighting (i.e. g:go_diagnostics_level) results in the expected behavior. Diagnostic highlighting does a few things with autocmds and also sets text properties. I'm not yet sure exactly what's triggering the errant behavior in Vim, but I suspect it's something to do with setting an autocmd or text properties while Vim is also processing for folding that causes the folds to not be applied when the foldmethod is syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants