Monday, April 21, 2025
HomeGolang`go mod obtain && go mod vendor && go mod tidy` usually...

`go mod obtain && go mod vendor && go mod tidy` usually requires 2x runs to succeed – Technical Dialogue


Go mod usually fails the primary time. It generates spurious errors like lacking go.sum entry. And so I usually find yourself working this sequence twice in a row:

  1. go mod obtain
  2. go mod vendor (advisable)
  3. go mod tidy

Can Go please repair the mod system? Not solely is it tiring to repeat the sequence twice, doubling my labor throughout very many Go tasks. However it’s additionally foolish to must invoke three distinct instructions to replace the Go caching system.

There ought to actually be a easy command like go mod refresh that performs every of those low stage steps. It ought to learn an non-compulsory boolean parameter that controls whether or not or to not vendor, from the go.mod configuration file. It ought to by no means requiring repeating the command once more to resolve low stage recoverable errors.

Yet one more case in favor of a single, unified refresh command is the truth that a number of instructions introduce delicate bugs into bigger scripts. POSIX shells could apply && and/or set -e to make sure that real errors propagate by means of downstream methods, equivalent to CI/CD jobs. However this syntax is just not moveable past *nix environments. Command Immediate and PowerShell battle to reliably bubble up correct exit codes. Combining these operations right into a single command makes scripts that run go mod… instructions rather more dependable. With out having to resort to shoving the whole lot right into a extra verbose mage Go job.

Good day there. You’ve got this error, as a result of the sequence of steps is unsuitable. Simply add module you want into go.mod (or as import into the file you’re engaged on) and easily run go mod tidy. It would obtain the whole lot by itself and add entry into go.sum. After you’ll be able to run go mod vendor when you want this.



1 Like

Agreed. I’ve by no means seen or had a difficulty with simply utilizing go mod tidy.

merely run go mod tidy . It would obtain the whole lot by itself and add entry into go.sum .

On my machine, go mod vendor usually complains that I have to manually go mod obtain beforehand.

go mod tidy ought to suffice. However you must obtain the modules to have the ability to vendor them. Additionally it checks vendor/modules.txt for discrepancies together with your go.mod and will report errors:

go mod vendor additionally creates the file vendor/modules.txt that incorporates a listing of vendored packages and the module variations they have been copied from. When vendoring is enabled, this manifest is used as a supply of module model info, as reported by go listing -m and go model -m. When the go command reads vendor/modules.txt, it checks that the module variations are according to go.mod. If go.mod has modified since vendor/modules.txt was generated, the go command will report an error. go mod vendor needs to be run once more to replace the vendor listing.

However nonetheless, try to be good working go mod tidy as soon as (clear up/obtain dependencies) then go mod vendor (vendor the dependencies we simply downloaded).

go mod tidy adopted by go mod vendor seems to work.

Nevertheless, once more, having to run two separate instructions to replace the dependency cache is dangerous, particularly when it comes to OS/shell portability. The programming language stands to enhance by adopting a boolean possibility in go.mod that may mechanically set off go mod vendor every time go mod tidy runs.

Unknown if there are some other situations the place a separate go mod obtain could also be required.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments