Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am having trouble getting golang.org/x/tools/cmd/goimports

Tags:

go

when I get golang.org/x/tools/cmd/goimports

go get golang.org/x/tools/cmd/goimports

...I get the following error:

package golang.org/x/tools/cmd/goimports: unrecognized import path "golang.org/x/tools/cmd/goimports"

and I tried to compile the goimports from source code, so I download it from

http://github.com/golang/tools.git and https://github.com/bradfitz/goimports

and how to compile it ?

like image 360
JohnsonDiao Avatar asked Dec 09 '22 02:12

JohnsonDiao


1 Answers

This answer will give an option to work around a failed go get due to network blocking. Note that if there is a configuration problem with your Go installation (such as an incorrect GOPATH), such failures should not be worked around but the underlying problem fixed; otherwise you'll just have later errors/failures. However, in this specific case apparently the root cause of the failure is unfixable due to no network access to the resource required. (Although I'd have expected a better, more specific error message from go get in that case; perhaps instead of just blocking an HTTP request has been replaced with a page that contains some data that confuses go get).

First, what is go get golang.org/x/tools/cmd/goimports trying to do. The documentation says it "downloads and installs the packages named by the import paths, along with their dependencies."

It's important to realize two things: one, it does this by fetching the package sources (usually by cloning a VCS repository) and then building the package(s)/program(s); and two, if the source directory appears to already exist it does not fetch or update anything. The later can cause issues if something undetected went wrong with a previous go get as you'll be stuck with the "corrupt" sources. If this happens you can either use the -u flag to attempt an update or you can remove the old/corrupt/incomplete directory from your GOPATH and try a fresh go get.

The documentation also describes how it interprets "remote import paths", basically trying to match it to a know code hosting service (GitHub, Bitbucket, etc) or to a remote version control system repository (git, mercurial, etc) or doing an HTTPS/HTTP request and looking for a meta name="go-import" … tag.

You can see some of this by using go get -v:

# go get -v golang.org/x/tools/cmd/goimports
Fetching https://golang.org/x/tools/cmd/goimports?go-get=1
Parsing meta tags from https://golang.org/x/tools/cmd/goimports?go-get=1 (status code 200)
get "golang.org/x/tools/cmd/goimports": found meta tag main.metaImport{Prefix:"golang.org/x/tools", VCS:"git", RepoRoot:"https://go.googlesource.com/tools"} at https://golang.org/x/tools/cmd/goimports?go-get=1
get "golang.org/x/tools/cmd/goimports": verifying non-authoritative meta tag
Fetching https://golang.org/x/tools?go-get=1
Parsing meta tags from https://golang.org/x/tools?go-get=1 (status code 200)
golang.org/x/tools (download)
golang.org/x/tools/go/ast/astutil
golang.org/x/tools/imports
golang.org/x/tools/cmd/goimports

In this case go get does an HTTPS request to golang.org to discover the location of the source and effectively does¹:

mkdir -p $GOPATH/src/golang.org/x/tools
git clone https://go.googlesource.com/tools $GOPATH/src/golang.org/x/tools
go build golang.org/x/tools/cmd/goimports

It found that golang.org/x/tools/cmd/goimports is part of golang.org/x/tools and downloads that whole repository by doing a git clone and then building the goimports command and it's two dependant packages (which conveniently are within the same repository, otherwise there would be multiple clone/download steps). The results are put under $GOPATH/bin (and $GOPATH/pkg/$GOOS_$GOARCH).

So if go get isn't working or is blocked from accessing golang.org the first thing to try would be the above commands. Failing that you could substitute a mirror (e.g. git clone https://github.com/golang/tools.git $GOPATH/golang.org/x/tools), or you could otherwise download the contents of the repository from somewhere and unpack them into the correct location within your GOPATH, e.g. perhaps something like:

curl https://github.com/golang/tools/archive/master.zip
unzip -d $GOPATH/src/golang.org/x/tools master.zip

Once the source for all dependant packages is "somehow" put into GOPATH/src you should be able to go build {path}. The important part here is to make sure the sources are where the go tools expects them to be, that is, under $GOPATH/src/{go/import/path}.

If only some domains (such as golang.org) are blocked you could do this for only the blocked repositories and then do go get golang.org/x/tools/cmd/goimports; since go get won't try to fetch existing directories this should only fetch any missing dependencies before building everything.

¹ Note, these examples assume the common case of GOPATH being a single directory. If instead your GOPATH is multiple directories, e.g. GOPATH=$HOME/go.public:$HOME/go.private, then you'd need to substitute the first path component of your GOPATH.

like image 116
Dave C Avatar answered Dec 11 '22 07:12

Dave C