Is there a way to create or sync Gokpkg.toml
with the packages that i import in my .go
files?
If my Gopkg.toml
is empty and I run dep ensure
, it would download all the packages that I import in my code to the vendor
folder but it won't update the Gopkg.toml
file.
It came up when I had to migrate a legacy project that didn't use a package manager to using dep
in the simplest way possible by just creating a Gopkg.toml
that contains the latest version of all the packages that I already import instead of running dep ensure -add ...
manually for every package.
Is there such command?
Asked
Active
Viewed 4,201 times
4

areller
- 4,800
- 9
- 29
- 57
1 Answers
7
Remove your Gopkg.toml
file and just run dep init
and then dep ensure
. It will create your Gopkg.toml
file and automatically set all of the dependencies.

Ullaakut
- 3,554
- 2
- 19
- 34
-
1Damnit! this is embarrassing.. Thank you. `dep init` was actually enough. It still would've been nice if there was a standalone command for syncing `Gopkg.toml` with source code imports because what if I'm not migrating to `dep`? what if I imported some dependencies ad hoc into a project that already uses `dep` because it was the most convenient way and then want to sync my `Gopkg.toml` to include them? Yes, I could delete the `Gopkg.toml` file and run `dep init` again but it's kind of a work around, no? You were very helpful anyway :) – areller Aug 09 '18 at 05:05
-
As far as I know simply running `dep ensure` should actually ensure that your `Gopkg.toml` file is up to date and add all of your new dependencies :) – Ullaakut Aug 09 '18 at 05:19
-
1See the [documentation on dep ensure](https://golang.github.io/dep/docs/daily-dep.html#basics) for more info on what it does, but this is a good way of thinking about it: When you run `dep ensure`, you basically ask `dep` to "_make sure that my project is in sync: that Gopkg.lock satisfies all the imports in my project, and all the rules in Gopkg.toml, and that vendor/ contains exactly what Gopkg.lock says it should._" – Ullaakut Aug 09 '18 at 05:22
-
2I though so too but it didn't work, it updates `Gopkg.lock`, it downloads the imports to the `vendor` folder but it doesn't add them to `Gopkg.toml`. Also, in the documentation you just sent, scroll down to the line that starts with `Of course, given this model,`, they explain that you can just add an import in your code and then run `dep ensure` to vendor the imports but they won't append it to `Gopkg.toml`. It's weird, it seems like it was made that way deliberately, I don't understand why. – areller Aug 09 '18 at 05:30
-
1Ah that's interesting, I didn't know that! I always assumed that it was added automatically! Thanks for pointing that out! :) – Ullaakut Aug 09 '18 at 05:47