-
Notifications
You must be signed in to change notification settings - Fork 18k
x/vgo: add ability to trim go.mod #24101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Related to #24150 I'll paste what I wrote there: The hard thing about unused dependencies is they are build tag dependant. While there is a maximum dependency graph for all tags, it is reasonably common to not care about a subset of them. In other words, there are three definitions of unused:
It would be nice if go list could take a tag Boolean expression and report the use status of the module. We could then use that primative to compute and of the thee views above. Then to prune something like:
|
After the script in #24150 I don't see much in go.mod:
What is there to prune? |
Never mind, I see this was filed before #24150 (despite the link confusing me) and is about the general pruning topic (which is difficult). |
FWIW, to add to the discussion, I'd like to quickly show one of the possible solutions, which worked perfectly well for us. In our in-house vendoring tool, we went with the semantics of pruning = "burning the house and rebuilding it from scratch, in additive way" (see zpas-lab/vendo/use-cases.md, search for "vendo-recreate"). I see it as having various advantages over pruning = "selectively deleting", such as good reproducibility, simpler operation, and keeping only one code path to the same result (no hysteresis), also meaning only one code path ever excercised (no different bugs on different paths, which again could lead to possible hysteresis). I'll try to explain the approach here, as tersely as I can. I purposefully don't try to stick too strictly to go.mod concepts, to have more freedom of expression; I assume fine-tuning this, exploring further and fitting to vgo concepts can be done later if needed. In our tool (named
In a simplest and crudest implementation,
However, the most glaring problem for us in this approach was that it would be losing all "explicit overrides" information included in the original "go.mod". So, in our tool, the
The Now that I think of it, in case the vgox tool was built as described above, the go.mod.old file could actually be called e.g. go.mod.overrides, and just explicitly kept in the user's repo, besides regular go.mod. The vgox tool could the just check if such a file exists, and if yes, automatically assume that an equivalent to
Some extra note regarding our real tool (vendo):
|
There is already code (e.g. vgo vendor) that attempts to understand the full set of imports required for building a particular module's packages and its dependency packages (not dependency modules). It could easily help trim go.mod as well, and I intend to do that, maybe in |
We still need to decide what command should do this. Actually doing it is mostly straightforward. |
I like the idea of a
My current opinion is that unused entries in go.mod should be aggressively pruned. AIUI any vgo command can add dependencies to go.mod - I think that probably any command should remove unused dependencies too (modulo the Unused dependencies in a go.mod file can be thought of a little like unused dependencies in a Go file. Similar to those, I wonder if a dependency in a go.mod file that isn't used at all by a module or any of its dependencies should be considered a build error when it does have a |
Follow-on thought: I wonder if any dependency that is explicitly |
You can decide you are missing a dependency with purely local information: here is one package, it has an import, and nothing provides that import. In contrast, deciding that a dependency is unused requires global information: for all packages in the module, for all possible build tag combinations that might be used to build them, no source file needs that import. Most vgo commands do not look at "all packages". I don't believe they should start. At the least that would be confusing (reporting errors about code not being built) and inefficient (reading files that are unnecessary to the task at hand).
Note that In any event, if pruning is manually triggered (as it probably must be) then it will probably be OK for |
Change https://golang.org/cl/118877 mentions this issue: |
There seems to be no straightforward way of pruning unused dependencies from a go.mod file.
Perhaps a variant of
vgo clean
might work for this.The text was updated successfully, but these errors were encountered: