47

When opening a directory in VSCode that consists of multiple Go projects the following error appears:

gopls requires a module at the root of your workspace.
You can work with multiple modules by opening each one as a workspace folder.
Improvements to this workflow will be coming soon (https://github.com/golang/go/issues/32394),
and you can learn more here: https://github.com/golang/go/issues/36899.

How can this be fixed?

F1ko
  • 3,326
  • 1
  • 9
  • 24
郭寰宇
  • 499
  • 1
  • 4
  • 3

11 Answers11

83

You probably have more than one go module in your workspace. If that is the case, you can change the go extension settings, in order to allow gopls to look for multiple modules in the workspace. Just add the following to your settings.json:

"gopls": {
    "experimentalWorkspaceModule": true,
}

You can read more about gopls configuration in the docs: https://github.com/golang/tools/blob/master/gopls/doc/settings.md

Stefano Mozart
  • 1,191
  • 10
  • 11
74

Go 1.18+

From Go 1.18 onwards there is native support for multi-module workspaces. This is done by having a go.work file present in your parent directory.

For a directory structure such as:

$ tree /my/parent/dir
/my/parent/dir
├── project-one
│   ├── go.mod
│   ├── project-one.go
│   └── project-one_test.go
└── project-two
    ├── go.mod
    ├── project-two.go
    └── project-two_test.go

Create and populate the file by executing go work:

cd /my/parent/dir
go work init
go work use project-one
go work use project-two

This will add a go.work file in your parent directory that contains a list of directories you marked for usage:

go 1.18

use (
    ./project-one
    ./project-two
)
Community
  • 1
  • 1
F1ko
  • 3,326
  • 1
  • 9
  • 24
  • 6
    This should be the accepted answer for Go1.18+ code issues. Here is the source for the answer https://github.com/golang/tools/blob/master/gopls/doc/workspace.md – Jasmeet Singh Dec 20 '22 at 20:38
  • 1
    this is amazing.. solved my problem for go v20 – lauc.exon.nod Feb 24 '23 at 16:15
  • From my experience, `go.work` support is not yet a fully baked feature as [not all Go tooling commands support go.work](https://github.com/golang/go/issues/50750) - I am currently figuring out how to use `go.mod` replacements instead. Reverting to the Go model before go.work appears simpler than fighting with go.work support. – RichVel Mar 23 '23 at 08:52
  • For VS Code, `/my/parent/dir` should be the workspace root. Ref: https://github.com/golang/vscode-go/issues/2534#issuecomment-1321922111 – Orca Apr 29 '23 at 13:46
54

To solve this, follow below steps :

step 1 : Open Vscode, and then go to settings.

step 2 : In the search bar , type gopls

step 3 : Just below that you will find settings.json, click on that

step 4 : Paste the below code their "gopls": { "experimentalWorkspaceModule": true, }

step 5 : Save it and restart the Vscode, You are good to go now.

Abhishek Nishad
  • 549
  • 4
  • 2
8

The setting has changed, now you need to use:

"gopls": {
    "build.experimentalWorkspaceModule": true,
}
Skatox
  • 4,237
  • 12
  • 42
  • 47
papillon88
  • 101
  • 1
  • 4
5

Just run go mod init your_module_mane. This command work for me.

Jules ADONSI
  • 51
  • 1
  • 2
3

I had this error because I had not created my modules inside a src directory. So I had:

$GOPATH
   -> bin
   -> pkg
   -> github.com
      -> Module
         -> go.mod

and that needed to be:

$GOPATH
   -> bin
   -> pkg
   -> src
       -> github.com
          -> Module
            -> go.mod
Liam
  • 27,717
  • 28
  • 128
  • 190
3

I just closed my vscode folder and re-opened it and it resolved the issue. no more action was required.

Ebrahim
  • 1,740
  • 2
  • 25
  • 31
  • 1
    Please try to suggest a more technical approach to the question as the proposed solution does not carry a sign of working the same on different devices – erPe Feb 15 '23 at 22:05
  • @erpe it was smart VS Code setting detection that resolved the issue! sorry no more feedback I remember. sometimes restarting your env can resolve the issue – Ebrahim Feb 16 '23 at 09:37
  • 1
    it showed also errors for me and they were only gone after i restarted my vscode ... – Jul Pod Mar 18 '23 at 07:54
2

This is working now...

"gopls": {
    "build.experimentalWorkspaceModule": true
}
sounish nath
  • 567
  • 4
  • 3
2

I installed the latest go "go version go1.20.5 linux/amd64" and fixed it by running the following command in my work directory

cd $<your work directory>
go work init
go work use ./tools/ ./tools/gopls/

Refer : https://github.com/golang/tools/blob/master/gopls/doc/workspace.md

Ashwin
  • 33
  • 6
0

Inside your project folder where main.go file is located in cmd type

code .

this will open new VS Code in that folder

General Zod
  • 27
  • 1
  • 5
0

Add go.work file in the root of the folder and define modules as below

enter image description here

Juned Raza
  • 228
  • 3
  • 6