Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
main: fix bug with major version >= 2 being installed to wrong target
Browse files Browse the repository at this point in the history
We previously had a test that ensure the cache path of a major package
>= 2 was correct, but this failed to ensure the installed binary had the
right target name.

Fix the test and fix the bug.

Fixes #82
  • Loading branch information
myitcv committed Aug 27, 2019
1 parent 350ab10 commit 773807a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ os:
matrix:
include:
- os: linux
go: "1.11.x"
go: "1.13beta1"
env: CHECK_GOFMT=1

env:
Expand Down
25 changes: 5 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,22 +360,10 @@ func mainerr() error {
gobin = filepath.Join(gobinCache, fmt.Sprintf("%x", h.Sum(nil)))
}

var base string
if mp.Module.Path == mp.ImportPath {
pref, _, ok := module.SplitPathVersion(mp.ImportPath)
if !ok {
return fmt.Errorf("failed to derive non-version prefix from %v", mp.ImportPath)
}
base = path.Base(pref)
} else {
base = path.Base(mp.ImportPath)
}
// mp.Target already has .exe for Windows
base := filepath.Base(mp.Target)
target := filepath.Join(gobin, base)

if runtime.GOOS == "windows" {
target += ".exe"
}

// optimistically remove our target in case we are installing over self
// TODO work out what to do for Windows
if mp.ImportPath == "github.com/myitcv/gobin" {
Expand Down Expand Up @@ -406,7 +394,7 @@ func mainerr() error {
fmt.Printf("%v %v\n", mp.Module.Path, mp.Module.Version)
case *fRun:
run := exec.Command(target, runArgs...)
run.Args[0] = path.Base(mp.ImportPath)
run.Args[0] = filepath.Base(mp.Target)
run.Stdin = os.Stdin
run.Stdout = os.Stdout
run.Stderr = os.Stderr
Expand All @@ -430,12 +418,8 @@ func mainerr() error {
return fmt.Errorf("failed to open %v: %v", target, err)
}
defer src.Close()
bin := filepath.Join(installBin, path.Base(mp.ImportPath))

if runtime.GOOS == "windows" {
bin += ".exe"
}

bin := filepath.Join(installBin, filepath.Base(mp.Target))
openMode := os.O_CREATE | os.O_WRONLY

// optimistically remove our target in case we are installing over self
Expand Down Expand Up @@ -466,6 +450,7 @@ type listPkg struct {
ImportPath string
Name string
Dir string
Target string
Module struct {
Path string
Dir string
Expand Down
4 changes: 4 additions & 0 deletions testdata/major_version.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# at the end of the main package path that we end up with
# the right binary name

gobin example.com/good/v2
stdout '^Installed example\.com/good/v2@v2\.0\.0 to '${WORK@R}'[/\\]gopath[/\\]bin[/\\]good'$exe$
! stderr .+

gobin -p example.com/good/v2
stdout [/\\]good$exe$
! stderr .+

0 comments on commit 773807a

Please sign in to comment.