DisposaBoy / GoSublime

A Golang plugin collection for SublimeText 3, providing code completion and other IDE-like features.
MIT License
3.42k stars 302 forks source link

Goimports doesn't work in GoSublime anymore. #773

Closed taggedboy closed 5 years ago

taggedboy commented 7 years ago

I whenever installed Sublime Text on a new computer I followed this tutorial and Goimports worked automatically in GoSublime. But the last time I installed it, it's not working anymore.

nasitra commented 7 years ago

If you use on Windows, there is something wrong with goimports.

Fix %GOPATH%/src/golang.org/x/tools/imports/fix.go

--- a/fix.go
+++ b/fix.go
@@ -254,10 +254,12 @@ func fixImports(fset *token.FileSet, f *ast.File, filename string) (added []stri
        results := make(chan result)
        for pkgName, symbols := range refs {
                go func(pkgName string, symbols map[string]bool) {
-                       sibling := packageInfo.Imports[pkgName]
-                       if sibling.Path != "" {
-                               results <- result{ipath: sibling.Path, name: sibling.Alias}
-                               return
+                       if packageInfo != nil {
+                               sibling := packageInfo.Imports[pkgName]
+                               if sibling.Path != "" {
+                                       results <- result{ipath: sibling.Path, name: sibling.Alias}
+                                       return
+                               }
                        }
                        ipath, rename, err := findImport(pkgName, symbols, filename)
                        r := result{ipath: ipath, err: err}

Then reinstall goimports

go install golang.org/x/tools/cmd/goimports
taggedboy commented 7 years ago

Thank You @nasitra! It worked for me. Did you push this change to the GoSublime?

nasitra commented 7 years ago

The change merged into 'golang/tools', not GoSublime.

https://github.com/golang/tools/commit/6e57528ade4c045edb91dac8af1c4a3ab30fa738

Now, we don't need to change the code locally. You can get the latest goimports program and update it.

go get -u golang.org/x/tools/cmd/goimports