go mod: cannot find module providing package
Asked Answered
S

7

64

I am creating a go project with version 1.12.1. If I run GOPATH="$(pwd)/vendor:$(pwd)" GOBIN="$(pwd)/bin" go clean I get the following error:

can't load package: package github.com/marvincaspar/go-example: unknown import path "github.com/marvincaspar/go-example": cannot find module providing package github.com/marvincaspar/go-example

This is only for go clean, go run or go build works fine.

Here is the folder structure of main code:

.
├── Makefile
├── cmd
│   └── server
│       └── main.go
├── go.mod
├── go.sum
└── pkg
    └── storage
        └── mysql
            └── storage.go

Here is how the go.mod file looks like:

module github.com/marvincaspar/go-example
go 1.12

require (
    github.com/go-sql-driver/mysql v1.4.1
)

And finally the main.go file:

package main

import (
    "fmt"
    "os"

    "github.com/marvincaspar/go-example/pkg/storage/mysql"
)

func main() {
    if err := run(); err != nil {
        fmt.Fprintf(os.Stderr, "%v", err)
        os.Exit(1)
    }
}

func run() error {
    // init storage
    s := mysql.NewStorage()
    // do some other stuff...
}

Any ideas what I am doing wrong?

Sagittarius answered 11/4, 2019 at 11:36 Comment(4)
You either do go modules or GOPATH. If you want to do modules it is best to force module builds with GO111MODULE=on. Your GOPATH looks suspicious, maybe you should switch to modules. Such errors is often a tiny typo; tripple check.Egoism
I already set the default for GO111MODULE to on.Sagittarius
You should clearly describe that only go clean results in this error. And if you do a module build don't distract by talking about GOPATH or GOBIN. Also note that go clean fails if run from a directory without source code (github.com/golang/go/issues/31002) . Where are you running go clean? It seems everything is working fine and you try to cleanup somewhere where nothing is to be cleaned.Egoism
I'm not quite sure what the intent is, but you should almost certainly not set GOPATH to include the vendor directory (GOPATH="$(pwd)/vendor:$(pwd)").Occasionally
R
25

Go build/install is trying to find main package in your root directory, it is not checking sub-directories (cmd/server) in your case. Hence you are getting package not found error.

To properly build your code, you can run:

go build github.com/marvincaspar/go-example/cmd/server

Similarly, to run your project, you will have to provide module-name/main-package-path:

go run github.com/marvincaspar/go-example/cmd/server

Go clean can be executed in same way, by providing module-name/path-with-main-package

go clean github.com/marvincaspar/go-example/cmd/server

or

GOPATH="$(pwd)/vendor:$(pwd)" GOBIN="$(pwd)/bin" go clean github.com/marvincaspar/go-example/cmd/server 

However, as per https://blog.learngoprogramming.com/code-organization-tips-with-packages-d30de0d11f46, just put your source files into your project’s root. It’s better that way.

Rebekahrebekkah answered 11/4, 2019 at 12:32 Comment(1)
What do you need to change if the github.com/name changes? (Ie. for something you forked?)Hubris
W
64

I generally use go get and go mod tidy for same. It works all the time.

go mod tidy
Wame answered 16/6, 2021 at 10:47 Comment(4)
This answer is spot on. Just to add some extra detail from the documentation here. go mod tidy ensures that the go.mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module’s packages and dependencies, and it removes requirements on modules that don’t provide any relevant packages. It also adds any missing entries to go.sum and removes unnecessary entries.Dreibund
In my case I was trying to import an individual .go file instead of the folder, if it saves someone else some timePayroll
I was having trouble finding some references to other .go package but I already had a go.mod file. What I did was delete the requires (...) and do go mod tidy again. And it worked.Priestridden
"It works all the time." No, it doesn't if the repo is a private repo...Muscular
E
39

Normally this new project approach works for me:

go mod init <project_name>
go test

I have found that developing projects outside of GOROOT and GOPATH are much easier

Ecclesiasticism answered 24/11, 2020 at 15:47 Comment(0)
R
25

Go build/install is trying to find main package in your root directory, it is not checking sub-directories (cmd/server) in your case. Hence you are getting package not found error.

To properly build your code, you can run:

go build github.com/marvincaspar/go-example/cmd/server

Similarly, to run your project, you will have to provide module-name/main-package-path:

go run github.com/marvincaspar/go-example/cmd/server

Go clean can be executed in same way, by providing module-name/path-with-main-package

go clean github.com/marvincaspar/go-example/cmd/server

or

GOPATH="$(pwd)/vendor:$(pwd)" GOBIN="$(pwd)/bin" go clean github.com/marvincaspar/go-example/cmd/server 

However, as per https://blog.learngoprogramming.com/code-organization-tips-with-packages-d30de0d11f46, just put your source files into your project’s root. It’s better that way.

Rebekahrebekkah answered 11/4, 2019 at 12:32 Comment(1)
What do you need to change if the github.com/name changes? (Ie. for something you forked?)Hubris
B
6

This can also happen if you are using workspaces. It seems like you can't use one package without workspaces if you are using others with workspaces.

So try going into your top level workspace and do

go work use ./problemPackage.

At least this worked for me.

Bremsstrahlung answered 24/1, 2023 at 14:44 Comment(0)
D
0

In my case, this was failing in CircleCI and I could not produce locally. Here was my scenario.

App A depends on Module B
Module B depends on Module C
App A directly depends on Module C

However, the module C's version was different in App A than in Module B. This was the reason for this error and by fixing this in Module B by running go get -u, publishing a new version, and pulling it down in App A fixed it for me.

Dzoba answered 17/3, 2023 at 8:25 Comment(0)
A
0

Also check if there are non *.go files in the current dir. This probelm occurred to me when I tried to run the command while not properly organising the files, as a result in the current dir there is a folder containing .tmpl files.

Assignment answered 13/4, 2024 at 17:20 Comment(0)
D
-3

To solve this problem you have to do few things, First, go to the project directory via the Terminal then run the following command ( If you are using git clone then go to the clone directory folder via Terminal and run the following command):

Step 1: sudo go mod init your-program.go

Step 2: sudo go mod tidy

Step 3: sudo go build your-program.go

Dermoid answered 24/12, 2022 at 13:5 Comment(1)
Running this via sudo, seriously?Cholecalciferol

© 2022 - 2025 — McMap. All rights reserved.