Skip to content

Commit 159c2b7

Browse files
committed
cmd/go: fix error for 'go install x.go' when GOBIN is not set
Fixes #6191. Fixes #5426. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/13234052
1 parent a547ad6 commit 159c2b7

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/cmd/go/build.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ func runInstall(cmd *Command, args []string) {
311311

312312
for _, p := range pkgs {
313313
if p.Target == "" && (!p.Standard || p.ImportPath != "unsafe") {
314-
if p.ConflictDir != "" {
314+
if p.cmdline {
315+
errorf("go install: no install location for .go files listed on command line (GOBIN not set)")
316+
} else if p.ConflictDir != "" {
315317
errorf("go install: no install location for %s: hidden by %s", p.Dir, p.ConflictDir)
316318
} else {
317319
errorf("go install: no install location for directory %s outside GOPATH", p.Dir)
@@ -486,6 +488,7 @@ func goFilesPackage(gofiles []string) *Package {
486488
bp, err := ctxt.ImportDir(dir, 0)
487489
pkg := new(Package)
488490
pkg.local = true
491+
pkg.cmdline = true
489492
pkg.load(&stk, bp, err)
490493
pkg.localPrefix = dirToImportPath(dir)
491494
pkg.ImportPath = "command-line-arguments"

src/cmd/go/pkg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type Package struct {
8282
fake bool // synthesized package
8383
forceBuild bool // this package must be rebuilt
8484
forceLibrary bool // this package is a library (even if named "main")
85+
cmdline bool // defined by files listed on command line
8586
local bool // imported via local path (./ or ../)
8687
localPrefix string // interpret ./ and ../ imports relative to this prefix
8788
exeName string // desired name for temporary executable

src/cmd/go/test.bash

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,16 @@ fi
150150

151151
# Without $GOBIN set, installing a program outside $GOPATH should fail
152152
# (there is nowhere to install it).
153-
TEST install without destination
154-
if ./testgo install testdata/src/go-cmd-test/helloworld.go; then
153+
TEST install without destination fails
154+
if ./testgo install testdata/src/go-cmd-test/helloworld.go 2>testdata/err; then
155155
echo "go install testdata/src/go-cmd-test/helloworld.go should have failed, did not"
156156
ok=false
157+
elif ! grep 'no install location for .go files listed on command line' testdata/err; then
158+
echo "wrong error:"
159+
cat testdata/err
160+
ok=false
157161
fi
162+
rm -f testdata/err
158163

159164
# With $GOBIN set, should install there.
160165
TEST install to GOBIN '(command-line package)'

0 commit comments

Comments
 (0)