Commit 593d8b0c authored by Dave Cheney's avatar Dave Cheney

cmd/go: remove $GOROOT as a go get target

Fixes #4186.

Back in the day, before the Go 1.0 release, $GOROOT was mandatory for building from source. Fast forward to now and $GOPATH is mandatory and $GOROOT is optional, and mainly used by those who use the binary distribution in uncommon places.

For example, most novices at least know about `sudo` as they would have used it to install the binary tarball into /usr/local. It is logical they would use the `sudo` hammer to `go get` other Go packages when faced with a permission error talking about the path they just had to use `sudo` on last time.

Even if they had read the documentation and set $GOPATH, go get will not work as expected as `sudo` masks most environment variables.

llucky(~) % ~/go/bin/go env | grep GOPATH
GOPATH="/home/dfc"
lucky(~) % sudo ~/go/bin/go env | grep GOPATH
GOPATH=""

This CL therefore proposes to remove support for using `go get` to download source into $GOROOT.

This CL also proposes an error when GOPATH=$GOROOT, as this is another place where new Go users can get stuck.

Further discussion: https://groups.google.com/d/topic/golang-nuts/VIg3fjHiHRI/discussion

R=rsc, adg, minux.ma
CC=golang-dev
https://golang.org/cl/6941058
parent 5ca4f5e4
...@@ -72,21 +72,38 @@ Functions written in assembly will need to be revised at least ...@@ -72,21 +72,38 @@ Functions written in assembly will need to be revised at least
to adjust frame pointer offsets. to adjust frame pointer offsets.
</p> </p>
<h3 id="race">Data race detector</h3> <h3 id="gotool">Changes to the go tool</h3>
<p> <p>The <code>go</code> tool has acquired several improvements which are intended to improve the experience for new Go users.</p>
The implementation now includes a built-in <a href="/doc/articles/race_detector.html">data race detector</a>.
<p>Firstly, when compiling, testing, or running Go code, the <code>go</code> tool will now give more detailed errors messages, including a list of paths searched, when a package cannot be located.
</p> </p>
<h3 id="symtab">Symbol table</h3> <pre>
$ go build foo/quxx
can't load package: package foo/quxx: cannot find package "foo/quxx" in any of:
/home/User/go/src/pkg/foo/quxx (from $GOROOT)
/home/User/src/foo/quxx (from $GOPATH)
</pre>
<p> <p>
In the gc toolchain, the symbol table format has been extended to allow Secondly, the <code>go get</code> command no longer allows <code>$GOROOT</code> as the default destination when downloading package source. To use <code>go get</code> command, a valid <code>$GOPATH</code> is now required.
little-endian encoding of symbol values, and the extension is used in </p>
binaries generated by the Go 1.1 version of the gc linker.
To the Go 1.0 toolchain and libraries, these new symbol tables appear empty. <pre>
$ GOPATH= go get code.google.com/p/foo/quxx
package code.google.com/p/foo/quxx: cannot download, $GOPATH not set. For more details see: go help gopath
</pre>
<p>Finally, as a result of the previous change, the <code>go get</code> command will also fail when <code>$GOPATH</code> and <code>$GOROOT</code> are set to the same value.
</p> </p>
<pre>
$ GOPATH=$GOROOT go get code.google.com/p/foo/quxx
warning: GOPATH set to GOROOT (/home/User/go) has no effect
package code.google.com/p/foo/quxx: cannot download, $GOPATH must not be set to $GOROOT. For more details see: go help gopath
</pre>
<h2 id="library">Changes to the standard library</h2> <h2 id="library">Changes to the standard library</h2>
<h3 id="debug/elf">debug/elf</h3> <h3 id="debug/elf">debug/elf</h3>
......
...@@ -453,7 +453,7 @@ On Unix, the value is a colon-separated string. ...@@ -453,7 +453,7 @@ On Unix, the value is a colon-separated string.
On Windows, the value is a semicolon-separated string. On Windows, the value is a semicolon-separated string.
On Plan 9, the value is a list. On Plan 9, the value is a list.
GOPATH must be set to build and install packages outside the GOPATH must be set to get, build and install packages outside the
standard Go tree. standard Go tree.
Each directory listed in GOPATH must have a prescribed structure: Each directory listed in GOPATH must have a prescribed structure:
......
...@@ -247,16 +247,17 @@ func downloadPackage(p *Package) error { ...@@ -247,16 +247,17 @@ func downloadPackage(p *Package) error {
} }
if p.build.SrcRoot == "" { if p.build.SrcRoot == "" {
// Package not found. Put in first directory of $GOPATH or else $GOROOT. // Package not found. Put in first directory of $GOPATH.
// Guard against people setting GOPATH=$GOROOT. We have to use list := filepath.SplitList(buildContext.GOPATH)
// $GOROOT's directory hierarchy (src/pkg, not just src) in that case. if len(list) == 0 {
if list := filepath.SplitList(buildContext.GOPATH); len(list) > 0 && list[0] != goroot { return fmt.Errorf("cannot download, $GOPATH not set. For more details see: go help gopath")
p.build.SrcRoot = filepath.Join(list[0], "src")
p.build.PkgRoot = filepath.Join(list[0], "pkg")
} else {
p.build.SrcRoot = filepath.Join(goroot, "src", "pkg")
p.build.PkgRoot = filepath.Join(goroot, "pkg")
} }
// Guard against people setting GOPATH=$GOROOT.
if list[0] == goroot {
return fmt.Errorf("cannot download, $GOPATH must not be set to $GOROOT. For more details see: go help gopath")
}
p.build.SrcRoot = filepath.Join(list[0], "src")
p.build.PkgRoot = filepath.Join(list[0], "pkg")
} }
root := filepath.Join(p.build.SrcRoot, rootPath) root := filepath.Join(p.build.SrcRoot, rootPath)
// If we've considered this repository already, don't do it again. // If we've considered this repository already, don't do it again.
......
...@@ -186,7 +186,7 @@ On Unix, the value is a colon-separated string. ...@@ -186,7 +186,7 @@ On Unix, the value is a colon-separated string.
On Windows, the value is a semicolon-separated string. On Windows, the value is a semicolon-separated string.
On Plan 9, the value is a list. On Plan 9, the value is a list.
GOPATH must be set to build and install packages outside the GOPATH must be set to get, build and install packages outside the
standard Go tree. standard Go tree.
Each directory listed in GOPATH must have a prescribed structure: Each directory listed in GOPATH must have a prescribed structure:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment