Skip to content
This repository has been archived by the owner on Feb 7, 2022. It is now read-only.

Enabled usage of go modules #144

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

kolaente
Copy link

@kolaente kolaente commented Dec 7, 2018

Resolves #138

This pr adds the automatic lookup for a go.mod file and if found, sets GO111MODULE=on on the build container to enable go modules instead of gopath.

I also want to pass -mod=vendor to the build script if a vendor folder is found and modules are enabled. How do I do that? I couldn't find a good way using existing flags and environment variables, should I introduce a new env variable for the build script for that?

@kolaente kolaente mentioned this pull request Dec 7, 2018
xgo.go Outdated Show resolved Hide resolved
@nickoneill
Copy link

Does this work for anyone? I tried go installing this branch for use, but I don't see a difference for my xgo deployment. I just transitioned an xgo project to modules and ran into this, I would love to keep using modules but can't seem to get things working with xgo anymore.

@zgramana
Copy link

zgramana commented Jan 16, 2019

I have issued a PR to @kolaente's feature/build-with-modules branch (kolaente#1) which completes the work for #138 that enables module support. Once/if he merges it (update: he has), then it will also show up here for review and merge.

@nickoneill @kolaente's branch now works, though be sure to read my notes on the PR (in particular regarding the docker image). If you aren't comfortable with docker, let me know and I'll publish an image that you can use to Docker Hub.

@kolaente
Copy link
Author

I've merged the pr, please re-review this one. @gordonklaus

@zgramana
Copy link

Or perhaps even @karalabe?

@zgramana
Copy link

@kolaente probably worth removing the WIP: from the title too, just to avoid confusion. :)

@kolaente kolaente changed the title WIP: Enabled usage of go modules Enabled usage of go modules Jan 16, 2019
Copy link

@gordonklaus gordonklaus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone else should probably take a look as I'm not familiar with the code.

docker/base/build.sh Show resolved Hide resolved
args = append(args, []string{"-v", fmt.Sprintf("%s:%s:ro", locals[i], mounts[i])}...)
if usesModules {
args = append(args, []string{"-e", "GO111MODULE=on"}...)
args = append(args, []string{"-v", os.Getenv("GOPATH") + ":/go"}...)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If GOPATH is empty, then we should use the default GOPATH ($HOME/go). It looks like go/build.Default.GOPATH will do the trick.

Also, should probably join using filepath.ListSeparator for portability.

Copy link

@zgramana zgramana Jan 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base Dockerfile currently maps GOPATH to /go, so all we're doing here is ensuring that we mount the users GOPATH in order to avoid go build hitting the network by getting all the packages (unless it needs to).

Do need to ensure GOPATH is a valid path first, though. That happens in the previous two cases, but not in this new one. I just missed that.

The "/go" is actually a docker-specific thing, and uses the same cli syntax on Windows.

@kolaente
Copy link
Author

kolaente commented Feb 5, 2019

@karalabe Would you mind reviewing this?

@nickoneill
Copy link

@zgramana I'd love to test this. I'm not familiar with how to generate docker images, but I'd give it a shot with some instructions.

@zgramana
Copy link

zgramana commented Feb 9, 2019

@nickoneill 😎 If you change into the docker/go-1.11.x directory and run docker build you’ll end up with the image you need to test with. You may want to pass a custom name for the resulting docker image to make it easier to know what name to pass xgo.

if strings.HasPrefix(config.Repository, string(filepath.Separator)) || strings.HasPrefix(config.Repository, ".") {
// Resolve the repository import path from the file path
config.Repository = resolveImportPath(config.Repository)

// Determine if this is a module-based repository
var modFile = config.Repository + "/go.mod"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently discovered that go env provides GOMOD which makes this easier.

  • When a repository contains a modules-based project, and GO111MODULES is either on or auto, go env GOMOD will contain the path to the go.mod file.
  • When GO111MODULES is either off or auto, the GOMOD var will be the empty string.
  • Interestingly, if GO111MODULES is set to on but the current repository is not a module-based project, then it will (at least sometimes) contain a path to a go.mod file which does not actually exist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* Interestingly, if `GO111MODULES` is set to `on` but the current repository is not a module-based project, then it will (at least sometimes) contain a path to a `go.mod` file which does not actually exist.

If thats the case I wouldn't want to rely on that env to find the mod file... Maybe this changes with Go 1.12 or 1.13, but as of now...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This same debate is being had by the golang team itself. It appears that go env GOMOD is intended to be The One Right Answer, but as the discussion on this issue illustrates, even the go build tool diverges. That same thread also confirms that the algorithm for determining module status is volatile and likely to change in the future.

Perhaps even more interesting is this comment:

I put GOMOD in 'go env' mainly so that it would be listed in bug reports. The expected way to get at this is go list -m -f {{.GoMod}} but I have not added that yet. :-)

The interesting thing is that, as of go 1.11.4 (at least), it actually does work (at least for my quick check on the command-line). So, worth looking at again in the future, especially given that GOPATH is expected to be deprecated at some point in the semi-distant future.

I'd like to see this PR get merged in the meanwhile--given how quickly people are adopting modules--and open issues + PR's to refactor and enhance later on.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see this PR get merged in the meanwhile--given how quickly people are adopting modules--and open issues + PR's to refactor and enhance later on.

Yes, I'm with you on that.
Also, go modules will become the new standard in 1.13, which will first deprecate and later drop GOPATH entirely, so we should get this merged.

At Gitea, we also need this, and because this repo looks not maintained anymore and other things we may start a fork of xgo (and maintain it).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karalabe Can you chime in?

@qianlifeng
Copy link

Any updates?

@kolaente
Copy link
Author

@qianlifeng Doesn't look like it

@karalabe Any chances this will be merged?

@vorlif
Copy link

vorlif commented May 11, 2019

For all people who are waiting for it, have a look at the fork https://github.com/techknowlogick/xgo. It is currently being maintained and the problem has already been solved there.

@tboerger
Copy link

Looks like xgo is more or less dead and we got to use the fork?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using with go mod
7 participants