Cannot determine module path for source directory - go

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?

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 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

Related

Go mod tidy find module but it's not getting to go.sum

i have a Revel project and i can't start it because everytime i run go mod tidy, it looks like finding module but it's not putting the found module in go.sum
here's my go env
GOENV = C:\Users\mycomp\AppData\Roaming\go\env
GOMOD = C:\Program Files\Go\src\myproject\go.mod
GOPATH = C:\Users\micha\go
go version
go version go1.18.3 windows/amd64
revel version
Revel executing: displays the Revel Framework and Go version
Revel Framework : Unknown (1.1.0 remote master branch)
Revel Cmd : 1.1.2 (1.1.2 remote master branch)
Revel Modules : Unknown (1.1.0 remote master branch)
go mod tidy
go: finding module for package github.com/PaesslerAG/jsonpath
go: finding module for package github.com/tdewolff/test
go: finding module for package github.com/PaesslerAG/gval
go: found github.com/tdewolff/test in github.com/tdewolff/test v1.0.7
go: found github.com/PaesslerAG/gval in github.com/PaesslerAG/gval v1.2.0
go: found github.com/PaesslerAG/jsonpath in github.com/PaesslerAG/jsonpath v0.1.1
go mod init myproject seem working because it create go.mod but it's just odd because it only creates this line, usually it creates a lot of modules i need
module myproject
go 1.18
then when i type go mod tidy, it creates empty go.sum. What do i miss ?
Cause the problem is too complex to reproduce online, i started to debug and reinstall my Go from scractch, between solving a bug where my program can't run i tried several things that might be useful
after Go 1.15 or 1.17 , my Go project can't run properly, so i put it in C:\Program Files\Go\src\<project_name>
The problem that i have is in GOPATH (usually in C:\Users\<comp_name>\go) don't have src folder, that makes it found downloaded module but can't seem to put it in go.mod inside my project
So in the end i reinstall all my Go and Revel. Here's how to delete all your associated Go to really really delete all your files
Delete Go Folder in C:\Program Files\Go\
Delete go folder in C:\Users\<comp_name>\go
View all hidden files and delete folder go-build in C:\Users\<comp_name>\AppData\Local
View all hidden files and delete folder go if any in C:\Users\micha\AppData\Roaming

go: module found but does not contain package

I am trying to install the net package for go but get "does not contain package error".
Terminal screenshot:
I have consulted: go module #latest found but does not contain package but none of the solutions seem to work for me.
I am using go version go1.18.5 linux/amd64
You have to initialize your module with go mod init in the project root directory
For local codebase
go mod init test
OR for hosted codebase e.g. github repo: test, github user: radiant
go mod init github.com/radiant/test
It will produce a go.mod file.
Then you can get the required package as:
go get golang.org/x/net
go mod tidy
Then import and use the net packages.
Hope this helps.
go get -u golang.org/x/net
go install used for executable packages

Go get behaving erratically during build and with VScode

Using Go version go1.17.1 linux/amd64 on my CentOS 7. GOPATH and GOROOT are set in my .bashrc in the order:
export GOPATH="${HOME}/.go"
export GOROOT="/usr/local/go"
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"
Got my project in /home/wsb/_projects/local/parlance:
[wsb#localhost local]$ pwd
/home/wsb/_projects/local
[wsb#localhost local]$ tree
.
└── parlance/
└── main.go
└── utils.go
From the terminal, using go get stores src in /home/wsb/.go/pkg/mod/cache... folder
[wsb#localhost parlance]$ go get -v -u github.com/gorilla/mux
go: downloading github.com/gorilla/mux v1.8.0
github.com/gorilla/mux
[wsb#localhost parlance]$ go build main.go
main.go:3:8: no required module provides package github.com/gin-gonic/gin: go.mod file not found in current directory or any parent directory; see 'go help modules'
[wsb#localhost parlance]$
[root#localhost /]# find -iname gin
./home/wsb/.go/pkg/mod/cache/download/github.com/gin-gonic/gin
Why would it throw error on build?
Using go mod is the key here for managing packages for any particular projects.
go mod is used for installing and tracking external packages outside of packages in GOPATH.
From within existing project root directory parlance we initialize go mod using:
[wsb#localhost parlance]$ go mod parlance
It creates two files within the project root directory: go.mod and go.sum.
Now we can go get any module we will use specifically for our project.
go get github.com/gin-gonic/gin
go get github.com/gofiber/fiber/v2
This creates external package requirement details in go.mod and their respective checksum in go.sum file.
Finally, we can build/run our package like
go build main.go
go run main.go

go module #latest found but does not contain package

I'm trying to make use of go module for the first time. What exactly the following error message is telling me?
module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
It happens during go build, whereas go get is just fine:
$ go get -v github.com/mkideal/cli
go: github.com/mkideal/cli upgrade => v0.2.2
but not go get -v ./..., which gave me the same error as above. My proxy setting looks OK:
$ go env | grep GOPROXY
GOPROXY="https://proxy.golang.org,direct"
Is it a problem of the go module/package I'm trying to use, or my own code's problem? -- I took a look at
https://github.com/mkideal/cli/blob/master/go.mod and it seems fine to me.
See the following update for details.
How can I overcome the situation? (I'm getting the same error message for my own repo as well)
UPDATE:
Here is the full log how I'm getting the above error:
prepare /tmp/015-file from https://github.com/mkideal/cli/blob/master/_examples/015-file
do go mod init
then go build
Now the details:
$ cd /tmp/015-file
$ GO111MODULE=on
$ go mod init github.com/mkideal/cli/015-file
go: creating new go.mod: module github.com/mkideal/cli/015-file
$ cat go.mod
module github.com/mkideal/cli/015-file
go 1.14
$ go build
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
main.go:6:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
main.go:7:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
$ go get -v github.com/mkideal/cli
go: github.com/mkideal/cli upgrade => v0.2.2
$ go get -v ./...
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
main.go:6:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
main.go:7:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
$ go version
go version go1.14.1 linux/amd64
Try clearing cache:
go clean -modcache
For more info on how this command works, use go help clean
In my case cleaning cache didn't help.
Running go install in a project root printed no Go files in ... and that was the root cause, in the same time running go install gitlab.com/.... printed info about a missing package.
What had to be done was creating a go file in a project root directory with main function.
Update to go version go1.14.3 linux/amd64
Clear go module cache
don't know which one solved the problem (or both), now AOK.
I had the same error, but in my case I was attempting to import a module that made available only resource files, and no go pkgs. Adding an empty go file in the module with a package declaration solved it.
In my case, go.mod files were under src, after moving the go.mod file into one level up, then it works
Refer the Samples below,
directory structure when "package not found" error
dir1/src/
main.go
go.mod
go.sum
directory structure after fix
dir1/
go.mod
go.sum
src/
main.go
I had a similar problem. In my case the package name was not matching the name of the folder it resided in.

go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision ...?

My coworker pushed a tag v3.0.1 before updating go.mod to have /v3 suffix (https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher). I have updated module path (go.mod) and all import paths (*.go) to fix it, tagged as v3.0.2.
Now the problem is:
go get -v git.example.com/owner/repo#v3.0.2
go: finding git.example.com/owner/repo v3.0.2
go: git.example.com/owner/repo#v0.0.0-20190722053407-d85c4f69ad17: go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision
d85c4f69ad17
Found this: go build keeps complaining that: go.mod has post-v0 module path
So, I deleted both v3.0.0 and v3.0.1 tags, pointed it to the latest commit, re-pushed but the problem still stand.
I noticed that go.mod still refered to the old version as an indirect dependency:
require (
git.example.com/owner.repo v0.1.2 // indirect
Even if I changed it to /v3 v3.0.2 it will be restored to v0.1.12 automatically.
Why?
Did I miss something?
Tue Jul 23 05:54:56 +07 2019
rm go.*
go mod init git.example.com/dependent/project
go mod tidy
and go.mod is updated correctly now:
require (
- git.example.com/owner/repo v0.1.2
+ git.example.com/owner/repo/v3 v3.0.2
but go get -v git.example.com/owner/repo#v3.0.2 still returned the error:
go: finding git.example.com/owner/repo v3.0.2
go: git.example.com/owner/repo#v0.0.0-20190722053407-d85c4f69ad17: go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision
d85c4f69ad17
(d85c4f69ad17 is the latest commit in master)
I noticed that there are both v0.1.2 and v3.0.2 in go.sum:
git.example.com/owner/repo v0.1.2 h1:mCGJEmyrFDTCGkRfUIORpqdrNkSONQ6K+AcTNgxqveY=
git.example.com/owner/repo v0.1.2/go.mod h1:FfUKnyPrARCtAXQZ3BQVJI7h2eJ0UpQBMLg4bNs4Kdc=
git.example.com/owner/repo/v3 v3.0.2 h1:mJtDKLeiP8vMRSZo08i/k/KDbIoZTlKW2aWu7DUBvMM=
git.example.com/owner/repo/v3 v3.0.2/go.mod h1:64LE0ts0Lk9InIQyhPYGmnxs6LZIl6H4Iorl1EXfqxo=
Please pay attention to my go get command:
go get -v git.example.com/owner/repo#v3.0.2
It should be:
go get -v git.example.com/owner/repo/v3#v3.0.2
for example you can replace repository with this hack: https://github.com/golang/go/wiki/Modules
require {
...
}
replace git.example.com/owner.repo v0.1.2 => git.example.com/owner.repo v3.0.2
or you can use go get at the commit hash you want:
go get git.example.com/owner.repo#af044c0995fe
go get will correctly update the dependency files (go.mod, go.sum).
For more information: https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies
or for last example you should clean cache
remove go.mod and go.sum
go cache clean
go mod vendor
Expanding on the answer from #quanta...
You are doing:
go get -v git.example.com/owner/repo#v3.0.2
Because it is a v3 module, the go get command should include a /v3 before the #:
go get -v git.example.com/owner/repo/v3#v3.0.2
Once a v3.x.y package is a module with its own go.mod, then whenenver you are operating with modules enabled, you pretty much always include the /v3 whenever you reference the v3.x.y module, including in:
arguments to go get on the command line
import statements in .go code for the consumer
require statements in a consumer's go.mod
replace or exclude statements in a consumer's go.mod
the module line of the v3 module's go.mod file
internal import statements in .go code inside the v3 module importing other packages in the v3 module
etc.
One way to think about it is that the module's name is now effectively git.example.com/owner/repo/v3, where its name includes the trailing /v3.
If you are a consumer of a vN module and need to update your import paths in your .go files to include the vN, then
github.com/marwan-at-work/mod is a commonly used tool from the community that automates adding the /vN in all the required spots. Separately, it also automates placing the /vN in all the required spots if you are a module author for a v2+ module.
From the "Semantic Import Versioning" section of the Go modules wiki:
If the module is version v2 or higher, the major version of the module must be included as a /vN at the end of the module paths used in go.mod files (e.g., module github.com/my/mod/v2, require github.com/my/mod/v2 v2.0.0) and in the package import path (e.g., import "github.com/my/mod/v2/mypkg").
I may have had a similiar issue where I updated a module to use the /v2 import path but go getting the module always returned an error about invalid go.mod
The solution was to go get -u github.com/<me>/<pkg>/v2

Resources