Home > Net >  What does it mean when a package is in the go/pkg/mod/cache dir but it has no source code extracted?
What does it mean when a package is in the go/pkg/mod/cache dir but it has no source code extracted?

Time:10-29

I'm trying to understand how the source code for third-party dependencies is or is not compiled into my Go binary. I'm building in a Docker container, so I can see precisely what's fetched for my build without interference from other builds.

After my go build completes I see source code files for several dependencies under go/pkg/mod/$module@$version directories. The Module cache documentation tells me that these directories contain "extracted contents of a module .zip file. This serves as a module root directory for a downloaded module." My best guess is that the presence of extracted source code for these dependencies indicates that "yes, these dependencies are definitely compiled into your binary."

I also see many more dependencies pulled into go/pkg/mod/cache/download/$module directories. The Module cache documentation tells me that this directory contains "files downloaded from module proxies and files derived from version control systems," which I don't fully understand. As far as I can see, these files do not include any extracted source code, though there are several .zip files that I assume contain the source. For the most part these files seem to be .mod files that just contain text representing some sort of dependency graph.

My question is: if a third-party dependency has module files under go/pkg/mod/cache/download but no source code under go/pkg/mod/$module@$version, does that mean that dependency's code was NOT compiled into my Go binary?

I don't understand why the Go build pulls in all these module files but only has extracted source code for some of the third-party modules. Perhaps Go preemptively parses and pulls module information for the full transitive set of modules referenced from the modules my first-party code imports, but perhaps many of those modules don't end up being needed for my binary's compile build process and therefore don't get extracted. If that's not true and the answer to my question is no, then I don't understand how or why my binary can link in those dependencies without go build fetching their source code.

CodePudding user response:

As mentioned in "Compile and install packages and dependencies"

Compiled packages are cached automatically.

GOPATH and Module includes:

When using modules, GOPATH is no longer used for resolving imports.
However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin).

So if you see sources in pkg/mod which are not in pkg/mod/cache, try a go mod tidy

add missing and remove unused modules

From there, you should have the same modules between sources (pkg/mod) and compiled modules (pkg/mod/cache)

CodePudding user response:

Based on the OP's comment

I need to know exactly what's included in the binary for compliance reasons.

I would recommend a completely different approach: dumping the list of symbols contained in the binary and then correlating it with whatever information is needed.

The command

go tool nm -type /path/to/the/executable/image/file

would dump the symbols — names of the functions — whose code was taken from both the standard library packages, 3rd-party and/or vendored packages and internal packages, compiled and linked into the binary, and print to its standard output stream a sequence of lines

address type name

which you can then process programmatically.

Another approach you might employ is to use go list with various flags to query the program's source code about the packages and/or modules which will be used when building: whatever that command outputs describing the full dependency graph of the source code is whatever go build will use when building — provided the source code is not changed between these calls.

Yet another possibility is to build the program using go build -x, save the debug trace it produces on its standard error stream and parsing it for exact module names the command reported as used during building.

  •  Tags:  
  • go
  • Related