Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/types: cannot locate transient dependency saved via Go modules #27556

Closed
dradtke opened this issue Sep 7, 2018 · 4 comments
Closed

go/types: cannot locate transient dependency saved via Go modules #27556

dradtke opened this issue Sep 7, 2018 · 4 comments

Comments

@dradtke
Copy link

dradtke commented Sep 7, 2018

What version of Go are you using (go version)?

go version go1.11 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/damien/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/damien/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build423725469=/tmp/go-build -gno-record-gcc-switches"

What did you do?

1. Ensure a clean slate.

Steps 2 and 3 set up a fresh Go development environment on a new Linux server for maximum reproducibility (I spun up a new CentOS server on Vultr). Alternatively, delete ${GOPATH}/pkg and ${GOPATH}/src/golang.org/x/text if they exist.

2. Install the latest Go:

# run as root
$ cd /usr/local
$ wget https://dl.google.com/go/go1.11.linux-amd64.tar.gz
$ tar xvf go1.11.linux-amd64.tar.gz

# also need to install Git on a fresh server
$ yum install git

3. Create a new user and set them up for Go development:

# run as root
$ useradd --create-home damien
$ su - damien

# now run as damien
$ mkdir go
$ export GOPATH=~/go
$ export PATH=/usr/local/go/bin:${PATH}

4. Create a new project outside of GOPATH.

I created a new workspace directory, and inside that added app/app.go:

// ~/workspace/app/app.go
package app

import (
	"golang.org/x/text/unicode/norm"
)

func UnicodeVersion() string {
	return norm.Version
}

I then ran the following to enable Go modules and install its dependency:

$ go mod init app
$ go get golang.org/x/text/unicode/norm

The app folder now contains 3 files: app.go, go.mod, and go.sum, all with the expected contents. Of note is the fact that go.mod does not contain a direct reference to golang.org/x/text/unicode/norm, but instead its parent package:

module app

require golang.org/x/text v0.3.0 // indirect

Running go build ensures that everything works, and indeed we can see the dependency stored at ~/go/pkg/mod/golang.org/x/text\@v0.3.0/.

5. Use go/types to type-check it.

I wrote a small program that uses the go/types package to typecheck a program: https://play.golang.org/p/5wWtuNqYv1n, which should be saved in ~/workspace.

What did you expect to see?

Given that the program builds fine, I would expect to see no errors when type-checking it.

What did you see instead?

The program fails with the following error:

2018/09/07 14:43:05 app/app.go:4:2: could not import golang.org/x/text/unicode/norm (type-checking package "golang.org/x/text/unicode/norm" failed (/home/damien/go/pkg/mod/golang.org/x/text@v0.3.0/unicode/norm/normalize.go:15:2: could not import golang.org/x/text/transform (cannot find package "golang.org/x/text/transform" in any of:
	/usr/local/go/src/golang.org/x/text/transform (from $GOROOT)
	/home/damien/go/src/golang.org/x/text/transform (from $GOPATH))))
exit status 1

In addition, using importer.Default() instead results in an even simpler error:

2018/09/07 14:47:04 app/app.go:4:2: could not import golang.org/x/text/unicode/norm (can't find import: "golang.org/x/text/unicode/norm")
exit status 1
@myitcv
Copy link
Member

myitcv commented Sep 7, 2018

With Go modules, you need to use golang.org/x/tools/go/packages (often shorted to just go/packages) to type check your code. go/packages uses the Go tool in order to be module-aware. As you can see from the definition of go/packages.Package, it has a field of type go/types.Package.

There is more discussion about go/packages happening in https://groups.google.com/forum/#!forum/golang-tools and in #tools on Gophers Slack.

I can't really say this is "working as intended", but I'm going to close the issue in any case. Please feel free to join us in golang-tools in case you have any queries about the use of go/packages.

@myitcv myitcv closed this as completed Sep 7, 2018
@dradtke
Copy link
Author

dradtke commented Sep 7, 2018

@myitcv How stable would you say go/packages is? I see an all-caps note at the top of the docs page saying that it's not yet ready for widespread use.

@myitcv
Copy link
Member

myitcv commented Sep 7, 2018

That warning is more relevant to pre the Go 1.11 release (cc @matloob - any update required here?).

go/packages is being used as the basis for the rewrite of many (if not all) go tools: #24661. It is build system agnostic, understands modules, will be more powerful etc. It should be used for all tools requiring anything from module/package file lists, to syntax trees, type information and much more.

Hence that warning should probably be read as "please start using and road testing go/packages, on the understanding we might make minor API changes".

@ianthehat's team is responsible for go/packages so I'll let him add anything I might have missed.

@ianthehat
Copy link

We will probably be changing the warning soon, at this point its more along the lines of: the API is fairly stable, we will be trying hard not to break it without a really good reason, and we would prefer if you are using it you come talk to us in golang-tools in case we do need to break it so we can let you know. We are not yet ready to promise it's fully stable, we need to be able to make changes when we find a tool that really needs something it cannot support.

@golang golang locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants