Cannot make gopls work with emacs in module mode

734 views
Skip to first unread message

Xdkkz Lpwjd

unread,
Aug 3, 2020, 12:59:08 AM8/3/20
to golang-tools
Hi everyone,

My go version is 1.14.6, and the GO111MODULE is unset. If it is GOPATH mode, gopls works fine, but if in module mode, it doesn't work at all on my macbook, here is what I do:

Create the following layout outside of GOPATH, 
        ~/projects-go
            /hello
                /foo
                     foo.go
                hello.go
                go.mod

hello.go:
package hello

import (
   
"example.com/hello/foo"
)

// Hello ...
func
Hello() string {
    foo
.Foo()
   
return "Hello, world."
}

foo.go:
package foo

import "fmt"

func
Foo() {
    fmt
.Println("Foo")
}

go.sum:
module example.com/hello


go
1.14

When I open hello.go in emacs, and hover the mouse to the import line, emacs shows the following message in mini buffer after a few minutes:
could not import example.com/hello/foo (no package for import example.com/hello/foo)


I tried to find any debug message why it cannot import the package with following settings, but no luck. I open the page http://localhost:8080, no useful message, just some memory info, rpc, trace, etc. The file gopls.log was created, but it is empty
(use-package lsp-mode
 
:ensure t
 
:init
 
(setq lsp-gopls-server-args '("-debug" ":8080" "-vv" "-logfile" "/Users/foobar/gopls.log"))
  :commands (lsp lsp-deferred)
  :hook (go-mode . lsp-deferred)
  :config
  ;; Set up before-save hooks to format buffer and add/delete imports.
  ;; Make sure you don'
t have other gofmt/goimports hooks enabled.
 
(defun lsp-go-install-save-hooks ()
   
"Comment lsp-go-install-save-hooks."
   
(add-hook 'before-save-hook #'lsp-format-buffer t t)
   
(add-hook 'before-save-hook #'lsp-organize-imports t t))
 
(add-hook 'go-mode-hook #'lsp-go-install-save-hooks)
 
)


Thank you very much

Muir Manders

unread,
Aug 3, 2020, 1:19:08 AM8/3/20
to 'Jay Conrod' via golang-tools
First I invite you to join #emacs on the Gophers' slack (https://invite.slack.golangbridge.org/), home to some of the world's foremost experts on using gopls in Emacs.

Regarding your issue, I would guess your lsp-mode workspace folder isn't configured properly. Your workspace folder should be the folder containing your go.mod (i.e. ~/projects-go/hello). You can run `M-x lsp-describe-session` to see your workspace folder(s), then use `M-x lsp-workspace-folders-remove` and `M-x lsp-workspace-folders-add` as needed.

Muir
--
You received this message because you are subscribed to the Google Groups "golang-tools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-tools...@googlegroups.com.

leopold.fr...@gmail.com

unread,
Aug 3, 2020, 1:24:31 AM8/3/20
to golang-tools
It works, thank you very much
Reply all
Reply to author
Forward
0 new messages