rancher/trash

What is the "unnecessary code" mentioned in the README

amoghe opened this issue · 5 comments

One the reasons this project exists (as per the README) is that "unnecessary code" should not be in vendor dir (which is version controller). Could someone help me understand what "unnecessary code" might make its way into that dir if we used glide?

While you certainly can use glide to specify exactly what you need in ./vendor, the thing is, with trash

  • you only need to specify the root (repo level) packages. Your .go source files already specify all the sub-packages that they want to import, so why do it another time?
  • you can have ./vendor cleaned of packages and source files you don't import, so you can safely import from even very large projects like docker or cadvisor and be sure only the bits you actually use get pushed into your repo.

Trash is only really useful if you want to push your dependencies into your project's repo in source form. And that's the only currently possible way to get reproducible builds with tools like go get and go build.

@amoghe Did I answer your question in my previous comment?

If I did, I'll edit the README accordingly and close this. Thanks!

Hmm, not sure I completely follow

you only need to specify the root (repo level) packages. Your .go source files already specify all the sub-packages that they want to import, so why do it another time?

go imports specify the path to the actual package they use, e.g. I'll import:

  import github.com/prometheus/client_golang/tree/master/api/prometheus

Should I interpret your statement to mean that I only need to specify github.com/prometheus/client_golang in the trash file at the top of my repo?

and be sure only the bits you actually use get pushed into your repo

... does this mean that other subpackages will be pruned? Does trash correctly detect when subpackages of a third party import refer to each other? (E.g. within the docker pkg, there's all sorts of cross importing happening)

Thank you for being patient with my questions :-)

When you run trash (with no arguments) it does the following:

  1. Read trash.conf file at the top of your project.
  2. For each specified package / repo does go get, git fetch, git checkout, using ~/.trash-cache dir as GOPATH.
  3. Wipe out ./vendor dir and then copy full sources of each checked out repo to ./vendor dir in your project.
  4. Construct preserved package list: add all subdirs in your project (except ./vendor), then scan all the imports in these packages and recursively add dependency packages from current ./vendor content.
  5. Delete all subdirs from ./vendor, that are not in the preserved package list.
  6. Delete all *_test.go files from ./vendor subdirs. Delete all empty ./vendor subdirs.

This means, if you import "github.com/prometheus/client_golang/api/prometheus" or "github.com/prometheus/client_golang/prometheus" in your .go code, you need to have something like this in trash.conf (assuming you want to use client_golang version 0.7.0):

github.com/beorn7/perks 3ac7bf7
github.com/golang/protobuf      c3cefd4
github.com/matttproud/golang_protobuf_extensions        v1.0.0
github.com/prometheus/client_golang     0.7.0
github.com/prometheus/client_model      086fe7c
github.com/prometheus/procfs    abf152e

Only the packages actually imported by your code (directly or transitively) will stay in ./vendor after you run trash. Trash does preserve the transitive dependencies of your direct imports, as long as the necessary repos are specified in trash.conf.

To initially populate trash.conf or bump versions of your libs, you might want to run trash -u which will re-create trash.conf with needed dependencies, only their very latest master commits. You might want to edit the file to use released tags instead.

Closing this.

Feel free to open another issue if you feel trash could handle things better.