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

GoErrCheck doesn't work in projects inside secondary GOPATH #1151

Closed
gunnihinn opened this issue Dec 23, 2016 · 2 comments · Fixed by #1321
Closed

GoErrCheck doesn't work in projects inside secondary GOPATH #1151

gunnihinn opened this issue Dec 23, 2016 · 2 comments · Fixed by #1321
Labels

Comments

@gunnihinn
Copy link

gunnihinn commented Dec 23, 2016

Behavior

Running :GoErrCheck in a project that lives in a secondary directory in GOPATH fails.

Steps to reproduce:

Suppose we've setup our GOPATH to have more than one directory, perhaps like this:

GOPATH="$HOME/work/go:$HOME/golang"

If we now start a project in the first of these, say "$HOME/work/go/src/hello", then running :GoErrCheck fails with the error message

import "$HOME/work/go/src/hello": cannot import absolute path
error: failed to check packages: could not type check: no initial packages were loaded

Thoughts on the problem

As far as I can tell, the problem is in the call to go#package#ImportPath in go#lint#Errcheck. The for loop in there always assigns workspace to the last of the dirs in GOPATH that passed the if check, and then assumes the project lives inside that directory. It then fails to dig the package name out of the absolute path. In our example, it tries to find the package name by deleting

$HOME/golang/src/

from

$HOME/work/go/src/hello

This of course deletes nothing, so we try to call

errcheck $HOME/work/go/src/hello

which fails with the error message above.

Configuration

Add here your current configuration and additional information that might be
useful, such as:

  • vimrc you used to reproduce:

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

  • vim version: Neovim 0.1.6

  • vim-go version: Git commit e46dd4d from December 17, 2016

  • go version: go1.7.3 darwin/amd64

@gunnihinn
Copy link
Author

gunnihinn commented Dec 23, 2016

I can get :GoErrCheck to run by modifying vim-go/autoload/go/package.vim as follows:

@@ -59,7 +59,7 @@ function! go#package#ImportPath(arg) abort
    let dirs = go#package#Paths()

    for dir in dirs
-        if len(dir) && matchstr(escape(path, '\/'), escape(dir, '\/')) == 0
+        if len(dir) && match(path, dir) == 0
              let workspace = dir
          endif
    endfor

One problem seems to be that matchstr returns a string and not an integer; match returns the position in the first argument where the second one first matched. Based on the == 0 check, this is probably what was intended.

Another problem is that Neovim won't match any of the paths once they've been escaped (including the one where the project lives): If I save the following to "match.vim"

let a="/foo"
let b="/foo"

if match(a, b) == 0
    echom "Matched"
else
    echom "Didn't match"
endif

if match(escape(a, '\/'), escape(b, '\/')) == 0
    echom "Matched"
else
    echom "Didn't match"
endif

and run it with :so match.vim it prints

Matched
Didn't match

which seems kind of weird. This happens in both Vim and Neovim on my machine.

@fatih
Copy link
Owner

fatih commented Jun 7, 2017

Sorry for the very late response. This is fixed now and pushed to master. I'm using now by default go list which does all the heavy work and is much more reliable. Thanks for the feedback.

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

Successfully merging a pull request may close this issue.

2 participants