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?
Best Answer
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.
According to Go documents, go get
is deprecated since Go 1.17. However, running command go install
may give similar errors. Here is my case. When I ran go install
in a directory where there' s a go.mod
file, the error was gone. You may try to create a new go.mod
and check whether or not the issue is there.
% go installgo: cannot find main module, but found .git/config in /Users/xxxx/Documents/learn-terraform-lambda-api-gatewayto create a module there, run:cd .. && go mod init% go mod initgo: cannot determine module path for source directory /Users/xxxx/Documents/learn-terraform-lambda-api-gateway/hello-world (outside GOPATH, module path must be specified)Example usage:'go mod init example.com/m' to initialize a v0 or v1 module'go mod init example.com/m/v2' to initialize a v2 moduleRun 'go help mod init' for more information.% go mod init example.com/mgo: creating new go.mod: module example.com/mgo: to add module requirements and sums:go mod tidy% go mod tidygo: finding module for package github.com/aws/aws-lambda-go/lambdago: found github.com/aws/aws-lambda-go/lambda in github.com/aws/aws- lambda-go v1.32.0go: downloading github.com/stretchr/testify v1.6.1go: downloading github.com/davecgh/go-spew v1.1.1go: downloading github.com/pmezard/go-difflib v1.0.0go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776% go install