郭寰宇
郭寰宇

Reputation: 659

VS Code shows an error when having multiple Go Projects in a directory

When opening a directory in VS Code 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?

Upvotes: 63

Views: 103150

Answers (15)

Ah_Ch_97
Ah_Ch_97

Reputation: 79

It is not the perfect solution but it is good as temporay one In my case I had a react project and a go project opened under the same root what solved my problem is opening each project is a separate window.

Upvotes: 0

Adioz Daniel
Adioz Daniel

Reputation: 516

there are so many beautiful answers here! The shortest way is: simply open the terminal in Vs Code from your working directory and enter the following command in the root of your working directory: go work init This will initialize the working directory!

Upvotes: 1

Vibhu Pandey
Vibhu Pandey

Reputation: 188

Simply Open the terminal:-

go mod init poc

Here "poc" could be anything related to your project name

Upvotes: 4

suresh
suresh

Reputation: 1

go version go1.21.1 windows/amd64

please go to source code parent dir(parent dir of your modules)

go work init ( this will create go.work file in your root dir)

go work use mod1 ( it will add module1 to go.work file)

go work use mod2 (it will add the module2 to go.work file)

Upvotes: -1

Juned Raza
Juned Raza

Reputation: 245

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

enter image description here

Upvotes: 0

General Zod
General Zod

Reputation: 47

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

code .

this will open new VS Code in that folder

Upvotes: 2

Ashwin
Ashwin

Reputation: 53

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

Upvotes: 4

Jules ADONSI
Jules ADONSI

Reputation: 101

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

Upvotes: 10

Ebrahim
Ebrahim

Reputation: 1818

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

Upvotes: 3

sounish nath
sounish nath

Reputation: 681

This is working now...

"gopls": {
    "build.experimentalWorkspaceModule": true
}

Upvotes: 2

F1ko
F1ko

Reputation: 4224

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
)

Upvotes: 105

papillon88
papillon88

Reputation: 111

The setting has changed, now you need to use:

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

Upvotes: 9

Liam
Liam

Reputation: 29664

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

Upvotes: 4

Abhishek Nishad
Abhishek Nishad

Reputation: 579

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.

Upvotes: 57

Stefano Mozart
Stefano Mozart

Reputation: 1251

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

Upvotes: 88

Related Questions