Cannot determine module path for source directory
Asked Answered
M

2

18

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?

Moppet answered 24/10, 2018 at 21:21 Comment(0)
C
4

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.

Clemmieclemmons answered 24/10, 2018 at 21:42 Comment(1)
I had the same problem ;(Hardspun
E
1

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 install
  go: cannot find main module, but found .git/config in /Users/xxxx/Documents/learn-terraform-lambda-api-gateway
to create a module there, run:
cd .. && go mod init
% go mod init
go: 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 module

Run 'go help mod init' for more information.
% go mod init example.com/m
go: creating new go.mod: module example.com/m
go: to add module requirements and sums:
go mod tidy
% go mod tidy
go: finding module for package github.com/aws/aws-lambda-go/lambda
go: found github.com/aws/aws-lambda-go/lambda in github.com/aws/aws- lambda-go v1.32.0
go: downloading github.com/stretchr/testify v1.6.1
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
% go install 
Elisabeth answered 22/6, 2022 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.