Subject: [bug#50227] [PATCH 1/3] build-system/go: Use a native-search-path for GOPATH.

From: Marius Bakke <[email-masked]>
Date: Fri, 27 Aug 2021 17:13:28 +0200

* gnu/packages/golang.scm (go-1.14)[native-search-paths]: New field.
* guix/build/go-build-system.scm (setup-go-environment): Make the working
directory the first entry on GOPATH instead of creating a union.
(unpack, install): Adjust accordingly.
(go-package?, go-inputs): Remove variables.
---
 gnu/packages/golang.scm        |  4 ++++
 guix/build/go-build-system.scm | 37 ++++------------------------------
 2 files changed, 8 insertions(+), 33 deletions(-)

diff --git a/gnu/packages/golang.scm b/gnu/packages/golang.scm
index 72579d6bd2..3a5c6ddc3f 100644
--- a/gnu/packages/golang.scm
+++ b/gnu/packages/golang.scm
@@ -257,6 +257,10 @@ in the style of communicating sequential processes (@dfn{CSP}).")
        (sha256
         (base32
          "1crh90qkvhlx23hwsi4wxy3l3h8973lr18135y6h1nnzzwr3n3ps"))))
+    (native-search-paths
+     (list (search-path-specification
+            (variable "GOPATH")
+            (files '("src/.." "pkg/..")))))
     (arguments
      (substitute-keyword-arguments (package-arguments go-1.4)
        ((#:system system)
diff --git a/guix/build/go-build-system.scm b/guix/build/go-build-system.scm
index 227df820db..01dd8cd249 100644
--- a/guix/build/go-build-system.scm
+++ b/guix/build/go-build-system.scm
@@ -5,6 +5,7 @@
 ;;; Copyright © 2020 Jack Hill <[email-masked]>
 ;;; Copyright © 2020 Jakub Kądziołka <[email-masked]>
 ;;; Copyright © 2020 Efraim Flashner <[email-masked]>
+;;; Copyright © 2021 Marius Bakke <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -88,7 +89,6 @@
 ;; a tmpdir when creating the inputs union.
 ;; * Use Go modules [4]
 ;; * Re-use compiled packages [5]
-;; * Avoid the go-inputs hack
 ;; * Stop needing remove-go-references (-trimpath ? )
 ;; * Remove module packages, only offering the full Git repos? This is
 ;; more idiomatic, I think, because Go downloads Git repos, not modules.
@@ -143,23 +143,12 @@ dependencies, so it should be self-contained."
   (setenv "GOCACHE" "/tmp/go-cache")
   ;; Using the current working directory as GOPATH makes it easier for packagers
   ;; who need to manipulate the unpacked source code.
-  (setenv "GOPATH" (getcwd))
+  (setenv "GOPATH" (string-append (getcwd) ":" (getenv "GOPATH")))
   ;; 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.
   (setenv "GO111MODULE" "off")
   (setenv "GOBIN" (string-append (assoc-ref outputs "out") "/bin"))
-  (let ((tmpdir (tmpnam)))
-    (match (go-inputs inputs)
-      (((names . directories) ...)
-       (union-build tmpdir (filter directory-exists? directories)
-                    #:create-all-directories? #t
-                    #:log-port (%make-void-port "w"))))
-    ;; XXX A little dance because (guix build union) doesn't use mkdir-p.
-    (copy-recursively tmpdir
-                      (string-append (getenv "GOPATH"))
-                      #:keep-mtime? #t)
-    (delete-file-recursively tmpdir))
   #t)
 
 (define* (unpack #:key source import-path unpack-path #:allow-other-keys)
@@ -191,31 +180,13 @@ unpacking."
     (display "WARNING: The Go import path is unset.\n"))
   (when (string-null? unpack-path)
     (set! unpack-path import-path))
-  (let ((dest (string-append (getenv "GOPATH") "/src/" unpack-path)))
+  (let ((dest (string-append (getcwd) "/src/" unpack-path)))
     (mkdir-p dest)
     (if (file-is-directory? source)
         (copy-recursively source dest #:keep-mtime? #t)
         (unpack-maybe-strip source dest)))
   #t)
 
-(define (go-package? name)
-  (string-prefix? "go-" name))
-
-(define (go-inputs inputs)
-  "Return the alist of INPUTS that are Go software."
-  ;; XXX This should not check the file name of the store item. Instead we
-  ;; should pass, from the host side, the list of inputs that are packages using
-  ;; the go-build-system.
-  (alist-delete "go" ; Exclude the Go compiler
-    (alist-delete "source" ; Exclude the source code of the package being built
-      (filter (match-lambda
-                ((label . directory)
-                 (go-package? ((compose package-name->name+version
-                                        strip-store-file-name)
-                               directory)))
-                (_ #f))
-              inputs))))
-
 (define* (build #:key import-path build-flags #:allow-other-keys)
   "Build the package named by IMPORT-PATH."
   (with-throw-handler
@@ -249,7 +220,7 @@ XXX We can't make use of compiled libraries (Go \"packages\")."
     (if (string-null? import-path)
         ((display "WARNING: The Go import path is unset.\n")))
     (let* ((out (assoc-ref outputs "out"))
-           (source (string-append (getenv "GOPATH") "/src/" import-path))
+           (source (string-append "src/" import-path))
            (dest (string-append out "/src/" import-path)))
       (mkdir-p dest)
       (copy-recursively source dest #:keep-mtime? #t)))
-- 
2.31.1