fatih / vim-go

Go development plugin for Vim
https://www.patreon.com/bhcleek
Other
16.02k stars 1.45k forks source link

goimports removes hyphenated imports if project is symlinked into GOPATH #1237

Closed jmrodri closed 7 years ago

jmrodri commented 7 years ago

Behavior

With goimports enabled in .vimrc, a file with "github.com/op/go-logging" in its imports, saving the file will remove the import. Removing the import causes the project not to build. After much digging this only happens if the go project was SYMLINKED into the GOPATH.

Expected Behavior: When saving the file, I expect goimports to find the proper import and name it correctly instead of deleting it: logging "github.com/op/go-logging

Steps to reproduce:

Setup project with a symlink.

  1. enable goimports in .vimrc: let g:go_fmt_command = "goimports"
  2. mkdir -p /tmp/go/{src,pkg,bin}
  3. export GOPATH=/tmp/
  4. mkdir -p /tmp/mycode
  5. cd /tmp/mycode
  6. git clone https://github.com/fusor/ansible-service-broker.git
  7. mkdir -p $GOPATH/src/github.com/fusor/
  8. ln -s /tmp/mycode/ansible-service-broker/ $GOPATH/src/github.com/fusor/ansible-service-broker
  9. cd $GOPATH/src/github.com/fusor/ansible-service-broker
  10. vi ./pkg/broker/broker.go
  11. NOTICE that there is a "github.com/op/go-logging" import.

    import (                                                   
    "fmt"                                                  
    
    "github.com/fusor/ansible-service-broker/pkg/ansibleapp"
    "github.com/fusor/ansible-service-broker/pkg/dao"      
    "github.com/op/go-logging"                             
    "github.com/pborman/uuid"                              
    )                                                          
  12. :w (save the file)
  13. NOTICE that the "github.com/op/go-logging" import was removed.

    import (                                                   
    "fmt"                                                  
    
    "github.com/fusor/ansible-service-broker/pkg/ansibleapp
    "github.com/fusor/ansible-service-broker/pkg/dao"      
    "github.com/pborman/uuid"                              
    )                                                          

    Configuration

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

" vim-go settings
let g:go_highlight_functions = 1                  " enable function highlighting
let g:go_highlight_methods = 1                    " enable method highlighting
let g:go_highlight_fields = 1                     " enable fields highlighting
let g:go_highlight_types = 1                      " enable types highlighting
let g:go_highlight_operators = 0                  " disable operator highlighting
let g:go_highlight_build_constraints = 1          " enable build constraints highlighting
let g:go_fmt_command = "goimports"                " enable goimports to insert import paths
"let g:go_fmt_options = "-v"
let g:go_gocode_propose_builtins = 1
let g:neocomplete#enable_at_startup = 1
let g:go_hightlight_trailing_whitespace_error = 0 " doo

" vim-go keymappings
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)
fatih commented 7 years ago

Hi @jmrodri

Can you please create a simple go package that imports the subfolder you mention? I'm not sure the problem is vim-go, we just use goimports and nothing else. Thanks

jmrodri commented 7 years ago

@fatih the problem is that vim-go uses fnamemodify(a:target, ":p") which passes in the full name of the file. Now under 99% of the cases this works, except for my case because I symlink my project into the GOPATH. So fnamemodify will return the original path of the file which is outside the GOPATH. goimports won't work that way.

If I change vim-go to not use fnamemodify everything works both with my project being a symlink and with my project being directly in GOPATH.

I created a PR with the fix: https://github.com/fatih/vim-go/pull/1238

Apparently the test failed, so I'll look over that.

fatih commented 7 years ago

This is now fixed in master. Thanks!