Subject: Re: [bug#50227] [PATCH] build-system/go: Trim store references using the native compiler option.

From: Marius Bakke <[email-masked]>
Date: Fri, 27 Aug 2021 21:38:40 +0200

Marius Bakke <[email-masked]> skriver:

> Marius Bakke <[email-masked]> skriver:
>
>> * guix/build/go-build-system.scm (build): Add '-trimpath' to the 'go install'
>> invocation.
>> (remove-store-references, remove-go-references): Remove procedures.
>> (%standard-phases): Don't include remove-go-references.
>> * gnu/packages/docker.scm (docker)[arguments]: Add the '-trimpath' option to
>> the build flags.  Remove phase remove-go-references.
>> * gnu/packages/uucp.scm (nncp)[arguments]: Likewise.

[...]

> Docker explodes from 764.4 MiB to 1215.5 MiB with this patch even though
> it does use the '-trimpath' option.  Perhaps -trimpath does not work as
> well with dynamically linked executables as it does for static?

The size difference comes from containerd, which has a custom build
system that does not add -trimpath.  After adding the following hunk:

diff --git a/gnu/packages/docker.scm b/gnu/packages/docker.scm
index 88dccc2ae2..e1ddfc6c38 100644
--- a/gnu/packages/docker.scm
+++ b/gnu/packages/docker.scm
@@ -221,6 +221,13 @@ Python without keeping their credentials in a Docker configuration file.")
                    (("exec\\.LookPath\\(\"unpigz\"\\)")
                     (string-append "\"" (assoc-ref inputs "pigz")
                                    "/bin/unpigz\", error(nil)"))))))
+           (add-before 'build 'trim-store-references
+             (lambda* (#:key import-path #:allow-other-keys)
+               (substitute* (string-append "src/" import-path "/Makefile")
+                 ;; Pass the '-trimpath' option down to 'go build' in order
+                 ;; to avoid spurious store references.
+                 (("^GO_BUILD_FLAGS=")
+                  "GO_BUILD_FLAGS=-trimpath"))))
            (replace 'build
              (lambda* (#:key import-path #:allow-other-keys)
                (with-directory-excursion (string-append "src/" import-path)

...the size of Docker becomes 763.7 MiB, or 0.7 less than before.

I realize we can set the flag globally in go-build-system, instead of
just for the build phase.  Then we don't need to patch Docker,
containerd, or anything else that does not use the stock build phase.

diff --git a/guix/build/go-build-system.scm b/guix/build/go-build-system.scm
index fc5ee39c8d..a6b9397d35 100644
--- a/guix/build/go-build-system.scm
+++ b/guix/build/go-build-system.scm
@@ -137,6 +137,9 @@ dependencies, so it should be self-contained."
   ;; Using the current working directory as GOPATH makes it easier for packagers
   ;; who need to manipulate the unpacked source code.
   (setenv "GOPATH" (string-append (getcwd) ":" (getenv "GOPATH")))
+  ;; Unconditionally set the -trimpath option to avoid spurious store references
+  ;; from having multiple GOPATH entries.  See <https://bugs.gnu.org/33620>.
+  (setenv "GOFLAGS" "-trimpath")
   ;; Go 1.13 uses go modules by default. The go build system does not
   ;; currently support modules, so turn modules off to continue using the old
   ;; GOPATH behavior.
@@ -188,8 +191,6 @@ unpacking."
       (apply invoke "go" "install"
               "-v" ; print the name of packages as they are compiled
               "-x" ; print each command as it is invoked
-              ;; Trim store references from the compiled binaries.
-              "-trimpath"
               ;; Respectively, strip the symbol table and debug
               ;; information, and the DWARF symbol table.
               "-ldflags=-s -w"
@@ -202,6 +203,9 @@ unpacking."
 ;; Can this also install commands???
 (define* (check #:key tests? import-path #:allow-other-keys)
   "Run the tests for the package named by IMPORT-PATH."
+  ;; Remove the global -trimpath option because it can break some test
+  ;; suites.
+  (unsetenv "GOFLAGS")
   (when tests?
     (invoke "go" "test" import-path))
   #t)

This may be a cleaner solution.  Thoughts?