unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Sarah Morgensen <iskarian@mgsn.dev>
To: Marius Bakke <marius@gnu.org>
Cc: 50227@debbugs.gnu.org
Subject: [bug#50227] [PATCH 0/3] go-build-system and GOPATH improvements
Date: Fri, 27 Aug 2021 19:16:49 -0700	[thread overview]
Message-ID: <86mtp248ku.fsf@mgsn.dev> (raw)
In-Reply-To: Marius Bakke's message of "Fri, 27 Aug 2021 17:10:52 +0200 (9 hours, 41 minutes, 46 seconds ago)"

Hello Marius,

(Apologies in advance for the length of this treatise! I did not have
the time to be concise.)

Marius Bakke <marius@gnu.org> writes:

> These patches adjust the Go build system to use Guix's regular
> native-search-paths mechanism instead of ad-hoc GOPATH trickery.

I have been working on overhauling the Go build system behind the
scenes; I expected to have a patch ready last week, but I fell down the
"modules" rabbit hole after learning GOPATH is expected to be deprecated
as soon as 1.18.  Sorry for the duplicated work!  (I also have a Go 1.17
ready to launch, but I've been attempting to nail down Go build system
changes first so I didn't introduce anything incompatible.)

In any case, I hadn't thought of using search paths, that's quite
clever!  I like it.

Before falling down the "modules" rabbit-hole, here is what was working
for my GOPATH-based Go build system:

1. Install source in "out/share/go/src" rather than "out/src", and then
simply create a directory union of "/share/go/src" from inputs.  This
avoids accidentally including non-go packages with a "/src" directory.
If you did this, then you could make the GOPATH search path be
"/share/go/..".

2. Split the GOPATH as you have done (except with only two components;
one for the package we're building and one for the union in 1).

3. Reuse build artifacts by copying the $GOPATH[0]/pkg directory to
"out/share/go/pkg" in the install phase.  They will be transparently
used since they will be in GOPATH.  (You can use "out/lib/go/pkg", but
you must set 'strip-directories' to avoid stripping Go archives, and
then include "/lib/go/.." in the GOPATH search path.)  However, "go
install" will eventually deprecate installing archives [0], perhaps even
before GOPATH is deprecated.

4. Use -trimpath instead of remove-go-references, as you did.  Also, to
avoid rebuilding the standard library with '-trimpath' for every package
(since the Go build cache does not persist between build environments):

  a) modify the Go package to build standard libraries with -trimpath,
     which would unfortunately mean most users of the Go package would
     find that ~180MB of space wasted; or
  
  b) build a '-trimpath' version of the standard library separately and
     use it with '-pkgdir' (which would prevent #3 from working) or by
     building a directory union of Go and Go-std-library-with-trimpath
     and setting GOROOT=/path/to/union.

Personally, I'm partial to a), along with removing the pre-compiled
standard library from the Go package since it ends up recompiled more
often than not, is very fast to recompile, and it will eventually no
longer be distributed or used by Go [0].

>
> The context is that I needed to hack on a Go package, and was somewhat
> surprised that my usual workflow of "guix environment PKG" did not work.
>
> It still does not work "out of the box", but these patches bring it a
> step further.  Now "all" that is needed is to:
>
>   $ cd ~/src/go-foo
>   $ guix environment go-example-com-foo
>   $ MYGOPATH="$HOME/tmp/go"
>   $ NAMESPACE="$MYGOPATH/src/example.com/foo"
>   $ mkdir -p $(dirname $NAMESPACE)
>   $ ln -s $PWD $NAMESPACE    # or git worktree add $NAMESPACE
>   $ export GOPATH="$MYGOPATH:$GOPATH"
>   $ go build                 # no 'go get' necessary!

Interesting.  I hadn't thought of the use-case for actually hacking on go
packages like this!  I'll have to think of how modules-mode can be made
to work with this.

(A digression: the current issue with fully implementing module-aware
mode is that Go really wants a specific version for each dependency.  If
we just populate the module cache with the versions we have, it will
inevitable complain when a package we try to build wants a version we do
not have.  I see a few solutions:

1. Put all dependencies in the module cache, and rewrite the main
module's go.mod (that is, add replace directives) to replace all
dependencies with the versions we have.

2. Rewrite the go.mod to replace all dependencies with the local
filesystem path of the versions we have.

3. Put all dependencies in the vendor/ directory, and use -mod=vendor.
Any pre-existing vendor directory must be handled properly.

These three solutions fail to allow re-using the build cache (and
therefore build artifacts), because Go computes the build cache keys
differently for main and non-main modules.  Building in Go is generally
fast, so we probably shouldn't compromise much to enable reusing the
build cache, but a few ideas for doing so:

4. Set up a dummy go.mod out of the source tree, which 'replace's all
dependencies AND the module we're building like in 1) or 2).  This may
have to account for replace directives in the go.mod of the module we're
building, though.

5. Put the module we're building in the module cache, and build it with
"go install module@version".  The same caveat as in 4) applies, as well
as that "go install module@version" only works for main packages (that
is, packages which produce an executable).)

>
> I don't know how feasible it is to avoid making a local directory and
> symlinking the project to the expected namespace.  Still a complete Go
> newbie, but this approach feels more natural and idiomatic Guix-wise.

My intuition is that if you're working in GOPATH-mode, you already have
a ~/go/src directory or similar, and your project is probably under
~/go/src/my-project. Then, in order to hack on it Guix-like, you would

  $ cd ~/go/src/my-project
  $ guix environment go-github-com-me-my-project
  $ export GOPATH=~/go:$GOPATH
  $ go build

I'm not sure what a similar idiom for Guix-like hacking in module-aware
mode would be; we'd have to set GOMODCACHE or something, but it would be
very easy for Go to overwrite (or fail to overwrite) things without
GOPROXY=off.  Alternatively, if we make a "full" go proxy directory
layout, we can do

  GOPROXY=file://path/to/gomodcache

or even a search path like

  GOPROXY=file:///gnu/store/p1/gomodcache|file://gnu/store/p2/gomodcache

though I'm not sure how well that would scale w.r.t. number of packages.

Both of these GOPROXY methods have the advantage over setting GOMODCACHE
that the user could modify GOPROXY to include the default proxy, and
would still be able to get packages and versions not packaged by Guix.

I suppose there's no reason we couldn't set both GOPATH and
e.g. GOPROXY.

[0] https://github.com/golang/go/issues/47257

--
Sarah




  parent reply	other threads:[~2021-08-28  2:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-27 15:10 [bug#50227] [PATCH 0/3] go-build-system and GOPATH improvements Marius Bakke
2021-08-27 15:13 ` [bug#50227] [PATCH 1/3] build-system/go: Use a native-search-path for GOPATH Marius Bakke
2021-08-27 15:13   ` [bug#50227] [PATCH 2/3] gnu: hyperledger-fabric: Do not assume GOPATH contains a single entry Marius Bakke
2021-08-27 15:13   ` [bug#50227] [PATCH 3/3] gnu: go-gotest-tools-assert: Provide internal inputs with the source Marius Bakke
2021-08-27 16:44 ` [bug#50227] [PATCH] build-system/go: Trim store references using the native compiler option Marius Bakke
2021-08-27 17:47   ` Marius Bakke
2021-08-27 19:38     ` Marius Bakke
2021-08-28  2:16 ` Sarah Morgensen [this message]
2021-08-28 14:52   ` [bug#50227] [PATCH 0/3] go-build-system and GOPATH improvements Marius Bakke
2022-01-14  3:13     ` Maxim Cournoyer
2021-08-29  6:17 ` [bug#50227] [PATCH 3/3] gnu: go-gotest-tools-assert: Provide internal inputs with the source Sarah Morgensen
2021-09-03 22:55 ` [bug#50227] [PATCH 0/3] go-build-system and GOPATH improvements Sarah Morgensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86mtp248ku.fsf@mgsn.dev \
    --to=iskarian@mgsn.dev \
    --cc=50227@debbugs.gnu.org \
    --cc=marius@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).