VSCode: Could not import Golang package
Asked Answered
B

17

72

I'm writing a Go project inside my GoPath, and i'm using the Redigo package for connecting to a Redis Server. The application runs fine, however in VSCode there is this annoying error on package import, which is preventing VSCode from giving intellisense suggestions

Could not import github.com/gomodule/redigo/redis (no package data for import path github.com/gomodule/redigo/redis)

This is my VSCode settings.json

{
    "editor.fontSize": 14,
    "editor.formatOnPaste": true,
    "editor.multiCursorModifier": "ctrlCmd",
    "editor.snippetSuggestions": "top",
    "extensions.ignoreRecommendations": false,
    "workbench.statusBar.visible": true,
    "workbench.iconTheme": "vscode-great-icons",
    "files.autoSave": "afterDelay",
    "go.useLanguageServer": true,
    "go.alternateTools": {
        "go-langserver": "bingo"
    },
    "go.toolsEnvVars": {
        "GO111MODULE": "on"
    },
    "go.languageServerExperimentalFeatures": {
        "autoComplete": true,
        "documentSymbols": true,
        "findReferences": true,
        "format": true,
        "goToDefinition": true,
        "goToTypeDefinition": true,
        "hover": true,
        "signatureHelp": true,
        "rename": true,
        "workspaceSymbols": true,
    },
    "go.lintTool": "golangci-lint",
    "go.lintFlags": [
        "--fast",
        "-E", "goimports",
        "-E", "gocritic",
        "-E", "gocyclo",
        "-E", "gosec",
        "-E", "maligned",
        "-E", "scopelint",
        "-E", "interfacer",
        "-E", "goconst",
        "-E", "unconvert",
        "-E", "unparam",
        "-E", "prealloc",
        "-E", "varcheck",
    ],
    "go.formatTool": "goimports",
    "editor.minimap.enabled": false,
    "breadcrumbs.enabled": false,
    "git.autofetch": true,
    "workbench.startupEditor": "newUntitledFile",
    "explorer.confirmDelete": false,
    "git.enableSmartCommit": true,
    "git.confirmSync": false,
    "window.zoomLevel": 0,
    "explorer.confirmDragAndDrop": false
}

I already have the GO111MODULE env var set to on, this is the output of go env

set GOARCH=amd64
set GOBIN=C:\Users\Francesco\Go\bin
set GOCACHE=C:\Users\Francesco\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Francesco\Go
set GOPROXY=
set GORACE=
set GOROOT=c:\go
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\Francesco\Go\src\test\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\FRANCE~1\AppData\Local\Temp\go-build928398422=/tmp/go-build -gno-record-gcc-switches

What should I change to make this work?

Breakaway answered 23/10, 2019 at 8:25 Comment(0)
I
188

This happens to me in a few specific situations. This is my troubleshooting process :

  1. Did you run go get github.com/gomodule/redigo/redis?

  2. Sometimes I have a similar issue when I open my editor in a root different than my project. (also check the updates at the end of the answer)

.  <- editor open here
| 
|_Folder
  | main.go
  | go.mod
  | go.sum
  1. Make sure your tools are up to date: run ctrl + shift + p, type Go and chose Install/Update tools.

  2. Try moving your project out of the GOPATH, and setting up go.mod for it.

  3. Restart the editor

Update for those with the issue "2":

Go 1.18 has a new feature called workspace! If you are opening your workspace in a root different than where your go mod files are, it is probably because you have multiple projects in the same folder. If that is the case, you can run:

go work init
go work use ./path-to-module  ./path-to-module2

What it looks like:

.  <- editor open here
| go.work
| app (folder)
  | go.mod
  | go.sum
  | main.go
| client (folder)
  | go.mod
  | go.sum
  | main.go
Illative answered 23/10, 2019 at 13:52 Comment(10)
Looks like updating has solved the issue. I did the go get command before, and the editor was open in the root project folder. I'll try to update the go tools more frequently i guess :PBreakaway
this issue has been bugging me for over a year, I've tried everything and your second point about opening the editor in root was the solution I least expected to work, but it did - many thanksMonochromatic
second part of opening the editor one folder above did the trick for me as wellMercantilism
This is the definitive solution for developers using VSC with Go modules! Like Hom Bahrani, I was in a similar situation. Videos on Go and modules did not provide details on how to resolve this issue. Thanks!Tellurium
My issue was exactly the point 2: having opened VSCode on the root of a larger project with many go.mod files inside. Opening the editor workspace into one at the time solved it for meFolio
Same for me - I need vscode to have the root folder of the project open. Is there no way for us to be able to have multiple go.mod projects open at once?As
Resolved ! I open VSCode as Open Folder at where main.go and go.mod are. IntelliSense can locate the place where a local module is e.i ...../mod/pkg/<module_name>Institutionalize
#3 worked for me - for whatever reason, updating the tools removed it. Many thanks!Doggish
for me, open folder at the project root does not work, i ended up created a folder wrap the current folder, and open the VSC from newly created one, works like charm....Gerenuk
The update worked like a charmSonata
A
38

I had a same sort of issue with a different package (on mac),

  1. Update the go tools - ctrl + shift + p or cmd + shift + p and update/install go tools
  2. Restart VScode

Issue solved

Aruabea answered 19/11, 2020 at 23:40 Comment(2)
Thank you. now name is "Go: install/Update Tools".Bergstrom
Thanks, after update, it gives more clear error message, telling me I should put my project inside GOPATH, I can import local package without error in VScodeLaruelarum
S
7

I encountered a similar problem, but the situation is slightly different. I encountered this problem in VSCode remote, and even the basic library like net/http cannot be imported. I found that it was because of some syntax errors in other files under the same package, such as undefined variables. When I solved these syntax errors and restarted VSCode, I found that the problem was solved.

Straightlaced answered 22/7, 2020 at 6:31 Comment(0)
S
6

go mod init package_name followed by go mod tidy got things working for me. go mod tidy installed the packages and updated the mod file.

Schuler answered 20/10, 2021 at 5:56 Comment(0)
S
4

Doing a full restart of vsCode fixed the issue for me.

Note cmd + shift + p -> Developer: Reload Window did NOT fix it, only a full close and re-opening of VS code

Savoie answered 2/8, 2022 at 14:13 Comment(0)
W
2

I ran into this issue when I had two files in one package that had different package names. Eg: one file had package main while the other had package app.

Ensuring all files have the same package name will result in imports across the package to start working again.

Womanlike answered 5/10, 2020 at 20:37 Comment(0)
G
2

This worked for me:

  1. File/New Window
  2. File/Add Folder To Workspace ...
Goldy answered 26/10, 2022 at 20:45 Comment(0)
S
1

I've installed on Fedora 35 gcc-go and after I uninstalled it all worked fine.

sudo dnf remove gcc-go
Sides answered 29/12, 2021 at 18:39 Comment(0)
G
1

My problem was I had the root folder (My Parent folder) which was the parent of my Go project folder open in VSCode

Ie,

My Parent folder
    My Go Project folder

Instead of opening the parent folder (My Parent folder) you could just open the project folder (My Go Project folder)

In my case, I needed access to files in both folders, so I added both parent & child folders to the VSCode workspace as per here and this got rid of the errors

Geotaxis answered 20/2, 2023 at 3:16 Comment(0)
B
1

I had the same symptoms. In my case, the root cause was the missing "CGO_ENABLED=1". Cause I had some "C" code in the package.

Barbershop answered 8/5, 2023 at 19:9 Comment(0)
M
0

I just recently ran into this same error, my solution was ensuring that my go version of my project's mod file was the same version as the installed version on my system.

Minter answered 1/9, 2021 at 2:51 Comment(0)
W
0

I had the same problem it was from go mod init packegeName and a named my package 'time' and the editor give me err so I simply replace it with 'timee' i do not know whats happen actually but it's working fine

Watermark answered 9/12, 2021 at 7:23 Comment(1)
When posting answers to stackoverflow, it's important to keep them concise and brief -- stackoverflow is not a forum to post messages, it's a Q&A site.Spectroheliograph
B
0

if you are using Go 1.18+, here is a new feature for multi-module workspaces, once you setup the go.work file at you root project folder, the issues will no longer exist, you can check the following links for more details

  1. https://mcmap.net/q/275800/-go-linter-in-vs-code-not-working-for-packages-across-multiple-files
  2. https://github.com/golang/tools/blob/master/gopls/doc/workspace.md
Baluchi answered 8/6, 2022 at 16:42 Comment(0)
S
0

Ran into the same issue, reinstalled go with goenv, and then i ran some go mod whys to check if everything is in order and it was

Snuck answered 26/8, 2022 at 22:57 Comment(0)
B
0

Setting up your workspace

gopls supports both Go module and GOPATH modes. However, it needs a defined scope in which language features like references, rename, and implementation should operate.

The following options are available for configuring this scope:

Module mode

One module

If you are working with a single module, you can open the module root (the directory containing the go.mod file), a subdirectory within the module, or a parent directory containing the module.

Note: If you open a parent directory containing a module, it must only contain that single module. Otherwise, you are working with multiple modules.

Multiple modules

Gopls has several alternatives for working on multiple modules simultaneously, described below. Starting with Go 1.18, Go workspaces are the preferred solution.

Go workspaces (Go 1.18+)

Starting with Go 1.18, the go command has native support for multi-module workspaces, via go.work files. These files are recognized by gopls starting with [email protected].

The easiest way to work on multiple modules in Go 1.18 and later is therefore to create a go.work file containing the modules you wish to work on, and set your workspace root to the directory containing the go.work file.

For example, suppose this repo is checked out into the $WORK/tools directory. We can work on both golang.org/x/tools and golang.org/x/tools/gopls simultaneously by creating a go.work file:

cd $WORK
go work init
go work use tools tools/gopls

...followed by opening the $WORK directory in our editor.


Source

Burnard answered 25/10, 2022 at 15:24 Comment(1)
I noticed go work use dirName may not work if dirName fails silently if it doesn't have a go.mod fileBurnard
M
0

If this is a repo that you have pulled from Github, and you are faced with the errors of a package/s could not be imported and have a go.mod in the repo, I did the following to fix:

  1. Open terminal and navigate to the root of the project where your go.mod and go.sum are located and run:
go mod download

Which as the go help menu outlines that the command: downloads modules to local cache

  1. Inside VS Code open the command palette with whatever command that you have for your architecture. For mine command on mac I use control + shift + p and search for Go: Install/Update tools. Press enter and update your go tooling.

After that my main.go updated with packages that I was missing without explicitly running go install thingVSCodeSaidIWasMissing

Marchelle answered 30/5, 2023 at 19:53 Comment(0)
A
0

I'm newbie with Golang, I don't know the consequencies of these commands in bigger project.

In my case, the problem was that I moved the probject to a subfolder. I had to delete go.mod and go.sum, and do go mod init package_name followed by go mod tidy

Achromatous answered 3/2 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.