In this tutorial, we will learn about how to fix the error “gopls was not able to find modules in your workspace”. I came across this issue while I was setting up my Golang project structure for developing a project. This is a generic Golang error which mostly says that gopls (official Golang server) is unable to recognize the new module that has been created in current workspace. There could be multiple reasons for this error. We will talk about some possible common reasons because of which this error occurs. After that, we will discuss few solutions to fix this error in this tutorial. So, let us get started.
Why the error occurred?
Before we jump into fixing the error. It is quite important to understand why even the error occurred. Did we do anything wrong while creating the project structure or while developing the code? Let’s dive in and understand the possible causes.
The error message “gopls was not able to find modules in your workspace” typically occurs when the Golang Server (gopls) cannot locate any Go modules within the current workspace. This could happen due to several reasons:
- Missing or Incorrect go.mod file: The most common reason is that the current directory doesn’t contain a
go.mod
file or it’s malformed.go.mod
is the Go module definition file, and it’s necessary for gopls to understand the module structure. - Incorrect Workspace Configuration: Sometimes, the IDE or editor workspace might not be properly configured to include the directory containing the
go.mod
file or might include directories that don’t contain Go modules. - Network or Proxy Issues: If the Go modules are supposed to be downloaded from the internet, but there are network issues or if a proxy is required for internet access, gopls might fail to retrieve the necessary module information.
Next, we will reproduce the error so that it gives you more visibility about when the error occurs. To do so, we will create a Golang project structure which will look like below.
[email protected] ~ % tree Milestone02 Milestone02 ├── main.go ├── src │ └── exercise05 │ └── openapi.go └── test 4 directories, 3 files
In the above tree,
Milestone02
is the main directory.main.go
is a file withinMilestone02
, belonging to themain
package.src
is a directory withinMilestone02
, but it’s not a package.exercise05
is a directory withinsrc
and represents a Go package.openapi.go
is a file withinexercise05
, belonging to theexercise05
package.test
is a directory withinMilestone02
, potentially containing test-related files, but we cannot determine if it’s a Go package without further inspection.
After you create the tree structure, add ‘package exercise05’ in openapi.go file. The moment you add the package, it will report the error as shown below.
NOTE:
brew install tree
. To install tree package on CentOS/RHEL, use command dnf install tree
.
[Solved] gopls was not able to find modules in your workspace
Also Read:[Solved] How to Fix the Error .\main.go: undefined in Golang
So far, we have understood about different reasons because of which the given error occurs. We also reproduced the error by creating a project structure. Let us now talk about different solutions which can fix the error. There are three solutions provided in this tutorial. These solutions had helped me to fix the error. Let’s look at them on by one in below section.
Solution-1: Create go.mod File
The error is clearly saying that it is not able to locate the module in current workspace. This is because, we have not initialized the go.mod file for the module in current workspace. So, to fix the error, create a new go.mod file at the root level. In this case, Milestone02 is the module name so we will create go.mod file at this level using the command go mod init <module-name>
as shown below.
[email protected] ~ % go mod init github.com/linuxnasa/milestone02
You will now see go.mod file created. This will resolve the error as gopls will now recognize the module. Now the tree will look like below.
[email protected] ~ % tree Milestone02 Milestone02 ├── go.mod ├── main.go ├── src │ └── exercise05 │ └── openapi.go └── test 4 directories, 3 files
Solution-2: Create go.work File
If the first solution did not fix the error, try this solution. From Go 1.18 onwards there is native support for multi-module workspaces which means if you have created more than one module in the same workspace, you can use those modules by initializing the go.work
file in your parent directory (Milestone02 in our case). So, create a go.work file at root level using the command go work init
as shown below.
First, Switch to the directory Milestone02
[email protected] ~ % cd Milestone02
Next, Create go.work file in the parent directory.
[email protected] Milestone02 % go work init
After creating the go.work file, now the error will be gone and the tree will look something like below.
[email protected] ~ % tree Milestone02 Milestone02 ├── go.mod ├── go.work ├── main.go ├── src │ └── exercise05 │ └── openapi.go └── test 4 directories, 4 files
Solution-3: Check Environment Variables
If none of the solution given above worked, Check and ensure that your GOPATH
and GO111MODULE
environment variables are properly set. GO111MODULE
should be set to “on” or “auto” for Go module support.
Summary
We have successfully resolved the error by using one of the solutions given in this tutorial. To know more about any Golang packages, check out go package which is Golang’s official documentation.