Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add local dependency to vendor when using go mod

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?

like image 244
linfeng Avatar asked Dec 03 '18 06:12

linfeng


People also ask

How does go mod vendor work?

The go mod vendor command constructs a directory named vendor in the main module's root directory that contains copies of all packages needed to support builds and tests of packages in the main module. Packages that are only imported by tests of packages outside the main module are not included.

How do I install Go package dependencies?

For installing the go project dependencies, we use go get command, which automatically updates the go. mod file. You can also install go project dependencies by following a few steps, but remember as the package is still not used anywhere in the project it will be marked as indirect.

Where are go module dependencies installed?

A Module is a collection of Go packages stored in a file tree under $GOPATH/pkg folder with a go. mod file at its root. This file defines the module's path, which is also the import path used for your project, and its dependency requirements, which are the other modules needed for a successful build.


1 Answers

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.

like image 53
Volker Avatar answered Sep 24 '22 19:09

Volker