Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go mod vendor without update to latest

Tags:

go

go-modules

I’m trying to figure out if it’s possible to run go mod vendor without the go tool updating my go.mod file.

I specifically go get package/subpackage@commit and commit my go.mod with the correct version.

Then I run go mod vendor and it automatically bumps the version of the package that I just specifically set.

I’ve looked at this page to no avail: https://github.com/golang/go/wiki/Modules#how-do-i-use-vendoring-with-modules-is-vendoring-going-away

I need to use vendor because I run a script that edits some of the vendored deps., I’m looking at the following build flow:

GO111MODULE=on go get package/subpackge@commit
GO111MODULE=on go mod vendor
./Script/patch_vendors.sh --write
GO111MODULE=off go build

My other option is modifying the copied source wherever go mod vendor donwloads it to, but not sure how to approach that.

Thanks in advance

like image 804
Tristian Avatar asked Apr 02 '19 04:04

Tristian


People also ask

Why are there 2 require in go mod?

Because in Go 1.17 the module graph has been changed to enable pruning and lazy loading. The second require block contains indirect dependencies. If a module specifies go 1.17 or higher, the module graph includes only the immediate dependencies of other go 1.17 modules, not their full transitive dependencies.

Is go mod vendor necessary?

At go 1.14 or higher, automatic vendoring may be enabled. If the file vendor/modules. txt is present and consistent with go. mod , there is no need to explicitly use the -mod=vendor flag.

What is the difference between go sum and go mod?

go mod tidy acts as if all build tags are enabled, so it will consider platform-specific source files and files that require custom build tags, even if those source files wouldn't normally be built. The go. sum file may contain hashes for multiple versions of a module.

What does indirect mean in go mod?

Indirect Dependencies You may see some dependencies in your go. mod file that are indirect (denoted by // indirect ). This means that one of your dependencies doesn't have its own go. mod file, so, the dependencies that it imports are included in your project's go. mod file.


1 Answers

Per https://tip.golang.org/cmd/go/#hdr-Maintaining_module_requirements:

The go command itself automatically updates the go.mod file to maintain a standard formatting and the accuracy of require statements.

Any go command that finds an unfamiliar import will look up the module containing that import and add the latest version of that module to go.mod automatically. […]

Any go command can determine that a module requirement is missing and must be added […].

The go mod vendor command copies in all of the transitive imports of your packages and their tests, so it will automatically update the go.mod file to ensure that all of the imported packages are present.

So the problem here is likely that the commit you've selected for package/subpackage fails to provide some package that appears in the transitive imports of your program. If that is correct, you should find that go list all, go test all, and go mod tidy all make that same edit to your module's requirements.

like image 80
bcmills Avatar answered Oct 30 '22 18:10

bcmills