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

When go_gopls_fumpt is enabled, go_gopls_local has no effect #3151

Closed
bravoecho opened this issue Feb 6, 2021 · 1 comment · Fixed by #3154
Closed

When go_gopls_fumpt is enabled, go_gopls_local has no effect #3151

bravoecho opened this issue Feb 6, 2021 · 1 comment · Fixed by #3154

Comments

@bravoecho
Copy link

What did you do?

I have configured vim-go to use gopls for formatting and imports, and I have prepared a dummy package to illustrate the problem.

  • the go_gopls_fumpt and go_gopls_local options do not work together
  • if both are set, only gofumpt is applied, whereas local is ignored
  • if gofumpt is not enabled, then local will work
  • if both are not set, the imports are still automatically populated normally, and basic formatting is still applied

What did you expect to happen?

I would like that the local and gofumpt options work correctly at the same time.

Given this file (see also rest of the package, attached vim-go-gopls-options.zip)

package main

import (
        "context"
        "fmt"
        "github.com/pkg/errors"
        "mymod/pkg1"
        "mymod/pkg2"
)

func main() {

        fmt.Println("vim-go")

}

func dummyFunc(ctx context.Context) {

        fmt.Println(ctx)
        pkg1.Foo()
        pkg2.Bar()
        err := errors.Wrap(errors.New("some error"), "some wrapping info")
        fmt.Println(err)

}

when I save I would like to obtain the following (with local imports separate from the rest, and extra lines at the beginning and end of functions removed)

package main

import (
	"context"
	"fmt"

	"mymod/pkg1"
	"mymod/pkg2"

	"github.com/pkg/errors"
)

func main() {
	fmt.Println("vim-go")
}

func dummyFunc(ctx context.Context) {
	fmt.Println(ctx)
	pkg1.Foo()
	pkg2.Bar()
	err := errors.Wrap(errors.New("some error"), "some wrapping info")
	fmt.Println(err)
}

What happened instead?

I obtain the following result, where only gofumpt was applied, whereas the local option was not respected:

package main

import (
        "context"
        "fmt"
        "mymod/pkg1"
        "mymod/pkg2"

        "github.com/pkg/errors"
)

func main() {
        fmt.Println("vim-go")
}

func dummyFunc(ctx context.Context) {
        fmt.Println(ctx)
        pkg1.Foo()
        pkg2.Bar()
        err := errors.Wrap(errors.New("some error"), "some wrapping info")
        fmt.Println(err)
}

Configuration

vim-go version:

007d9bc7786d7

vimrc you used to reproduce:

vimrc
set encoding=utf-8
scriptencoding utf-8

call plug#begin('~/.vim/plugged')
Plug 'fatih/vim-go', { 'do': ':GoInstallBinaries' }
call plug#end()

let g:go_fmt_command = 'gopls'
let g:go_fmt_autosave = 1
let g:go_fmt_fail_silently = 1

let g:go_imports_mode = 'gopls'
let g:go_imports_autosave = 1
let g:go_gopls_local = 'mymod'
let g:go_gopls_gofumpt = v:true

Vim version (first three lines from :version):

VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb  1 2021 23:54:08)
macOS version - x86_64
Included patches: 1-2450

Go version (go version):

go version go1.15.7 darwin/amd64

Go environment

go env Output:
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/luca.belmondo/Library/Caches/go-build"
GOENV="/Users/luca.belmondo/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/luca.belmondo/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/luca.belmondo/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.7_1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.7_1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/luca.belmondo/workspace/vim-go-bug/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/7l/jq5b64g946n0lxq82d8x32tryf6wnc/T/go-build767811874=/tmp/go-build -gno-record-gcc-switches -fno-common"

gopls version

gopls version Output:
golang.org/x/tools/gopls v0.6.5
    golang.org/x/tools/[email protected] h1:kLt9rD/dWtVdvc8LmdcxagDFih6zxYXREpKSYYZu5KE=

vim-go configuration:

vim-go configuration
g:go_fmt_command = 'gopls'
g:go_loaded_install = 1
g:go_gopls_local = 'mymod'
g:go_fmt_fail_silently = 1
g:go_imports_mode = 'gopls'
g:go_fmt_autosave = 1
g:go_jump_to_error = 1
g:go_loaded_gosnippets = 1
g:go_imports_autosave = 1

filetype detection configuration:

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

bhcleek commented Feb 6, 2021

This looks like a gopls bug. I've filed golang/go#44136.

bhcleek added a commit to bhcleek/vim-go that referenced this issue Feb 10, 2021
Order matters when formatting and adjusting imports, because of gopls'
support for gofumpt. Gofumpt formatting will group all imports that look
like a stdlib package (e.g. there's no '.' in the package path)
together. When the local setting is provided, the only way to get the
local imports grouped separately when gofumpt is used to format is to
format first and then organize imports.

Fixes fatih#3151
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

Successfully merging a pull request may close this issue.

2 participants