Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot determine module path for source directory

Tags:

go

go-modules

I have go.mod file inside root/src/abc. And in root/build-scripts I have a script which does go get. As I am using Go 1.11 I have not used the go path instead the mod file in root/src/abc takes care of other imports except for the packages that are being used in build script which gives error:

go: cannot determine module path for source directory.

Any suggestions?

like image 955
Batman Rises Avatar asked Oct 24 '18 21:10

Batman Rises


People also ask

What does go mod init do?

The go mod init command creates a go.mod file to track your code's dependencies. So far, the file includes only the name of your module and the Go version your code supports. But as you add dependencies, the go.mod file will list the versions your code depends on.

Where does go look for modules?

The go command starts by searching the build list for modules with paths that are prefixes of the package path. For example, if the package example.com/a/b is imported, and the module example.com/a is in the build list, the go command will check whether example.com/a contains the package, in the directory b .

How do I make a go module?

Creating a Go Module First, we need to create a folder inside the GOPATH src directory. Then we for simplicity's sake add two files. One is general go code and the other is a testing file. Now, we run go test to see the result.

Where is go mod file?

What is Go Module. A Module is a collection of Go packages stored in a file tree under $GOPATH/pkg folder with a go. mod file at its root. This file defines the module's path, which is also the import path used for your project, and its dependency requirements, which are the other modules needed for a successful build.


1 Answers

It's hard to say anything with certainty without seeing the actual commands you run, by it seems your scripts do not change the working directory, and therefore the go commands they execute are not in the module's root folder or any of its subfolders.

Quoting from Command Go: The go.mod file:

A module version is defined by a tree of source files, with a go.mod file in its root. When the go command is run, it looks in the current directory and then successive parent directories to find the go.mod marking the root of the main (current) module.

So your scripts should change the working directory to root/src/abc or any of its subfolders, else the go command will not find the go.mod file.

like image 182
icza Avatar answered Sep 23 '22 06:09

icza