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

Go extension cannot recognize local go module. #529

Closed
Aetherbase opened this issue Aug 15, 2020 · 4 comments
Closed

Go extension cannot recognize local go module. #529

Aetherbase opened this issue Aug 15, 2020 · 4 comments

Comments

@Aetherbase
Copy link

Aetherbase commented Aug 15, 2020

What version of Go, VS Code & VS Code Go extension are you using?

go version go1.15 linux/amd64
vscode 1.48.0 (using remote-ssh 0.51.0)
go extension 0.16.1
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build715886316=/tmp/go-build -gno-record-gcc-switches"

Share the Go related settings you have added/edited

    "go.formatTool": "goimports",
    "go.useLanguageServer": true,
"[go]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true,
    },
    // Optional: Disable snippets, as they conflict with completion ranking.
    "editor.snippetSuggestions": "none",
},
"[go.mod]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true,
    },
},
"gopls": {
     // Add parameter placeholders when completing a function.
    "usePlaceholders": true,

    // If true, enable additional analyses with staticcheck.
    // Warning: This will significantly increase memory usage.
    "staticcheck": false,
}

Describe the bug

Go extension cannot recognize go packages in go modules. Suggestions dont work either.Compiling and running the code works.
Expected auto suggestions support for go modules.

Steps to reproduce the behavior:

  1. Create a mod inside a folder in the workspace using go mod init <go-mod-example>
  2. Create a package <greet> folder inside the module and create a function in go file <greet.go> inside it, specifying <greet> package
  3. in main.go in mod folder, try importing <go-mod-example/greet> package and call the function created in <greet.go>
  4. Go extension will show that the package cannot be imported.
    could not import go-mod-example/greet (no package for import go-mod-example/greet)

Screenshots or recordings

Package code:
Annotation 2020-08-15 150206
Main code with problem:
Annotation 2020-08-15 150257
Main code with output of go run main.go:
Annotation 2020-08-15 150405

@hyangah
Copy link
Contributor

hyangah commented Aug 15, 2020

@Aetherbase The go command treats them completely separate modules and tries to find go-mod-example/greet from its module cache & goes to the network even though both are in the same repo. There is a lengthy discussion in https://github.com/golang/go/wiki/Modules#faqs--multi-module-repositories.

A suggested workaround is to use the replace directive in go.mod file. Can you try go mod edit -replace go-mod-example/greet=./greet from the main module and see if it helps?

@stamblerre
Copy link
Contributor

stamblerre commented Aug 15, 2020

It looks like your module is not at the root of your workspace, and gopls currently requires you to open a directory that is at or below your module root. If you want to open your top-level directory instead, you can add the module as a workspace folder (View -> Add Folder to Workspace).

@Aetherbase
Copy link
Author

Aetherbase commented Aug 17, 2020

Thank you very much for the suggestions.
@hyangah using replace didnt work. Building was successful prior to using replace.
@stamblerre I opened the folder at the module root, and autosuggest started working. Thanks alot! Maybe include this in the documentation? I tried looking for how to use go mods with the extension but didnt find anything related.

@hyangah
Copy link
Contributor

hyangah commented Aug 17, 2020

@Aetherbase That's strange but without knowing actual directory layout and how you used redirect, I don't know what's going on.

The pinned #275 issue describes the current gopls limitation. You can add to https://github.com/golang/vscode-go#language-server section. PR is welcome.

@golang golang locked and limited conversation to collaborators Aug 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants