8

Before I used go dep, but now office ensure the official tool is go mod.

When I use go dep, I can add local dependency packages to vendor and ignored in Gopkg.toml to ignore search the package from repo. I can execute go dep update normally.

Now I use go mod, I also add local dependence package to vendor and add it exclude in go.mod. But when i execute go mod tidy, it remove the package even though my project exist import xxx.

What did i do:

  1. create new project
  2. execute go mod init
  3. modify go.mod exclude privaterepo.com/bb/bb

  4. copy my local module to vendor because the local module is on a private repo which not support https.

  5. now the vendor just like:

    vendor |-github.com/aa/aa |-privaterepo.com/bb/bb

  6. import "privaterepo.com/bb/bb"
  7. execute go build -mod vendor
  8. than i got error "cannot find module for path privaterepo.com/bb/bb" 9 always try with replace, but it also not work

So, what should I do to add local package to vendor and avoid go mod remove it?

6
  • Did you try go mod vendor? Commented Dec 3, 2018 at 7:53
  • What is a "local package"? All packages are equal. Commented Dec 3, 2018 at 8:32
  • @ttomalak if i didn't execute go mod vendor, it wouldn't create the vendor folder. Commented Dec 3, 2018 at 13:46
  • @Volker I means the package in my private repo which not support https both and need auth. So the only way is that i git clone use ssh than i copy the package to vendor. Such as vendor/xxx.com/xxx/xx. Commented Dec 3, 2018 at 13:49
  • Okay, got that. But I still do not understand the problem. Can you describe the problem more clearly? Commented Dec 3, 2018 at 15:12

1 Answer 1

12

So, what should I do to add local package to vendor and avoid go mod remove it?

Well, I think you cannot do this. This is not how it works. go mod vendor manages your vendor folder.

Instead of excludeing you package from go.mod you should add a replace directive to instruct the go tool to look up the package not from privaterepo.com but from the local filesystem. Quoting from https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive

replace also can be used to inform the go tooling of the relative or absolute on-disk location of modules in a multi-module project, such as:

   replace example.com/project/foo => ../foo

So in your case: Do not try to manually put privaterepo.com/bb/bb in vendor, but have it somewhere outside the current project and use

replace privaterepo.com/bb/bb => ../bb

And let go mod copy this stuff from the filesystem to your vendor.

Sign up to request clarification or add additional context in comments.

5 Comments

I had read the modules wiki, and i known i can copy privaterepo.com/bb/bb to outside vendor folder, also if i do this i won't need to add replace privaterepo.com/bb/bb => ../bb in the go.mod. But what's the mean of exclude, when should i use exclude? I prefer manager my private repo in vendor just like other public repo.
@yanyandenuonuo exclude is for excluding a specific version of a module if you know that this version is broken/doesn't work with our code. IN this case you can mark this version as excluded an go will not select this version during version selection. For the second part: If you manage your build with modules and go mod than you must adopt your workflow to go mod.
May be i should ask the question at golang issue. I don't think exclude is for excluding a specific version because require already declare the version. Anyway, thank you so much for your answer.
@yanyandenuonuo exclude is for excluding a single version from the set of possibles and require is to select a single fixed version. Have a second look at the documentation if unsure.
Got it, but i don't think this is right things about exclude and i had create a issue about this. Thank you once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.